博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
19windows_19_OwnerDraw自制按钮DIYBUTTON
阅读量:5965 次
发布时间:2019-06-19

本文共 3728 字,大约阅读时间需要 12 分钟。

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. }

转载于:https://www.cnblogs.com/nfking/p/5573181.html

你可能感兴趣的文章
【干货】界面控件DevExtreme视频教程大汇总!
查看>>
闭包 !if(){}.call()
查看>>
python MySQLdb安装和使用
查看>>
Java小细节
查看>>
poj - 1860 Currency Exchange
查看>>
chgrp命令
查看>>
Java集合框架GS Collections具体解释
查看>>
洛谷 P2486 BZOJ 2243 [SDOI2011]染色
查看>>
数值积分中的辛普森方法及其误差估计
查看>>
Web service (一) 原理和项目开发实战
查看>>
跑带宽度多少合适_跑步机选购跑带要多宽,你的身体早就告诉你了
查看>>
深入理解Java的接口和抽象类
查看>>
Javascript异步数据的同步处理方法
查看>>
iis6 zencart1.39 伪静态规则
查看>>
SQL Server代理(3/12):代理警报和操作员
查看>>
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>
2018年尾总结——稳中成长
查看>>
JFreeChart开发_用JFreeChart增强JSP报表的用户体验
查看>>
度量时间差
查看>>
通过jsp请求Servlet来操作HBASE
查看>>