欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

给MFC编辑框加灰色文字提示/鼠标滑到编辑框或按钮提示

程序员文章站 2022-06-23 21:24:48
①鼠标滑到编辑框或按钮提示在OnInitDialog()添加以下代码CToolTipCtrl m_ToolTip;m_ToolTip.Create(this);m_ToolTip.AddTool(GetDlgItem(IDC_EDIT9), _T("选择到sub01、sub02、sub....目录即可"));m_ToolTip.AddTool(GetDlgItem(IDC_RootDir), _T("选择到sub01、sub02、sub....目录即可"));重写PreTranslateM...

①鼠标滑到编辑框或按钮提示
在OnInitDialog()添加以下代码

CToolTipCtrl   m_ToolTip;
m_ToolTip.Create(this);
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT9), _T("选择到sub01、sub02、sub....目录即可"));
m_ToolTip.AddTool(GetDlgItem(IDC_RootDir), _T("选择到sub01、sub02、sub....目录即可"));

重写PreTranslateMessage

BOOL CVideoCutDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_NCLBUTTONDBLCLK) { return TRUE; }       
	ASSERT(pMsg != NULL);
	if (pMsg->message == WM_MOUSEMOVE ||
		pMsg->message == WM_LBUTTONDOWN ||
		pMsg->message == WM_LBUTTONUP)
	{
		m_ToolTip.RelayEvent(pMsg);
	}
	
	return CDialog::PreTranslateMessage(pMsg);
	//return CDialogEx::PreTranslateMessage(pMsg);
}

②给MFC编辑框加灰色文字提示
原始的编辑框CEdit类没有这样的功能,我们可以通过继承来改造得到:
给MFC编辑框加灰色文字提示/鼠标滑到编辑框或按钮提示给MFC编辑框加灰色文字提示/鼠标滑到编辑框或按钮提示DimEditCtrl.h

#pragma once
/*|*\
* | File:      DimEditCtrl.h
| *|
|*| By : James R.Twine, TransactionWorks, Inc.
| *| Copyright 2000, TransactionWorks, inc.
| *| Date : Thursday, September 21, 2000
| *|
|*| Notes : This Is The Implementation Of A "Dim Edit Control".
| *| It Provides Visual Instructions Within The Edit
| *| Control Itself.It Can Be Used To Indicate Special
| *| Properties Of A Edit Control Used On A Crowded
| *| Interface
| *|
|*| May Be Freely Incorporated Into Projects Of Any Type
| *| Subject To The Following Conditions :
| *|
|*| o This Header Must Remain In This File, And Any
| *| Files Derived From It
| *| o Do Not Misrepresent The Origin Of This Code
| *| (IOW, Do Not Claim You Wrote It)
| *|
|*| A "Mention In The Credits", Or Similar Acknowledgement,
| *| Is * NOT* Required.It Would Be Nice, Though!:)
\* | */
#if !defined(AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_)
#define AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_

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


//
//  This Specifies The Length Of The Dim Text Buffer...
//
static  const   int     DIM_TEXT_LEN = 128;                // Dim Text Buffer Length


/////////////////////////////////////////////////////////////////////////////
// CDimEditCtrl window

class CDimEditCtrl : public CEdit
{
	// Construction
public:
	/**/
	CDimEditCtrl();                                 // Constructor

	// Attributes
public:

	// Operations
public:

	void    SetShowDimControl(bool bShow);                // Show Or Hide The Dim Control
	void    SetDimText(LPCTSTR cpText);                   // Set The Dim Text
	void    SetDimColor(COLORREF crDColor);               // Set The Dim Color
	void    SetDimOffset(char cRedOS, char cGreenOS,
		char cBlueOS);                           // Set The Dim Color Offset

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDimEditCtrl)
public:
	virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID, CCreateContext *pContext = NULL);
protected:
	virtual void PreSubclassWindow();
	//}}AFX_VIRTUAL

	// Implementation
public:
	virtual ~CDimEditCtrl();

	// Generated message map functions
protected:
	//{{AFX_MSG(CDimEditCtrl)
	afx_msg void OnChange();
	afx_msg void OnSetfocus();
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC *pDC);
	afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
	afx_msg void OnKillfocus();
	//}}AFX_MSG
	//  afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	//  afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);

	DECLARE_MESSAGE_MAP()

	void        DrawDimText(void);                        // Draw The Dim Text

	COLORREF    m_crDimTextColor;                           // "Hard" Dim Text Color
	TCHAR       m_caDimText[DIM_TEXT_LEN + 1];            // Dim Text Buffer
	bool        m_bShowDimText;                             // Are We Showing The Dim Text?
	bool        m_bUseDimOffset;                            // Are We Using The Offset Colors (Not Hard Color)?
	char        m_cRedOS;                                   // Red Color Dim Offset
	char        m_cGreenOS;                                 // Green Color Dim Offset
	char        m_cBlueOS;                                  // Blue Color Dim Offset
	int         m_iDimTextLen;                              // Length Of Dim Text
};

/////////////////////////////////////////////////////////////////////////////

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

#endif // !defined(AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_)

DimEditCtrl.cpp

#include "stdafx.h"
#include "DimEditCtrl.h"


/*|*\
|*|  File:      DimEditCtrl.cpp
|*|
|*|  By:        James R. Twine, TransactionWorks, Inc.
|*|             Copyright 2000, TransactionWorks, inc.
|*|  Date:      Thursday, September 21, 2000
|*|
|*|  Notes:     This Is The Implementation Of A "Dim Edit Control".
|*|             It Provides Visual Instructions Within The Edit
|*|             Control Itself.  It Can Be Used To Indicate Special
|*|             Properties Of A Edit Control Used On A Crowded
|*|             Interface
|*|
|*|             May Be Freely Incorporated Into Projects Of Any Type
|*|             Subject To The Following Conditions:
|*|
|*|             o This Header Must Remain In This File, And Any
|*|               Files Derived From It
|*|             o Do Not Misrepresent The Origin Of This Code
|*|               (IOW, Do Not Claim You Wrote It)
|*|
|*|             A "Mention In The Credits", Or Similar Acknowledgement,
|*|             Is *NOT* Required.  It Would Be Nice, Though! :)
\*|*/
#include "stdafx.h"
//#include "DimEdit.h"
//#include "DimEditCtrl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDimEditCtrl

CDimEditCtrl::CDimEditCtrl() :
	m_bShowDimText(true),                                 // Set The Dim Flag
	//  m_cRedOS( -0x40 ),                                      // Set The Default Dim Offset Colors
	//  m_cGreenOS( -0x40 ),                                    // Set The Default Dim Offset Colors
	//  m_cBlueOS( -0x40 ),                                     // Set The Default Dim Offset Colors
	//  m_bUseDimOffset( true ),                                // Use The Offset Colors
	m_iDimTextLen(0),                                     // No Dim Text Set Yet
	m_crDimTextColor(RGB(0x00, 0x00, 0x00))             // No "Hard" Dim Text Color
{
	m_caDimText[0] = _T('\0');                          // Terminate The Buffer
	SetDimOffset(-0x40, -0x40, -0x40);                    // Set The Dim Offset

	return;                                                 // Done!
}


CDimEditCtrl::~CDimEditCtrl()
{
	return;                                                 // Done!
}


BEGIN_MESSAGE_MAP(CDimEditCtrl, CEdit)
	//{{AFX_MSG_MAP(CDimEditCtrl)
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_SETTINGCHANGE()
	ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
	//}}AFX_MSG_MAP
	//  ON_WM_LBUTTONDOWN()
	//  ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDimEditCtrl message handlers

void    CDimEditCtrl::PreSubclassWindow()
{
	CEdit::PreSubclassWindow();                             // Do Default...

	SetShowDimControl(true);                              // Default To Show The Dim Control

	return;                                                 // Done!
}


void    CDimEditCtrl::SetDimText(LPCTSTR cpDimText)
{
	if (cpDimText)                                         // If Dim Text Specified
	{
		_tcsncpy(m_caDimText, cpDimText, DIM_TEXT_LEN);   // Copy Over The Text
		m_caDimText[DIM_TEXT_LEN] = _T('\0');           // Enforce Termination (I Am Paranoid, I Know!)
		m_iDimTextLen = _tcslen(m_caDimText);             // Store Length Of The Dim Text
	}
	else                                                    // If No Dim Text
	{
		m_caDimText[0] = _T('\0');                      // Just Terminate The Buffer (No Text)
		m_iDimTextLen = 0;                                  // No Dim Text
	}
	if (m_bShowDimText)                                    // If Showing Any Dim Text
	{
		DrawDimText();                                      // Draw The Dim Text
	}
	return;                                                 // Done!
}


void CDimEditCtrl::SetShowDimControl(bool bShow)
{
	m_bShowDimText = bShow;                                 // Set The Dim Flag
	if (bShow)                                             // If Showing Any Dim Text
	{
		DrawDimText();                                      // Draw The Dim Text
	}
	return;                                                 // Done!
}


BOOL    CDimEditCtrl::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
	DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID,
	CCreateContext *pContext)
{
	BOOL    bCreated = CWnd::Create(lpszClassName,
		lpszWindowName, dwStyle, rect,
		pParentWnd, nID, pContext);           // Try To Create Ourselves...

	if (bCreated)                                          // If We Got Created
	{
		SetShowDimControl(true);                          // Show The Dim Control
	}
	return(bCreated);                                     // Return Creation Status
}


void CDimEditCtrl::OnChange()
{
	int iLen = GetWindowTextLength();                   // Get Control's Text Length

	if (!iLen)                                             // If No Text
	{
		SetShowDimControl(true);                          // Show The Dim Text
	}
	else                                                    // If Text Now In The Control
	{
		SetShowDimControl(false);                         // Disable The Dim Text
	}
	return;                                                 // Done!
}


void CDimEditCtrl::OnSetfocus()
{
	if (m_bShowDimText)                                    // If Showing Any Dim Text
	{
		DrawDimText();                                      // Draw The Dim Text
	}
	return;                                                 // Done!
}


void    CDimEditCtrl::OnPaint()
{
	Default();                                              // Do Default Control Drawing

	if (m_bShowDimText)                                    // If Showing Any Dim Text
	{
		DrawDimText();                                      // Draw The Dim Text
	}
	return;                                                 // Done!
}


void CDimEditCtrl::DrawDimText(void)
{
	if (!m_iDimTextLen)                                    // If No Dim Text
	{
		return;                                             // Stop Here
	}
	CClientDC   dcDraw(this);
	CRect       rRect;
	int         iState = dcDraw.SaveDC();                   // Save The DC State

	GetClientRect(&rRect);                                // Get Drawing Area
	rRect.OffsetRect(1, 1);                               // Add Sanity Space

	dcDraw.SelectObject((*GetFont()));                    // Use The Control's Current Font
	dcDraw.SetTextColor(m_crDimTextColor);                // Set The Text Color
	dcDraw.SetBkColor(GetSysColor(COLOR_WINDOW));       // Set The Bk Color
	dcDraw.DrawText(m_caDimText, m_iDimTextLen, &rRect,
		(DT_CENTER | DT_VCENTER));                  // Draw The Dim Text

	dcDraw.RestoreDC(iState);                             // Restore The DC State

	return;                                                 // Done!
}


BOOL CDimEditCtrl::OnEraseBkgnd(CDC *pDC)
{
	BOOL    bStatus = CEdit::OnEraseBkgnd(pDC);

	if ((bStatus) && (m_bShowDimText))                 // If All Good, And Showing Any Dim Text
	{
		DrawDimText();                                      // Draw The Dim Text
	}
	return(bStatus);                                      // Return Erase Status
}

/*
void CDimEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
	TRACE( _T( "Click...\n" ) );
	CEdit::OnLButtonDown(nFlags, point);
}

void CDimEditCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	TRACE( _T( "DClick...\n" ) );
	CEdit::OnLButtonDblClk(nFlags, point);
}
*/

void CDimEditCtrl::SetDimOffset(char cRedOS, char cGreenOS, char cBlueOS)
{
	COLORREF    crWindow = GetSysColor(COLOR_WINDOW);
	BYTE        btRedOS = (GetRValue(crWindow) + cRedOS);
	BYTE        btGreenOS = (GetGValue(crWindow) + cGreenOS);
	BYTE        btBlueOS = (GetBValue(crWindow) + cBlueOS);

	m_bUseDimOffset = true;                                 // Set The Flag
	m_cRedOS = cRedOS;                                      // Store Red Offset
	m_cGreenOS = cGreenOS;                                  // Store Green Offset
	m_cBlueOS = cBlueOS;                                    // Store Blue Offset
	m_crDimTextColor = RGB((BYTE)btRedOS, (BYTE)btGreenOS,
		(BYTE)btBlueOS);                               // Build The New Dim Color

	return;                                                 // Done!
}


void CDimEditCtrl::SetDimColor(COLORREF crColor)
{
	m_bUseDimOffset = false;                                // Unset The Flag
	m_crDimTextColor = crColor;                             // Set The New Dim Color
	m_cRedOS = m_cGreenOS = m_cBlueOS = 0;                  // No Offset

	return;                                                 // Done!
}


void CDimEditCtrl::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	CEdit::OnSettingChange(uFlags, lpszSection);

	if (m_bUseDimOffset)                                   // If Using An Offset For The Dim Color
	{
		COLORREF    crWindow = GetSysColor(COLOR_WINDOW);

		m_crDimTextColor = RGB(GetRValue(crWindow) +
			m_cRedOS, GetGValue(crWindow) +
			m_cGreenOS, GetBValue(crWindow) +
			m_cBlueOS);                                // Rebuild The Dim Color
	}
	return;                                                 // Done!
}

void CDimEditCtrl::OnKillfocus()
{
	int iLen = GetWindowTextLength();                   // Get Control's Text Length

	if (!iLen)                                             // If No Text
	{
		SetShowDimControl(true);                          // Show The Dim Text
	}
	else                                                    // If Text Now In The Control
	{
		SetShowDimControl(false);                         // Disable The Dim Text
	}
	return;

}

创建类之后,在我们的主函数xxxxDlg.cpp中包含新建类的头文件

#include "DimEditCtrl.h"

如果报错:未知重写说明符
则在xxxxDlg.h中

#include "DimEditCtrl.h"

然后在OnInitDialog()中加入以下代码

m_grayInform.SetDimText(_T("选择到sub01、sub02、sub....目录即可"));

结果如下图所示
给MFC编辑框加灰色文字提示/鼠标滑到编辑框或按钮提示
DimEditCtrl类地址
链接:https://pan.baidu.com/s/1AdrAtauXOYwfvB_ymZnT9g
提取码:30ct

本文地址:https://blog.csdn.net/ningmengshuxiawo/article/details/107282307