当前位置:   article > 正文

C++ Windows窗口程序:子窗口控件之按钮类button_bs_pushbutton

bs_pushbutton

Windows窗口程序设计中,按钮、文本编辑框等控件都作为一个子窗口在WM_CREATE事件中创建的。其中按钮类button有多种类型和风格,常见的单选钮、复选钮、分组框也在此类中,见下表:

  1. 子窗口控件:按钮类button
  2. 按钮类型,可以是按钮风格与窗口风格的组合
  3. 窗口风格:
  4. WS_CHILD 子窗口,必须有
  5. WS_VISIBLE 窗口可见,一般都有
  6. WS_DISABLED 禁用窗口,创建初始状态为灰色不可用的按钮时使用
  7. WS_TABSTOP 可用Tab键选择
  8. WS_GROUP 成组,用于成组的单选按钮中的第一个按钮
  9. 按钮风格:
  10. BS_PUSHBUTTON 下压式按钮,也即普通按钮
  11. BS_CHECKBOX 复选按钮,不常用
  12. BS_AUTOCHECKBOX 含自动选中状态的复选按钮
  13. BS_RADIOBUTTON 单选按钮,不常用
  14. BS_AUTORADIOBUTTON 含自动选中状态的单选按钮
  15. BS_3STATE 三态复选按钮,不常用
  16. BS_AUTO3STATE 含自动选中状态的三态复选按钮
  17. BS_GROUPBOX 分组框,用来放置单选或复选按钮的
  18. BS_OWNERDRAW 用于自定义形状的类型,比如画个圆形的按钮
  19. 以上风格指定了创建的按钮类型,不能同时使用,但必须有其一
  20. 其中非自动类型的按钮需要自己编程来实现选中与否的状态切换
  21. BS_BITMAP 按钮上将显示位图
  22. BS_DEFPUSHBUTTON 设置为默认按钮,只用于下压式按钮,一个对话框中只能指定一个默认按钮
  23. 创建方法:
  24. HWND hwndButton;
  25. hwndButton = CreateWindow ( TEXT("button"), /*子窗口类名称*/
  26. TEXT("Command Button"), /*按钮上的文字*/
  27. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, /*按钮类型*/
  28. left, top, width, height, /*位置*/
  29. hwnd, /*父窗口句柄*/
  30. (HMENU)IDcmdButton, /*按钮ID,一个自定义整型常量*/
  31. ((LPCREATESTRUCT) lParam)->hInstance, NULL);
  32. if (!hwndButton) MessageBox(NULL,"创建按钮失败","Message",MB_OK|MB_ICONERROR);
  33. ShowWindow(hwndButton,SW_SHOW);
  34. UpdateWindow(hwndButton);

再来用DEV-C++的windows application示例代码,创建这十种button类按钮。(代码获得方法见上一篇博文《C++ 用DEV-C++建一个Windows窗口程序带文本框和命令按钮》)

先建一个结构数组,存放这些按钮类型常数:

  1. struct button {
  2. int swStyle;
  3. const char *szText;
  4. }
  5. Buttons[] = {
  6. BS_PUSHBUTTON, TEXT("普通按钮"),
  7. BS_DEFPUSHBUTTON, TEXT("默认按钮"),
  8. BS_RADIOBUTTON, TEXT("单选按钮"),
  9. BS_CHECKBOX, TEXT("复选按钮"),
  10. BS_3STATE, TEXT("三态按钮"),
  11. BS_AUTORADIOBUTTON, TEXT("自动单选按钮"),
  12. BS_AUTOCHECKBOX, TEXT("自动复选按钮"),
  13. BS_AUTO3STATE, TEXT("自动三态按钮"),
  14. BS_GROUPBOX, TEXT("按钮分组框"),
  15. BS_OWNERDRAW, TEXT("自画类型按钮")
  16. };

然后在WM_CREATE事件中遍历数组创建全部按钮,在WM_COMMAND事件中写入鼠标单击响应代码就完成了:

  1. #include <windows.h>
  2. struct button {
  3. int swStyle;
  4. const char *szText;
  5. }
  6. Buttons[] = {
  7. BS_PUSHBUTTON, TEXT("普通按钮"),
  8. BS_DEFPUSHBUTTON, TEXT("默认按钮"),
  9. BS_RADIOBUTTON, TEXT("单选按钮"),
  10. BS_CHECKBOX, TEXT("复选按钮"),
  11. BS_3STATE, TEXT("三态按钮"),
  12. BS_AUTORADIOBUTTON, TEXT("自动单选按钮"),
  13. BS_AUTOCHECKBOX, TEXT("自动复选按钮"),
  14. BS_AUTO3STATE, TEXT("自动三态按钮"),
  15. BS_GROUPBOX, TEXT("按钮分组框"),
  16. BS_OWNERDRAW, TEXT("自画类型按钮")
  17. };
  18. /* This is where all the input to the window goes to */
  19. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  20. {
  21. int num = sizeof Buttons / sizeof Buttons[0];
  22. switch(Message) {
  23. //在 WM_CREATE 事件中创建所有按钮类型
  24. case WM_CREATE: {
  25. for (int i = 0; i < num; i++) {
  26. CreateWindow( TEXT("button"),
  27. Buttons[i].szText,
  28. WS_CHILD | WS_VISIBLE | WS_TABSTOP | Buttons[i].swStyle,
  29. 80 + (i >= num / 2 ? 240 : 0),
  30. 60 + i * 50 - (i >= num / 2 ? 250 : 0),
  31. 160, 40,
  32. hwnd,
  33. (HMENU)i,
  34. ((LPCREATESTRUCT) lParam)->hInstance,
  35. NULL);
  36. }
  37. break;
  38. }
  39. //在 WM_COMMAND 事件中会响应button类的鼠标单击,但其中GROUPBOX不响应操作
  40. case WM_COMMAND: {
  41. MessageBox(NULL, Buttons[LOWORD(wParam)].szText, "Button_Click()", MB_OK);
  42. break;
  43. }
  44. /* Upon destruction, tell the main thread to stop */
  45. case WM_DESTROY: {
  46. PostQuitMessage(0);
  47. break;
  48. }
  49. /* All other messages (a lot of them) are processed using default procedures */
  50. default:
  51. return DefWindowProc(hwnd, Message, wParam, lParam);
  52. }
  53. return 0;
  54. }
  55. /* The 'main' function of Win32 GUI programs: this is where execution starts */
  56. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  57. {
  58. WNDCLASSEX wc; /* A properties struct of our window */
  59. HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
  60. MSG msg; /* A temporary location for all messages */
  61. /* zero out the struct and set the stuff we want to modify */
  62. memset(&wc,0,sizeof(wc));
  63. wc.cbSize = sizeof(WNDCLASSEX);
  64. wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
  65. wc.hInstance = hInstance;
  66. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  67. /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
  68. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  69. wc.lpszClassName = "WindowClass";
  70. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
  71. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
  72. if(!RegisterClassEx(&wc)) {
  73. MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  74. return 0;
  75. }
  76. hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Button Style",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
  77. CW_USEDEFAULT, /* x */
  78. CW_USEDEFAULT, /* y */
  79. 640, /* width */
  80. 480, /* height */
  81. NULL,NULL,hInstance,NULL);
  82. if(hwnd == NULL) {
  83. MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  84. return 0;
  85. }
  86. /*
  87. This is the heart of our program where all input is processed and
  88. sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
  89. this loop will not produce unreasonably high CPU usage
  90. */
  91. while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
  92. TranslateMessage(&msg); /* Translate key codes to chars if present */
  93. DispatchMessage(&msg); /* Send it to WndProc */
  94. }
  95. return msg.wParam;
  96. }

运行效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/488403
推荐阅读
相关标签
  

闽ICP备14008679号