自定义密码框
之前要做个自定义的编辑框,网上找资料发现了这个,转过来以备后用,自己做的编辑框下次发上来
这个分2中方法做的:
上代码:
基于CEikEdwin的:
#ifndef __EIKSECRETEDWIN_H__
#define __EIKSECRETEDWIN_H__
// INCLUDES
#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);
}
使用方法:
用动态方法创建,使用方法与CEikEdwin一样。
ConstructL(TInt)函数中只需传一密码最大长度值。
基于CCoeControl的
#ifndef __CUSTOMSECRETEDITOR_H__
#define __CUSTOMSECRETEDITOR_H__
// INCLUDES
// System Includes
//#include <e32base.h> // CBase, link against euser.lib
// FORWARD DECLARATIONS
//class MyClass;
#include <aknview.h>
// CLASS DECLARATION
class CCustomSecretEditor : public CCoeControl
{
public:
// 设置密码框的背景颜色
void SetSecretEditorBackground(TRgb aColor);
// 开始画光标
void Start();
// 画光标
void DrawCursor();
// 回调函数
static TInt PeriodicTimerCallBack(TAny* aAny);
// 设置密码的长度
void SetPasswordLength(TInt aLength);
// 获得密码的
void GetPassword(TDes& aDes) const;
// 设置焦点
void SetFocus(TBool aFocus);
// 判断是否有焦点
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:
// 绘图
void Draw( const TRect& aRect ) const;
private:
// 密码框所控制的区域
TRect iSecretEditorRect;
// 密码框的背景颜色
TRgb iSecretEditorBackground;
// 定时器
CPeriodic* iPeriodicTimer;
// 光标闪烁标记
TInt iCursorFlag;
// 输入多少字符的标记
TInt iCharacterAmount;
// 画光标的两个点
// 上面的点
TPoint iUpPoint;
// 下面的点
TPoint iDownPoint;
// 密码符号的宽度
TInt iTextWidth;
// 密码符号的高度
TInt iTextHeight;
// 画密码符号的坐标
TPoint iPasswordFlag;
// 设置密码的长度
TInt iLength;
// 密码
HBufC* iInputPassword;
// 是否有焦点
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 includes
//#include <e32base.h> // For CBase, link against euser.lib
//#include <ResourceFile.rsg>
// User includes
//#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);
// 构造
CCustomSecretEditor::CCustomSecretEditor():iSecretEditorBackground(TRgb(KRgbRed)),
iCursorFlag(0),
iCharacterAmount(0),
iLength(0),
iFocus(EFalse)
{
iCursorFlag = 0;
iInputPassword = NULL;
}
// 析构
CCustomSecretEditor::~CCustomSecretEditor()
{
if (iFocus)
{
iPeriodicTimer->Cancel();
MEMFREE(iPeriodicTimer);
}
// else
// {
// MEMFREE(iPeriodicTimer);
// }
MEMFREE(iInputPassword);
}
// 二段构造
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 );
}
// 设置密码的长度
void CCustomSecretEditor::SetPasswordLength(TInt aLength)
{
iLength = aLength;
iInputPassword = HBufC::NewL(iLength);
}
// 按键处理
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);
// 光标的上面的点的X轴坐标+密码符号的宽度
iUpPoint.iX = (iUpPoint.iX+iTextWidth);
// 光标的下面的点的X轴坐标+密码符号的宽度
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轴坐标-密码符号的宽度
iUpPoint.iX = (iUpPoint.iX-iTextWidth);
// 光标的下面的点的X轴坐标-密码符号的宽度
iDownPoint.iX = (iDownPoint.iX-iTextWidth);
DrawNow(iSecretEditorRect);
return EKeyWasConsumed;
}
}
}
return EKeyWasNotConsumed;
}
// 设置密码框的背景颜色
void CCustomSecretEditor::SetSecretEditorBackground(TRgb aColor)
{
iSecretEditorBackground = aColor;
}
// 闪动光标
void CCustomSecretEditor::Start()
{
if (iFocus)
{
if (iPeriodicTimer == NULL)
{
iPeriodicTimer = CPeriodic::NewL(EPriorityNormal);
iPeriodicTimer->Start(KPeriodicTimerInterval1Sec, KPeriodicTimerInterval1Sec, TCallBack(PeriodicTimerCallBack, this));
}
}
}
// 回调函数
TInt CCustomSecretEditor::PeriodicTimerCallBack(TAny* aAny)
{
CCustomSecretEditor* self = (CCustomSecretEditor*)aAny;
self->DrawCursor();
return 0;
}
// 画光标
void CCustomSecretEditor::DrawCursor()
{
if (iFocus)
{
iCursorFlag = (iCursorFlag + 1) % 2;
DrawNow(iSecretEditorRect);
}
}
// 绘图
void CCustomSecretEditor::Draw( const TRect& /*aRect*/ ) const
{
CWindowGc& gc = SystemGc();
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.SetBrushColor(iSecretEditorBackground);
// 画出了背景框
gc.DrawRect(iSecretEditorRect);
if (iCursorFlag == 1)
{
// 画光标
if (iFocus)
{
gc.DrawLine( iUpPoint, iDownPoint );
}
}
// 画出了密码标记
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);
}
}
// 设置焦点
void CCustomSecretEditor::SetFocus(TBool aFocus)
{
iFocus = aFocus;
if (iFocus)
{
Start();
}
else
{
iPeriodicTimer->Cancel();
MEMFREE(iPeriodicTimer);
iCursorFlag = 0;
DrawNow(iSecretEditorRect);
}
}
// 判断是否有焦点
TBool CCustomSecretEditor::IsFocused()
{
return iFocus;
}
// End of File
使用方法:
iCustomSecretEditor = new(ELeave) CCustomSecretEditor;
iCustomSecretEditor->SetContainerWindowL(*this);
iCustomSecretEditor->SetExtent(TPoint(80, 50), TSize(200, 30));
iCustomSecretEditor->SetPasswordLength(6);
iCustomSecretEditor->Start();