控件的消息wm_drawitem是什么意思,如何触发这个消息

解决方案 »

  1.   

    wm_drawitem这个消息是系统发出的,只要系统需要重新绘制该控件就会发出该消息,如切换窗口,菜单下拉等。
      

  2.   

    一般情况下,按钮的绘制工作是由系统完成的,但是有时我们觉得系统绘制的效果太单调,想使自己的按钮与众不同,这时你可以选中控件的Owner draw属性。选择了这个属性后,系统便不再绘制该控件,而是当需要显示控件时,系统会发送WM_DRAWITEM消息给CButton类(对于SDK编程,消息通常是发给父窗口)。
      

  3.   

    上面这位老兄讲了原理,下面我给了一个例子,看看吧!也许很有帮助。
    class CMyClass : public CButton
    {
    // Construction
    public:
    CMyClass();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyClass)
    public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CMyClass(); // Generated message map functions
    protected:
    //{{AFX_MSG(CMyClass)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
    #include "MyClass.h"
    CMyClass::CMyClass()
    {
    }CMyClass::~CMyClass()
    {
    }
    BEGIN_MESSAGE_MAP(CMyClass, CButton)
    //{{AFX_MSG_MAP(CMyClass)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyClass message handlersvoid CMyClass::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    // TODO: Add your code to draw the specified item
    CDC dc;
    dc.Attach(lpDrawItemStruct->hDC); //Get device context object
    CRect rt;
    rt = lpDrawItemStruct->rcItem; //Get button rect dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color UINT state = lpDrawItemStruct->itemState; //Get state of the button
    if ( (state & ODS_SELECTED) ) // If it is pressed
    {
    //dc.SetBkColor(RGB(255,0,0));
    //dc.SetTextColor(RGB(0,255,0));
    dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT);    // Draw a sunken face
    }
    else
    {//BF_RECT   Entire border rectangle.   dc.DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face
    }
    dc.SetTextColor(RGB(255,255,120)); // Set the color of the caption to be yellow
    CString strTemp;
    GetWindowText(strTemp);
    // Get the caption which have been set
    //DT_CENTER   Centers text horizontally.
    dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); // Draw out the caption
    if ( (state & ODS_FOCUS ) )       // If the button is focused
    {
    // Draw a focus rect which indicates the user 
    // that the button is focused
    int iChange = 2;
    rt.top += iChange;
    rt.left += iChange;
    rt.right -= iChange;
    rt.bottom -= iChange;
    dc.DrawFocusRect(rt);
    }
    dc.Detach();
    }
      

  4.   

    指定了自绘风格的控件需要绘制时,系统不会调用标准过程进行绘制,而是向控件的父窗口发送WM_DRAWITEM消息。由父窗口的窗口过程中来为该控件完成绘制工作。
    在MFC中同过消息反射机制,WM_DRAWITEM消息被送到控件中,从而实现了自绘代码的自包含。更加符合面向对象的标准