參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
建立Dialog視窗的方式,除了使用CreateWindow()之外,也可以使用另一個比較簡單的CreateDialog(),此種方式主要依賴於Resource檔案編輯,也就是一般快速建立GUI的工具,主要特點就是拖拉就可以完成GUI設計,而設計完成的Resource檔案(*.rc)必須編譯成res檔案,再透過編譯、連結成執行檔案,而在使用CreateDialog()建立Dialog時,則傳入該Resource檔案的ID即可
main.c
#include <windows.h> #include "resource.h" INT_PTR CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { DWORD dwID = wParam; switch (uMsg) { case WM_COMMAND: if (dwID == IDOK) { PostQuitMessage(0); return TRUE; } break; } return FALSE; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hWin = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc); ShowWindow(hWin, SW_SHOW); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } ExitProcess(0); return 0; }
main.rc
#include <windows.h> #include "resource.h" IDR_MAINFRAME ICON "main.ico" IDD_MAIN DIALOGEX 0, 0, 100, 42 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "main" FONT 10, "MS Shell Dlg", 0, 0, 0x1 BEGIN LTEXT "MyDialog", IDC_STATIC, 32, 0, 32, 16, SS_NOPREFIX | SS_CENTER ICON IDR_MAINFRAME, IDC_STATIC, 42, 10, 16, 16 DEFPUSHBUTTON "OK", IDOK, 0, 25, 100, 16, WS_GROUP END
resource.h
#define IDD_MAIN 101 #define IDR_MAINFRAME 102 #define IDC_STATIC -1
main.ico
編譯、執行
$ x86_64-w64-mingw32-windres main.rc -o main.res $ winegcc main.c main.res -o main $ wine ./main.exe