创建基于对话框的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。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 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)。
————————————————
本文为本人原创,转载请注明出处。