19windows_19_OwnerDraw自制按钮DIYBUTTON

19windows_19_OwnerDraw自制按钮DIYBUTTON
  1. #include <windows.h>
  2. #include <iostream>
  3. CHAR szText[256] = { 0 };
  4. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  5. HINSTANCE g_hInst = NULL; //窗口句柄
  6. HANDLE g_hStdout = NULL; //控制台句柄
  7. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  8. {
  9. CreateWindow("BUTTON", "DIY按钮",
  10. WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
  11. 50, 50, 200, 50, hWnd, (HMENU)1001, g_hInst, NULL);
  12. CreateWindow("BUTTON", "普通按钮", WS_VISIBLE | WS_CHILD,
  13. 400, 50, 200, 50, hWnd, NULL, g_hInst, NULL);
  14. }
  15. void OnDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam)
  16. {
  17. LPDRAWITEMSTRUCT pDis = (LPDRAWITEMSTRUCT)lParam;
  18. if (ODT_BUTTON == pDis->CtlType)
  19. {
  20. if (pDis->itemState & ODS_SELECTED)
  21. {
  22. //使用画刷
  23. //1、创建 2、交换 3、销毁
  24. HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 255));
  25. HBRUSH hOldBrush = (HBRUSH )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(hOldBrush);
  31. }
  32. else
  33. {
  34. HBRUSH hBrush = CreateSolidBrush(RGB(100, 100, 255));
  35. HBRUSH hOldBrush = (HBRUSH)SelectObject(pDis->hDC, hBrush);
  36. RoundRect(pDis->hDC, pDis->rcItem.left,
  37. pDis->rcItem.top, pDis->rcItem.right,
  38. pDis->rcItem.bottom, 15, 15);
  39. SelectObject(pDis->hDC, hOldBrush);
  40. DeleteObject(hOldBrush);
  41. }
  42. //绘制按扭
  43. /*Rectangle(pDis->hDC, pDis->rcItem.left,
  44. pDis->rcItem.top, pDis->rcItem.right,
  45. pDis->rcItem.bottom);*/
  46. //绘制圆角按扭
  47. /*RoundRect(pDis->hDC, pDis->rcItem.left,
  48. pDis->rcItem.top, pDis->rcItem.right,
  49. pDis->rcItem.bottom,15,15);*/
  50. GetWindowText(pDis->hwndItem, szText, 256);
  51. //绘制按钮文字名称
  52. int nOldMode = SetBkMode(pDis->hDC, TRANSPARENT);//去文字背景色
  53. DrawText(pDis->hDC, szText, strlen(szText), &pDis->rcItem,
  54. DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  55. SetBkMode(pDis->hDC, nOldMode);//在给设置回来
  56. }
  57. }
  58. void OnMeasureitem(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  59. {
  60. LPMEASUREITEMSTRUCT pMis = (LPMEASUREITEMSTRUCT)lParam;
  61. pMis->itemHeight = 200;
  62. }
  63. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  64. {
  65. switch (nMsg)
  66. {
  67. case WM_CREATE:
  68. OnCreate(hWnd,nMsg,wParam,lParam);
  69. break;
  70. case WM_DRAWITEM:
  71. OnDrawItem(hWnd, wParam, lParam);
  72. return 0;
  73. case WM_MEASUREITEM: //使用这项,这项对于BUTTON没有影响,但对COMBOBOXLIST有影响
  74. OnMeasureitem(hWnd,nMsg,wParam,lParam);
  75. break;
  76. case WM_DESTROY:
  77. PostQuitMessage(0);
  78. break;
  79. }
  80. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  81. }
  82. BOOL RegisterWnd(LPSTR pszClassName)
  83. {
  84. WNDCLASSEX wce = { 0 };
  85. wce.cbSize = sizeof(wce);
  86. wce.cbClsExtra = 0;
  87. wce.cbWndExtra = 0;
  88. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  89. wce.hCursor = NULL;
  90. wce.hIcon = NULL;
  91. wce.hIconSm = NULL;
  92. wce.hInstance = g_hInst;
  93. wce.lpfnWndProc = WndProc;
  94. wce.lpszClassName = pszClassName;
  95. wce.lpszMenuName = NULL;
  96. wce.style = CS_HREDRAW | CS_VREDRAW;
  97. ATOM atom = RegisterClassEx(&wce);
  98. if (atom == NULL)
  99. {
  100. return FALSE;
  101. }
  102. else
  103. {
  104. return TRUE;
  105. }
  106. }
  107. HWND CreateWnd(LPSTR pszClassName)
  108. {
  109. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  110. CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
  111. return hWnd;
  112. }
  113. void ShowWnd(HWND hWnd)
  114. {
  115. ShowWindow(hWnd, SW_SHOW);
  116. UpdateWindow(hWnd);
  117. }
  118. void Msg()
  119. {
  120. MSG msg = { 0 };
  121. while (GetMessage(&msg, NULL, 0, 0))
  122. {
  123. TranslateMessage(&msg);
  124. DispatchMessage(&msg);
  125. }
  126. }
  127. void ConsoleWnd()
  128. {
  129. AllocConsole();
  130. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  131. CHAR szText[] = "Debug start:\n";
  132. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  133. }
  134. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  135. {
  136. g_hInst = hInstance;
  137. //ConsoleWnd();
  138. RegisterWnd("oooo");
  139. HWND hWnd = CreateWnd("oooo");
  140. ShowWnd(hWnd);
  141. Msg();
  142. return 0;
  143. }





posted @ 2016-06-10 08:32  -刀狂剑痴-  阅读(295)  评论(0编辑  收藏  举报