模态对话框,和非模态对话框,都可以用这个函数
image.png
image.png
image.png
代码运行结果:
[图片上传中...(image.png-aa322f-1607224657467-0)]
#include <windows.h>
#include <tchar.h>
#include <CommCtrl.h>
#include "resource.h"
//创建窗口模态对话框函数
INT_PTR WINAPI DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam )
{
switch (uMsg)
{
case WM_INITDIALOG:
MessageBox(hwndDlg, _T("对话框窗口初始化完成!"), _T("tip"), MB_OK);
break;
case WM_COMMAND:
{
UINT nCtrlID = LOWORD(wParam); //低16位是控件id
UINT nCode = HIWORD(wParam); //高16位是控件事件
if (nCode ==BN_CLICKED ) //如果是按钮点击事件!
{
if (nCtrlID == IDC_BTN_1) //如果控件id是按钮1的控件id
{
MessageBox(hwndDlg, _T("按钮1被点击了"), _T("tip"), MB_OK);
}
else if (nCtrlID == IDC_BTN_2)
{
MessageBox(hwndDlg, _T("按钮2被点击了"), _T("tip"), MB_OK);
}
else if (nCtrlID == IDCANCEL) //IDCANCEL,比较特殊,取消
{
int iRet = MessageBox(hwndDlg, _T("您确定要关闭按钮吗?"), _T("tip"), MB_OKCANCEL);
if (iRet ==IDOK )
{
EndDialog(hwndDlg, 888);
}
}
return true;
}
}
break;
default:
break;
}
return FALSE;
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
//MAKEINTRESOURCE,讲int转换为LPCWSTR
int nRet= DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc); //IDD_DIALOG1,定义在resource.h当中的,所以需要包含头文件,用include命令
return true;
}
网友评论