以前在看孙鑫老师的视频时,发现了一个在CDialog直接输入字母或文字的思路及其实现方法
说白了就是直接在对话框上画出文字
根据孙鑫老师的代码,经过了自己的小小的修改,发现还是比较好用的,现公布出来
实现代码:
href="http://www.j2megame.org/wupei/plugins/plogeshi/styles/plogeshi.css" type="text/css" rel="stylesheet" />
-
/
-
//相关成员变量声明
-
CFontDialog m_FontSetting; //字体选择对话框
-
CBitmap m_pBitmap; //缓冲图片
-
CString m_strLine; //字符串(为了中文显示正常,需要初始化为一个空格)
-
CPoint m_ptOrigin; //输入起点坐标
-
-
-
BOOL CMyDlg:: PreTranslateMessage (MSG* pMsg )
-
{
-
//判断WM_CHAR消息,也可以相应该消息
-
if (pMsg->message == WM_CHAR )
-
{
-
//取得字符(当是中文时,这个为半个中文,也就是1字节)
-
TCHAR nChar = pMsg->wParam;
-
-
/
-
//双缓冲实现,为了不使屏幕过分闪动(读者请根据需要修改)
-
CDC dc;
-
CDC* pDC = GetDC ( );
-
dc. CreateCompatibleDC (pDC );
-
dc. SelectObject (m_pBitmap );
-
/
-
-
//选择自己喜欢的字体,或者由外部指定
-
CFont font;
-
LOGFONT pLogFont;
-
m_FontSetting. GetCurrentFont (&pLogFont );
-
font. CreateFontIndirect (&pLogFont );
-
CFont *pOldFont=dc. SelectObject (&font );
-
-
//获得当前字体物理信息,比如高度,宽度,间隔.....
-
TEXTMETRIC tm;
-
dc. GetTextMetrics (&tm );
-
-
if (0x0d==nChar ) //回车
-
{
-
m_strLine= " "; //为了中文显示正常,需要初始化为一个空格
-
m_ptOrigin. y+= tm. tmHeight;
-
}
-
else if (0x08==nChar ) //退格
-
{
-
COLORREF clr=dc. SetTextColor (dc. GetBkColor ( ) );
-
dc. TextOut (m_ptOrigin. x,m_ptOrigin. y,m_strLine );
-
m_strLine=m_strLine. Left (m_strLine. GetLength ( ) -1 );
-
dc. SetTextColor (clr );
-
}
-
else
-
{
-
m_strLine+=nChar;
-
}
-
-
//提示符位置
-
CSize sz=dc. GetTextExtent (m_strLine );
-
CPoint pt;
-
pt. x=m_ptOrigin. x+sz. cx;
-
pt. y=m_ptOrigin. y;
-
SetCaretPos (pt );
-
-
//字的颜色
-
COLORREF oldClr = dc. SetTextColor (m_FontSetting. GetColor ( ) );
-
int oldbkMode = dc. SetBkMode (TRANSPARENT );
-
-
//输出
-
dc. TextOut (m_ptOrigin. x,m_ptOrigin. y,m_strLine );
-
-
dc. SetBkMode (oldbkMode );
-
dc. SetTextColor (oldClr );
-
-
dc. SelectObject (pOldFont );
-
-
pDC->DeleteDC ( );
-
-
Invalidate ( );
-
}
-
-
return false;
-
}
可以仔细想想退格和其他内部的实现,很好玩的.