創建基於對話框的Windows應用程序(一) —— 新建窗體
1、新建一個Visual C++的Empty Project。
2、在Solution Explorer中右鍵Add New Item,添加 .cpp 文件,並提供Win32應用程序的入口點函數。
3、在Solution Explorer或 Resources View 中右鍵Add Resource,選擇Dialog。並在修改相關內容。
4、切換到 .cpp文件中,創建回調函數(Dlg_Proc),並在入口點函數中調用DialogBoxParam。

1 #include <Windows.h> 2 #include <tchar.h> 3 #include "Resource.h" 4 5 INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 6 switch (uMsg) 7 { 8 case WM_CLOSE: 9 EndDialog(hwnd, 0); 10 break; 11 } 12 13 return(FALSE); 14 } 15 16 int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) { 17 DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_DIALOG), 18 NULL, Dlg_Proc, _ttoi(pszCmdLine)); 19 return(0); 20 }
5、此時按下F5 Start Debugging,可以看到剛才新建的對話框。
6、在Output欄中顯示的路徑下可以找到生成的 .exe文件。將其拷貝到其他運行環境再運行時可能會發生錯誤。
7、該錯誤的解決辦法是在項目的Properties里,在C/C++ – “Code Generation”中,將“Runtime Library”一欄設為Multi-threaded Debug (/MTd)。
————————————————
本文為本人原創,轉載請注明出處。