Custom Password box)

Source: Internet
Author: User
Custom Password box

I want to create a custom edit box. I found this in the online materials and forwarded it for later use. I will make my own edit box and send it next time.

The method in this score is as follows:

UpperCode:

Based on ceikedwin:
# Ifndef _ eiksecretedwin_h __
# DEFINE _ eiksecretedwin_h __

// Des
# Include <eikedwin. h>

Class ceiksecretedwin: Public ceikedwin
{
Public: // constructors and destructor
Ceiksecretedwin ();
Virtual ~ Ceiksecretedwin ();
Void constructl (tint acount );
Void draw (const trect & arect) const;
Tkeyresponse offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype );
Void gettext (TDES & ades) const;
PRIVATE: // functions from base classes
Tint icount;
Hbufc * ipassword;
};
# Endif/_ eiksecretedwin_h __
// End of File

# Include "eiksecretedwin. h"

Ceiksecretedwin: ceiksecretedwin ()
{
Ipassword = NULL;
}

Ceiksecretedwin ::~ Ceiksecretedwin ()
{
If (ipassword)
{
Delete ipassword;
Ipassword = NULL;
}
}

Void ceiksecretedwin: constructl (tint acount)
{
Icount = acount;
Ceikedwin: constructl (eakneditorflagdefault | eeikedwinnoautoselection | eeikedwinjustautocurend, 0, acount, 1 );
Ceikedwin: setreadonly (etrue );
Ipassword = hbufc: newl (acount );
}

Void ceiksecretedwin: Draw (const trect & arect) const
{
Ceikedwin: Draw (arect );
}

Tkeyresponse ceiksecretedwin: offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype)
{
If (Atype = eeventkey) & akeyevent. iscancode> = 48 & akeyevent. iscancode <= 57)
{
If (ipassword-> length () <icount)
{
Ipassword-> des (). appendnum (akeyevent. iscancode-48 );
Hbufc * pbuf = hbufc: newl (icount );
For (tint I = 0; I <ipassword-> length (); ++ I)
{
Pbuf-> des (). append (_ L ("*");
}
Ceikedwin: settextl (pbuf );
Delete pbuf;
Drawdeferred ();
}
Return ekeywasconsumed;
}
If (Atype = eeventkey & akeyevent. iscancode = 1)
{
If (ipassword-> length ()> 0)
{
Ipassword-> des (). Copy (ipassword-> mid (0, ipassword-> length ()-1 ));
}
Hbufc * pbuf = hbufc: newl (icount );
For (tint I = 0; I <ipassword-> length (); ++ I)
{
Pbuf-> des (). append (_ L ("*");
}
Ceikedwin: settextl (pbuf );
Delete pbuf;
Drawdeferred ();
Return ekeywasconsumed;
}

Return ekeywasnotconsumed;
}

Void ceiksecretedwin: gettext (TDES & ades) const
{
Ades. Copy (* ipassword );
}

Usage:
Use a dynamic method to create an instance. Use the same method as ceikedwin.
In the constructl (Tint) function, you only need to pass the maximum length of one password.

Ccoecontrol-based

# Ifndef _ customsecreteditor_h __
# DEFINE _ customsecreteditor_h __

// Des
// System regiondes
// # Include <e32base. h> // cbase, link against euser. Lib
// Forward declarations
// Class myclass;

# Include <aknview. h>

// Class declaration

Class ccustomsecreteditor: Public ccoecontrol
{
Public:
// Set the background color of the password box
Void setsecreteditorbackground (trgb acolor );
// Start to draw the cursor
Void start ();
// Draw the cursor
Void drawcursor ();
// Callback function
Static tint periodictimercallback (Tany * Aany );
// Set the password length
Void setpasswordlength (tint alength );
// Obtain the password
Void GetPassword (TDES & ades) const;
// Set the focus
Void setfocus (tbool afocus );
// Determine whether there is a focus
Tbool isfocused ();
Public:
Ccustomsecreteditor ();
Virtual ~ Ccustomsecreteditor ();
Void constructl (const trect & arect );
Tkeyresponse offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype );
Void setextent (tpoint aextentpoint, tsize aextentsize );
PRIVATE:
// Drawing
Void draw (const trect & arect) const;
PRIVATE:
// The region controlled by the Password box
Trect isecreteditorrect;
// Background color of the password box
Trgb isecreteditorbackground;
// Timer
Cperiodic * iperiodictimer;
// Blinking cursor
Tint icursorflag;
// How many characters are entered
Tint icharacteramount;
// Draw two points of the cursor
// The above Vertex
Tpoint iuppoint;
// The following Vertex
Tpoint idownpoint;
// Password width
Tint itextwidth;
// Password height
Tint itextheight;
// Draw the coordinates of the cryptographic symbols
Tpoint ipasswordflag;
// Set the password length
Tint ilength;
// Password
Hbufc * iinputpassword;
// Whether there is a focus
Tbool ifocus;
};

# Endif // customsecreteditor_h

/*
========================================================== ==========================================
Name: customsecreteditor. cpp
Author:
Version:
Copyright: Your copyright notice
Description: customsecreteditor. cpp-source file
========================================================== ==========================================
*/

// Include files
// Class include
# Include "customsecreteditor. h"
// System regiondes
// # Include <e32base. h> // For cbase, link against euser. Lib
// # Include <resourcefile. RSG>
// User regiondes
// # Include "myheaderfile. h"

// =========================== Member functions ============================ ====

# Include <e32base. h>
# Include <eikenv. h>
# Include <GDI. h>

# Include <aknnotewrappers. h>

# Define memfree (A) if (a) {Delete A; A = NULL ;}

Const tint kperiodictimerinterval1sec (500000 );

// Construct
Ccustomsecreteditor: ccustomsecreteditor (): isecreteditorbackground (trgb (krgbred )),
Icursorflag (0 ),
Icharacteramount (0 ),
Ilength (0 ),
Ifocus (efalse)
{
Icursorflag = 0;
Iinputpassword = NULL;
}

// Structure
Ccustomsecreteditor ::~ Ccustomsecreteditor ()
{
If (ifocus)
{
Iperiodictimer-> cancel ();
Memfree (iperiodictimer );
}
// Else
//{
// Memfree (iperiodictimer );
//}
Memfree (iinputpassword );
}

// Second segment structure
Void ccustomsecreteditor: constructl (const trect & arect)
{
Isecreteditorrect = arect;
Iuppoint = tpoint (isecreteditorrect. ITL. IX + 1, isecreteditorrect. ITL. Iy + 2 );
Idownpoint = tpoint (isecreteditorrect. ITL. IX + 1, isecreteditorrect. IBR. iY-2 );
Itextwidth = ceikonenv: static ()-> titlefont ()-> charwidthinpixels (1)/2;
Itextheight = ceikonenv: static ()-> titlefont ()-> heightinpixels ()/2;
Ipasswordflag = tpoint (isecreteditorrect. ITL. IX, (isecreteditorrect. Height ()-itextheight)/2 + isecreteditorrect. ITL. Iy );
}

// Set the password length
Void ccustomsecreteditor: setpasswordlength (tint alength)
{
Ilength = alength;
Iinputpassword = hbufc: newl (ilength );
}

// Button Processing
Tkeyresponse ccustomsecreteditor: offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype)
{
If (ifocus)
{
If (Atype = eeventkey) & (akeyevent. iscancode> = 48 & akeyevent. iscancode <= 57 ))
{
If (iinputpassword-> length () <ilength)
{
Iinputpassword-> des (). appendnum (akeyevent. iscancode-48 );
Icharacteramount = (icharacteramount + 1 );
// The X axis coordinate of the point above the cursor + the width of the cryptographic symbol
Iuppoint. IX = (iuppoint. IX + itextwidth );
// The X axis coordinates of the points below the cursor + the width of the cryptographic symbol
Idownpoint. IX = (idownpoint. IX + itextwidth );
Drawnow (isecreteditorrect );
}
Return ekeywasconsumed;
}
Else if (Atype = eeventkey) & (akeyevent. iscancode = 0 × 01 ))
{
If (iinputpassword-> length ()> 0)
{
Iinputpassword-> des (). Copy (iinputpassword-> mid (0, iinputpassword-> length ()-1 ));
Icharacteramount = (icharacteramount-1 );
// X axis coordinate of the point above the cursor-width of the cryptographic symbol
Iuppoint. IX = (iuppoint. ix-itextwidth );
// X axis coordinate of the point below the cursor-width of the cryptographic symbol
Idownpoint. IX = (idownpoint. ix-itextwidth );
Drawnow (isecreteditorrect );
Return ekeywasconsumed;
}
}
}

Return ekeywasnotconsumed;
}

// Set the background color of the password box
Void ccustomsecreteditor: setsecreteditorbackground (trgb acolor)
{
Isecreteditorbackground = acolor;
}

// Flashing the cursor
Void ccustomsecreteditor: Start ()
{
If (ifocus)
{
If (iperiodictimer = NULL)
{
Iperiodictimer = cperiodic: newl (eprioritynormal );
Iperiodictimer-> Start (kperiodictimerinterval1sec, kperiodictimerinterval1sec, tcallback (periodictimercallback, this ));
}
}
}

// Callback function
Tint ccustomsecreteditor: periodictimercallback (Tany * Aany)
{
Ccustomsecreteditor * Self = (ccustomsecreteditor *) Aany;
Self-> drawcursor ();
Return 0;
}

// Draw the cursor
Void ccustomsecreteditor: drawcursor ()
{
If (ifocus)
{
Icursorflag = (icursorflag + 1) % 2;
Drawnow (isecreteditorrect );
}
}

// Drawing
Void ccustomsecreteditor: Draw (const trect &/* arect */) const
{
Cwindowgc & GC = systemgc ();
GC. setbrushstyle (cgraphicscontext: esolidbrush );
GC. setbrushcolor (isecreteditorbackground );

// Draws the background frame.
GC. drawrect (isecreteditorrect );

If (icursorflag = 1)
{
// Draw the cursor
If (ifocus)
{
GC. drawline (iuppoint, idownpoint );
}
}

// The password is marked
Tint characteramount = 0;
While (characteramount <icharacteramount)
{
GC. setbrushcolor (krgbblack );

Trect rect_password_flag = trect (tpoint (ipasswordflag. IX + itextwidth * characteramount, ipasswordflag. Iy ),
Tsize (iTextWidth-2, iTextHeight-2 ));
GC. drawellipse (rect_password_flag );

++ Characteramount;
}
}

Void ccustomsecreteditor: setextent (tpoint aextentpoint, tsize aextentsize)
{
Constructl (trect (aextentpoint, aextentsize ));
Ccoecontrol: setextent (aextentpoint, aextentsize );
}

Void ccustomsecreteditor: GetPassword (TDES & ades) const
{
If (iinputpassword-> length () = ilength)
{
Ades. Copy (* iinputpassword );
}
}

// Set the focus
Void ccustomsecreteditor: setfocus (tbool afocus)
{
Ifocus = afocus;
If (ifocus)
{
Start ();
}
Else
{
Iperiodictimer-> cancel ();
Memfree (iperiodictimer );
Icursorflag = 0;
Drawnow (isecreteditorrect );
}
}

// Determine whether there is a focus
Tbool ccustomsecreteditor: isfocused ()
{
Return ifocus;
}

// End of File

Usage:
Icustomsecreteditor = new (eleave) ccustomsecreteditor;
Icustomsecreteditor-> setcontainer1_wl (* This );
Icustomsecreteditor-> setextent (tpoint (80, 50), tsize (200, 30 ));
Icustomsecreteditor-> setpasswordlength (6 );
Icustomsecreteditor-> Start ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.