我想用一个函数改变ListCtrl列表单行的颜色,并且可以随时调用该函数。
我是初学者,希望各位高手能说的详细点,多谢!!!
我是初学者,希望各位高手能说的详细点,多谢!!!
解决方案 »
- 请教: VMware 基于scsi硬盘和ide硬盘安装多系统问题
- 对话框的背景图片
- 向导属性页的外观
- Opengl多文档问题
- 除了自己加输出,还有别的方法得到Release版的函数调用顺序、参数及执行时间
- 请问:我用CFileDialog 打开多个文件得时候出现问题
- 如何将CString 的对象 转换成 Int 或 Double类型???
- 怎样利用FindExecutable函数返回的HINSTANCE关闭程序?
- CString类的多线程问题
- 在线程里调用put_Item函数给EXCEL写数据,会产生中断,如果put_Item放到线程外面使用就可以了,咋回事?
- 如何注册为IE插件,都需要修改那些注册表,在2000/xp下,分别说明一下,所有需要修改的注册表项,和需要做的其他工作
- 请教VC中使用资源的问题
处理NM_CUSTOMDRAW消息,但是在class wizard中可能看不到这个消息,可以自己手动添加1.
BEGIN_MESSAGE_MAP(CIHISSERVERView, CListView)
//{{AFX_MSG_MAP(CIHISSERVERView)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnShowAlarm)
ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
ON_COMMAND(IDM_SHOWALARM, OnShowalarmDlg)
ON_WM_CREATE()
ON_COMMAND(IDM_FINDALARM, OnFindalarm)
ON_COMMAND(IDM_SHOWALARMINFO, OnShowalarminfo)
ON_COMMAND(IDM_PROCESSALARMINFO, OnProcessalarminfo)
ON_COMMAND(IDM_CLEARALL, OnClearall)
ON_WM_ERASEBKGND()
ON_WM_HSCROLL()
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw) // 自己添加这行代码
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()2.
void CIHISSERVERView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// Contains information specific to an NM_CUSTOMDRAW
// notification message sent by a list-view control.
// mean:draw each item.set txt color,bkcolor....
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)(pNMHDR); // Take the default processing unless we set this to something else below.
*pResult = CDRF_NEWFONT; // First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
else if ( pLVCD->nmcd.dwDrawStage==CDDS_ITEMPREPAINT )
{
COLORREF m_crTextBk , m_clrText;
int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
CListCtrl &m_list = GetListCtrl();
CString str1 = m_list.GetItemText(nItem ,15);
bool bDBImplFail = false ;
if (str1 == "信息不祥")
{
m_crTextBk = RGB(255, 255, 0) ;
m_clrText = RGB(128, 0, 128) ;
}
else
{
m_crTextBk = RGB(150, 255, 255);
m_clrText = RGB(12,26,234);
}
pLVCD->clrTextBk = m_crTextBk;
pLVCD->clrText = m_clrText; *pResult = CDRF_DODEFAULT;
}
}3.
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIHISSERVERView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void OnInitialUpdate(); // called first time after construct
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
afx_msg void OnCustomDraw(NMHDR*, LRESULT*);
--------------------------------------------------------------------------------
The information in this article applies to: The Microsoft Foundation Classes (MFC)
Microsoft Visual C++ for Windows, 16-bit edition 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++, 32-bit Editions 1.0, 2.0, 2.1, 2.2, 4.0
Microsoft Visual C++, 32-bit Enterprise Edition 5.0, 6.0
Microsoft Visual C++, 32-bit Professional Edition 5.0, 6.0
Microsoft Visual C++, 32-bit Learning Edition 6.0
Microsoft Visual C++ .NET (2002)
Microsoft Windows XP Home Edition
Microsoft Windows XP Professional--------------------------------------------------------------------------------
Summary
To change the color scheme of a standard class of controls in an MFC-based application, follow these steps: Derive a class from the standard control class, such as CEdit.
Define a static member variable of class CBrush to be the brush for that class of controls.
Override the control's member function OnChildNotify(), handle the message WM_CTLCOLOR, and use the new brush. In MFC 4.0, this could also be done by using an ON_WM_CTLCOLOR_REFLECT handler.
NOTE: In 32-bit Windows, the controls do not send the WM_CTLCOLOR message. They send WM_CTLCOLORxxx messages, where xxx is the type of control. For example, static control sends the WM_CTLCOLORSTATIC message. More Information
This approach works for list boxes, the list boxes of combo boxes, button controls, edit controls, static controls, message boxes, and dialog boxes. (This approach does not work for push buttons and the CRichEditCtrl. The color of a standard CButton object is determined by system settings. If you want a different color for push buttons, use a CBitmapButton. To change the color of a CRichEditCtrl use its member functions.) For an alternative approach, please see the following article in the Microsoft Knowledge Base: Q117778 Changing the Background Color of an MFC Edit Control
It illustrates how to change the background color of a control in the parent window. When a control is about to be redrawn, it sends the message WM_CTLCOLOR to its parent. This message is handled by the OnCtlColor() member function of the parent. OnCtlColor() allows the parent to modify the drawing of the child by:
Specifying the background brush.
Changing the text color.
Making other changes to the device context with which the drawing is to be done.
One of the first things the default implementation of OnCtlColor() does is to call the OnChildNotify() member function of the child that sent the message. By overriding this OnChildNotify() member function, the child can determine its own color scheme, instead of taking it from the parent. The following sample code defines a class of edit controls with red text on a green background. The sample code shows only what is necessary to change the color of the controls. It does not include code generated by the ClassWizard.
Sample Code
/* Compile options needed: Default
*/ // NOTE: The sample code is for 32-bit. It has to be modified for
// 16-bit. See the comment below. // ** MYEDIT.H ** class CMyEdit : public CEdit
{
public:
BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM Param,
LRESULT* pLResult);
protected:
static CBrush m_brush;
}; // ** MYEDIT.CPP ** #include "myedit.h" // Create a green brush for the background for the class of controls:
CBrush CMyEdit::m_brush(RGB(0,128,0)); BOOL CMyEdit::OnChildNotify(UINT message, WPARAM wParam,
LPARAM lParam, LRESULT* pLResult)
{
// If "message" is not the message you're after, do default processing: // For 16-bit applications change WM_CTLCOLOREDIT to WM_CTLCOLOR
if (message != WM_CTLCOLOREDIT)
{
return CEdit::OnChildNotify(message,wParam,lParam,pLResult);
} // Set the text foreground to red:
HDC hdcChild = (HDC)wParam;
SetTextColor(hdcChild, RGB(0,0,255)); // Set the text background to green:
SetBkColor(hdcChild, RGB(0,128,0)); // Send what would have been the return value of OnCtlColor() - the
// brush handle - back in pLResult:
*pLResult = (LRESULT)(m_brush.GetSafeHandle()); // Return TRUE to indicate that the message was handled:
return TRUE;
} Additional query words: 2.50 2.51 2.52 3.00 3.10 3.20 Keywords: kbCtrl kbGDI kbMFC KbUIDesign kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600 kbGrpDSMFCATL
Issue Type: kbhowto
Technology: kbWinXPHome kbWinXPPro kbAudDeveloper kbMFC kbWinXPProSearch kbWinXPHomeSearch kbWinXPSearch kbVCNET --------------------------------------------------------------------------------Send feedback to Microsoft© 2002-2003 Microsoft Corporation. All rights reserved.
如果能,请详细说说,拜托啦.
CSDN里面收一下。。多得要死~~:)