9th 学习博客:使用Codebloks实现C++的图形化界面

     使用开发工具codeblocks,添加ResEdit.exe这个控件,可以很方便地进行图形化编辑,这是在网上找得教程,实现的是最基本的在对话框内添加按钮,并实现单击响应在控制台输出相应的文字。

main.cpp

 1 #include <windows.h>
 2 #include <commctrl.h>
 3 #include <stdio.h>
 4 #include "resource.h"
 5 
 6 HINSTANCE hInst;
 7 HWND button1;
 8 
 9 BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
10 {
11     switch(uMsg)
12     {
13     case WM_INITDIALOG:
14     {
15 
16     }
17     return TRUE;
18 
19     case WM_CLOSE:
20     {
21         if(MessageBox(hwndDlg,"Close the dialog?","Prompt",MB_YESNO) == IDYES)
22         {
23             EndDialog(hwndDlg, 0);
24         }
25     }
26     return TRUE;
27 
28     case WM_COMMAND:
29     {
30         switch(LOWORD(wParam))
31         {
32         case IDC_BUTTON1:
33             printf("the button1 is clicked!
");
34             SetWindowText(button1,TEXT("my button"));
35             break;
36         }
37     }
38     return TRUE;
39     }
40     return FALSE;
41 }
42 
43 
44 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
45 {
46     hInst=hInstance;
47     InitCommonControls();
48     return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
49 }

resource.h

1 #ifndef IDC_STATIC
2 #define IDC_STATIC (-1)
3 #endif
4 
5 #define DLG_MAIN                                100
6 #define IDC_BUTTON1                             40000

执行效果图

    无论最终能否真正编辑出理想的程序,但在这个努力的过程中,我学会了C++的GUI开发方式也是一种收获,同时也开拓了眼界,也希望给其余正在学习的同学们带来借鉴。

原文地址:https://www.cnblogs.com/landscape/p/6072246.html