Win32_控件的自绘制

 控件的自绘制

  1 控件具有 OWNERDRAW样式
    BUTTON  LISTBOX  COMBOBOX
  2 WM_MEASUREITEM
    计算控件的大小
    (UINT) wParam - 控件ID
    (LPMEASUREITEMSTRUCT)lParam - 控件项的大小
  3 WM_DRAWITEM
    实现控件的绘制
    (UINT) wParam - 控件ID
    (LPDRAWITEMSTRUCT) - lParam - 控件绘制的信息

View Code
  1 // WinDlg.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 
  6 HINSTANCE g_hInst = NULL;
  7 
  8 void OnCreate(HWND hWnd,WPARAM wParam,LPARAM lParam)
  9 {
 10     CreateWindow("BUTTON","MyButton",WS_CHILD | WS_VISIBLE |
 11         BS_OWNERDRAW,50,50,200,30,hWnd,(HMENU)1001,g_hInst,NULL);
 12 }
 13 void OnDrawItem( HWND hWnd, WPARAM wParam, 
 14                 LPARAM lParam )
 15 {
 16     LPDRAWITEMSTRUCT pDis = 
 17         ( LPDRAWITEMSTRUCT )lParam;
 18     if( ODT_BUTTON == pDis->CtlType )
 19     {    //绘制按钮
 20         if( pDis->itemState & ODS_SELECTED )
 21         {
 22             HBRUSH hBrush = CreateSolidBrush( 
 23                 RGB(200, 200, 255 ) );
 24             HBRUSH hOldBrush = (HBRUSH)
 25                 SelectObject( pDis->hDC, hBrush );
 26             RoundRect( pDis->hDC, pDis->rcItem.left,
 27                 pDis->rcItem.top, pDis->rcItem.right,
 28                 pDis->rcItem.bottom, 15, 15 );
 29             SelectObject( pDis->hDC, hOldBrush );
 30             DeleteObject( hBrush );
 31         }
 32         else
 33         {
 34             HBRUSH hBrush = CreateSolidBrush( 
 35                 RGB(100, 100, 255 ) );
 36             HBRUSH hOldBrush = (HBRUSH)
 37                 SelectObject( pDis->hDC, hBrush );
 38             RoundRect( pDis->hDC, pDis->rcItem.left,
 39                 pDis->rcItem.top, pDis->rcItem.right,
 40                 pDis->rcItem.bottom, 15, 15 );
 41             SelectObject( pDis->hDC, hOldBrush );
 42             DeleteObject( hBrush );
 43         }
 44         //绘制按钮文字
 45         CHAR szName[260] = { 0 };
 46         GetWindowText( pDis->hwndItem, 
 47             szName, 260 );
 48         int nOldMode = SetBkMode( pDis->hDC, 
 49             TRANSPARENT );
 50         DrawText( pDis->hDC, szName, strlen(szName),
 51             &pDis->rcItem, 
 52             DT_CENTER|DT_VCENTER|DT_SINGLELINE );
 53         SetBkMode( pDis->hDC, nOldMode );
 54     }
 55 }
 56 
 57 
 58 LRESULT CALLBACK WndProc(HWND hWnd,
 59                          UINT nMsg,
 60                          WPARAM wParam,
 61                          LPARAM lParam)
 62 {
 63     switch(nMsg)
 64     {
 65     case WM_CREATE:
 66         OnCreate(hWnd,wParam,lParam);
 67         break;
 68     case WM_DRAWITEM:
 69         OnDrawItem(hWnd,wParam,lParam);
 70         break;
 71     case WM_DESTROY:
 72         PostQuitMessage(0);
 73         return 0;
 74     }
 75     return DefWindowProc(hWnd,nMsg,wParam,lParam);
 76 }
 77 
 78 BOOL RegisterWnd(LPSTR pszClassName)
 79 {
 80     WNDCLASSEX wce = {0};
 81     wce.style    = CS_HREDRAW | CS_VREDRAW;
 82     wce.cbSize    = sizeof(wce);
 83     wce.cbClsExtra = 0;
 84     wce.cbWndExtra = 0;
 85     wce.hbrBackground = HBRUSH(COLOR_WINDOW);
 86     wce.hCursor    = NULL;
 87     wce.hIcon    = NULL;
 88     wce.hIconSm    = NULL;
 89     wce.hInstance    = g_hInst;
 90     wce.lpfnWndProc = WndProc;
 91     wce.lpszClassName = pszClassName;
 92     wce.lpszMenuName    = NULL;
 93     
 94     ATOM nAtom = RegisterClassEx(&wce);
 95     if (0 == nAtom)
 96     {
 97         return FALSE;
 98     }
 99     return TRUE;
100 }
101 
102 HWND CreateWnd(LPSTR pszClassName)
103 {
104     HWND hWnd = CreateWindowEx(0,pszClassName,"MyWnd",
105         WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
106         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInst,NULL);
107     return hWnd;
108 }
109 
110 void DisplayWnd(HWND hWnd)
111 {
112     ShowWindow(hWnd,SW_SHOW);
113     UpdateWindow(hWnd);
114 }
115 
116 void Message()
117 {
118     MSG msg = {0};
119     while(GetMessage(&msg,NULL,0,0))
120     {
121         TranslateMessage(&msg);
122         DispatchMessage(&msg);
123     }
124 }
125 
126 int APIENTRY WinMain(HINSTANCE hInstance,
127                      HINSTANCE hPrevInstance,
128                      LPSTR     lpCmdLine,
129                      int       nCmdShow)
130 {
131     // TODO: Place code here.
132     g_hInst = hInstance;
133     RegisterWnd("MYWND");
134     HWND hWnd = CreateWnd("MYWND");
135     DisplayWnd(hWnd);
136     Message();
137     
138     return 0;
139 }

 

转载于:https://www.cnblogs.com/wjay/archive/2012/10/13/2722551.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值