如何编写一个可显示不同颜色字符的编辑框(如vc++编辑器中出现保留字显示绿色) CEdit可以做到,不过你必需建一个基于CEdit 的类并重载其OnDraw和OnChar函数使用CDC自己控制显示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以下是MSDN的帮助:HOWTO: Change the Background Color of an MFC Edit ControlLast reviewed: March 2, 1998Article ID: Q117778 The information in this article applies to: The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 4.1, 4.2, 5.0SUMMARYTo change the background color of an edit control in an MFC application, you must override the OnCtlColor() message-handling function of the window containing the edit control. In the new OnCtlColor() function, set the background color and return a handle to a brush that will be used for painting the background. This must be done in response to receiving both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX messages in the OnCtlColor() function. This is also documented in the "Class Library Reference" under CWnd::OnCtlColor(). MORE INFORMATIONThe sample code below uses a CDialog-derived class to demonstrate the process. Class Wizard was used to generate message-handling functions for the WM_CTLCOLOR and WM_DESTROY messages. These functions are called CEditDialog::OnCtlColor() and CEditDialog::OnDestroy(), respectively. Sample Code // editdlg.h : header file ////////////////////////////////////////////////////////////////////////// /// // CEditDialog dialog class CEditDialog : public CDialog { // Construction public: CEditDialog(CWnd* pParent = NULL); // standard constructor // Add a CBrush* to store the new background brush for edit controls. CBrush* m_pEditBkBrush; // Dialog Data //{{AFX_DATA(CEditDialog) enum { IDD = IDD_EDITDIALOG }; // NOTE: The ClassWizard will add data members here. //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CEditDialog) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CEditDialog) afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; // editdlg.cpp : implementation file // #include "stdafx.h" #include "mdi.h" #include "editdlg.h" #ifdef _DEBUG #undef THIS_FILE static char BASED_CODE THIS_FILE[] = __FILE__; #endif ////////////////////////////////////////////////////////////////////// // CEditDialog dialog CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/) : CDialog(CEditDialog::IDD, pParent) { //{{AFX_DATA_INIT(CEditDialog) // NOTE: The ClassWizard will add member initialization here. //}}AFX_DATA_INIT // Instantiate and initialize the background brush to black. m_pEditBkBrush = new CBrush(RGB(0, 0, 0)); } void CEditDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CEditDialog) // NOTE: The ClassWizard will add DDX and DDV calls here. //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CEditDialog, CDialog) //{{AFX_MSG_MAP(CEditDialog) ON_WM_CTLCOLOR() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP()////////////////////////////////////////////////////////////////////// // CEditDialog message handlers HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch (nCtlColor) { case CTLCOLOR_EDIT: case CTLCOLOR_MSGBOX: // Set color to green on black and return the background brush. pDC->SetTextColor(RGB(0, 255, 0)); pDC->SetBkColor(RGB(0, 0, 0)); return (HBRUSH)(m_pEditBkBrush->GetSafeHandle()); default: return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } } void CEditDialog::OnDestroy() { CDialog::OnDestroy(); // Free the space allocated for the background brush delete m_pEditBkBrush; } --------------------------------------------------------------------------------Additional query words: CEditKeywords : MfcUI kbprgTechnology : kbMfcVersion : 1.0 1.5 1.51 1.5 2.0 4.1 4.2 5.0Platform : WINDOWSIssue type : kbhowtoTHE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Last reviewed: March 2, 1998 © 1998 Microsoft Corporation. All rights reserved. Terms of Use. 何必定要用CEdit,用CRichEdit不是很方便? 大家都进来看看怎么回事 vc中想实现游标 有什么方法? 请问,怎么创建和Formail样式的工具栏,我试了自绘,要处理的太多了 请教大家下面的代码为什么不能通过ADO连接数据库? 为什么在VC6.0代码中按F1没法调用MSDN6.0 kao!google是否又被“封”了,有人回答一下吗 时间转换问题 如何在DAO中排序和过滤?急急! directshow 怎么把字幕合到播放视频里 初学者请问:CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)是何意思 ? 一个关于CString.Format的问题 问题1
HOWTO: Change the Background Color of an MFC Edit Control
Last reviewed: March 2, 1998
Article ID: Q117778
The information in this article applies to:
The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
- Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0,
4.1, 4.2, 5.0
SUMMARY
To change the background color of an edit control in an MFC application, you must override the OnCtlColor() message-handling function of the window containing the edit control. In the new OnCtlColor() function, set the background color and return a handle to a brush that will be used for painting the background. This must be done in response to receiving both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX messages in the OnCtlColor() function. This is also documented in the "Class Library Reference" under CWnd::OnCtlColor(). MORE INFORMATION
The sample code below uses a CDialog-derived class to demonstrate the process. Class Wizard was used to generate message-handling functions for the WM_CTLCOLOR and WM_DESTROY messages. These functions are called CEditDialog::OnCtlColor() and CEditDialog::OnDestroy(), respectively. Sample Code // editdlg.h : header file
//////////////////////////////////////////////////////////////////////////
///
// CEditDialog dialog class CEditDialog : public CDialog
{
// Construction
public:
CEditDialog(CWnd* pParent = NULL); // standard constructor // Add a CBrush* to store the new background brush for edit controls.
CBrush* m_pEditBkBrush; // Dialog Data
//{{AFX_DATA(CEditDialog)
enum { IDD = IDD_EDITDIALOG };
// NOTE: The ClassWizard will add data members here.
//}}AFX_DATA // Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL // Implementation
protected: // Generated message map functions
//{{AFX_MSG(CEditDialog)
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}; // editdlg.cpp : implementation file
// #include "stdafx.h"
#include "mdi.h"
#include "editdlg.h" #ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif //////////////////////////////////////////////////////////////////////
// CEditDialog dialog CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
: CDialog(CEditDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CEditDialog)
// NOTE: The ClassWizard will add member initialization here.
//}}AFX_DATA_INIT // Instantiate and initialize the background brush to black.
m_pEditBkBrush = new CBrush(RGB(0, 0, 0));
} void CEditDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEditDialog)
// NOTE: The ClassWizard will add DDX and DDV calls here.
//}}AFX_DATA_MAP
} BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
//{{AFX_MSG_MAP(CEditDialog)
ON_WM_CTLCOLOR()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()//////////////////////////////////////////////////////////////////////
// CEditDialog message handlers HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
switch (nCtlColor) { case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
// Set color to green on black and return the background
brush.
pDC->SetTextColor(RGB(0, 255, 0));
pDC->SetBkColor(RGB(0, 0, 0));
return (HBRUSH)(m_pEditBkBrush->GetSafeHandle()); default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
} void CEditDialog::OnDestroy()
{
CDialog::OnDestroy(); // Free the space allocated for the background brush
delete m_pEditBkBrush;
}
--------------------------------------------------------------------------------Additional query words: CEdit
Keywords : MfcUI kbprg
Technology : kbMfc
Version : 1.0 1.5 1.51 1.5 2.0 4.1 4.2 5.0
Platform : WINDOWS
Issue type : kbhowto
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Last reviewed: March 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.