一个类似QQ登陆框的EDIT

    技术2022-05-11  135

    网易的登陆框也有这个功能,就是其中显示一串提示字符,当用户把输入焦点定位到EDIT中后,提示信息自动消失,这个类很简单,所以我没有做demo,大家看一下就会用了。

    .h文件:

    #if !defined(AFX_STEDIT_H__FA85F3FB_68AC_48E7_8108_5A61A09148CE__INCLUDED_)#define AFX_STEDIT_H__FA85F3FB_68AC_48E7_8108_5A61A09148CE__INCLUDED_

    #if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// STEdit.h : header file//

    /// CSTEdit window

    class CSTEdit : public CEdit{// Constructionpublic: CSTEdit();

    // Attributespublic:

    // Operationspublic:

    // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSTEdit) protected: virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); //}}AFX_VIRTUAL

    // Implementationpublic: virtual ~CSTEdit();

     // Generated message map functionsprotected: int nSetText; //{{AFX_MSG(CSTEdit) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnSetFocus(CWnd* pOldWnd); //}}AFX_MSG

     DECLARE_MESSAGE_MAP()};

    /

    //{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_STEDIT_H__FA85F3FB_68AC_48E7_8108_5A61A09148CE__INCLUDED_)

    .cpp文件:

    // STEdit.cpp : implementation file//

    #include "stdafx.h"#include "STEdit.h"

    #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif

    /// CSTEdit

    CSTEdit::CSTEdit(){ nSetText = 0;}

    CSTEdit::~CSTEdit(){}

    BEGIN_MESSAGE_MAP(CSTEdit, CEdit) //{{AFX_MSG_MAP(CSTEdit) ON_WM_LBUTTONDOWN() ON_WM_SETFOCUS() //}}AFX_MSG_MAPEND_MESSAGE_MAP()

    /// CSTEdit message handlers

    LRESULT CSTEdit::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) { if (nSetText < 2)  if (WM_SETTEXT == message)   nSetText++;  else if (WM_CHAR == message)   nSetText = 2;

     return CEdit::DefWindowProc(message, wParam, lParam);}

    void CSTEdit::OnLButtonDown(UINT nFlags, CPoint point) { if (1 == nSetText)  SetWindowText("");

     CEdit::OnLButtonDown(nFlags, point);}

    void CSTEdit::OnSetFocus(CWnd* pOldWnd) { CEdit::OnSetFocus(pOldWnd); if (1 == nSetText && IsWindowVisible() && pOldWnd && GetParent() == pOldWnd->GetParent())  SetWindowText("");}

    大家自行将其分为.h和.cpp即可,使用的时候,关联一个CSTEdit控制型变量,设置提示信息的时候,用SetWindowText等均可。


    最新回复(0)