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

时间:2023-03-09 03:11:06
9th 学习博客:使用Codebloks实现C++的图形化界面

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

main.cpp

 #include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h" HINSTANCE hInst;
HWND button1; BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{ }
return TRUE; case WM_CLOSE:
{
if(MessageBox(hwndDlg,"Close the dialog?","Prompt",MB_YESNO) == IDYES)
{
EndDialog(hwndDlg, );
}
}
return TRUE; case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
printf("the button1 is clicked!\n");
SetWindowText(button1,TEXT("my button"));
break;
}
}
return TRUE;
}
return FALSE;
} int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

resource.h

 #ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif #define DLG_MAIN 100
#define IDC_BUTTON1 40000

执行效果图

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

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