纯api实现控件的自绘
在网上查了许多资料有的说响应WM_DRAWITEM消息,但在窗体的wndproc里面收不到WM_DRAWITEM消息。还需要做什么工作?谁能个写个比如buttoon 的自绘demo谢谢了。。,

解决方案 »

  1.   

    http://www.vckbase.com/document/finddoc.asp?keyword=buttonst
      

  2.   

    他用的是SDK
    应该是动态创建的吧
    创建的时候属性里加上OwnDraw属性
    另外用MFC自绘时类似WM_DRAWITEM属性是控件本身响应的
    不是主窗口
      

  3.   


    combobox 风格里面有个CBS_OWNERDRAWVARIABLE风格,我添上了。是在主窗体的消息响应函数中响应不到WM_DRAWITEM消息,如果不是主窗体响应,那在哪里响应? 
      

  4.   

    SDK我不熟
    用MFC是在自己继承的CCombox类响应的
      

  5.   

    http://blog.csdn.net/geniusdot/archive/2007/03/03/1520058.aspx
    参考这个
      

  6.   

    1.创建控件时,添加自绘属性
    2.在SDK中 WM_DRAWITEM是控件的父窗口响应的
    3.响应WM_DRAWITEM,根据DRAWITEMSTRUCT里的参数进行绘制MSDN中有描述:DRAWITEMSTRUCT Structure--------------------------------------------------------------------------------Provides necessary information the owner window to determine how to paint an owner-drawn control or menu item. The owner window of the owner-drawn control or menu item receives a pointer to this structure as the lParam parameter of the WM_DRAWITEM message.Syntaxtypedef struct tagDRAWITEMSTRUCT {
        UINT CtlType;
        UINT CtlID;
        UINT itemID;
        UINT itemAction;
        UINT itemState;
        HWND hwndItem;
        HDC hDC;
        RECT rcItem;
        ULONG_PTR itemData;
    } DRAWITEMSTRUCT;
    MembersCtlType
    The control type. This member can be one of the following values. See Res.
    ODT_BUTTON
    Owner-drawn button
    ODT_COMBOBOX
    Owner-drawn combo box
    ODT_LISTBOX
    Owner-drawn list box
    ODT_LISTVIEW
    List-view control
    ODT_MENU
    Owner-drawn menu item
    ODT_STATIC
    Owner-drawn static control
    ODT_TAB
    Tab control
    CtlID
    The identifier of the combo box, list box, button, or static control. This member is not used for a menu item.
    itemID
    The menu item identifier for a menu item or the index of the item in a list box or combo box. For an empty list box or combo box, this member can be -1. This allows the application to draw only the focus rectangle at the coordinates specified by the rcItem member even though there are no items in the control. This indicates to the user whether the list box or combo box has the focus. How the bits are set in the itemAction member determines whether the rectangle is to be drawn as though the list box or combo box has the focus. 
    itemAction
    The required drawing action. This member can be one or more of the values. 
    ODA_DRAWENTIRE
    The entire control needs to be drawn.
    ODA_FOCUS
    The control has lost or gained the keyboard focus. The itemState member should be checked to determine whether the control has the focus.
    ODA_SELECT
    The selection status has changed. The itemState member should be checked to determine the new selection state.
    itemState
    The visual state of the item after the current drawing action takes place. This member can be a combination of the values shown in the following table. 
    ODS_CHECKED
    The menu item is to be checked. This bit is used only in a menu.
    ODS_COMBOBOXEDIT
    The drawing takes place in the selection field (edit control) of an owner-drawn combo box.
    ODS_DEFAULT
    The item is the default item.
    ODS_DISABLED
    The item is to be drawn as disabled.
    ODS_FOCUS
    The item has the keyboard focus.
    ODS_GRAYED
    The item is to be grayed. This bit is used only in a menu.
    ODS_HOTLIGHT
    Windows 98/Me, Windows 2000/XP: The item is being hot-tracked, that is, the item will be highlighted when the mouse is on the item.
    ODS_INACTIVE
    Windows 98/Me, Windows 2000/XP: The item is inactive and the window associated with the menu is inactive.
    ODS_NOACCEL
    Windows 2000/XP: The control is drawn without the keyboard accelerator cues.
    ODS_NOFOCUSRECT
    Windows 2000/XP: The control is drawn without focus indicator cues.
    ODS_SELECTED
    The menu item's status is selected.
    hwndItem
    A handle to the control for combo boxes, list boxes, buttons, and static controls. For menus, this member is a handle to the menu that contains the item. 
    hDC
    A handle to a device context; this device context must be used when performing drawing operations on the control. 
    rcItem
    A rectangle that defines the boundaries of the control to be drawn. This rectangle is in the device context specified by the hDC member. The system automatically clips anything that the owner window draws in the device context for combo boxes, list boxes, and buttons, but does not clip menu items. When drawing menu items, the owner window must not draw outside the boundaries of the rectangle defined by the rcItem member. 
    itemData
    The application-defined value associated with the menu item. For a control, this parameter specifies the value last assigned to the list box or combo box by the LB_SETITEMDATA or CB_SETITEMDATA message. If the list box or combo box has the LBS_HASSTRINGS or CBS_HASSTRINGS style, this value is initially zero. Otherwise, this value is initially the value that was passed to the list box or combo box in the lParam parameter of one of the following messages: 
    CB_ADDSTRING
    CB_INSERTSTRING
    LB_ADDSTRING
    LB_INSERTSTRING
    If CtlType is ODT_BUTTON or ODT_STATIC, itemData is zero. 
    ResSome control types, such as status bars, do not set the value of CtlType. 
      

  7.   

    接上面的:
    WM_DRAWITEM Notification--------------------------------------------------------------------------------
    The WM_DRAWITEM message is sent to the parent window of an owner-drawn button, combo box, list box, or menu when a visual aspect of the button, combo box, list box, or menu has changed.A window receives this message through its WindowProc function.
    Syntax
    WM_DRAWITEM    WPARAM wParam
        lpDrawItem = (LPDRAWITEMSTRUCT) lParam;
        
    ParameterswParam
    Specifies the identifier of the control that sent the WM_DRAWITEM message. If the message was sent by a menu, this parameter is zero. 
    lpDrawItem
    Pointer to a DRAWITEMSTRUCT structure containing information about the item to be drawn and the type of drawing required. 
    Return ValueIf an application processes this message, it should return TRUE. ResBy default, the DefWindowProc function draws the focus rectangle for an owner-drawn list box item. The itemAction member of the DRAWITEMSTRUCT structure specifies the drawing operation that an application should perform. Before returning from processing this message, an application should ensure that the device context identified by the hDC member of the DRAWITEMSTRUCT structure is in the default state. 
      

  8.   

    重写下面的CButton的虚函数:
     
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
      

  9.   

    在空间属性要改一下:OwnDraw属性TRUE