0%

基于Windows.h的C++提示对话框封装接口

  1. MessageBox函数接口
  2. 类型常量说明
  3. 函数返回值说明
  4. 封装接口实现

MessageBox函数接口

1
int MessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType);
  • hWnd:对话框父窗口句柄,对话框显示在Delphi窗体内,可使用窗体的Handle属性,否则可用0,使其直接作为桌面窗口的子窗口。
  • lpText:欲显示的信息字符串。
  • lpCaption:对话框标题字符串。
  • uType:对话框类型常量。

该函数的返回值为整数,用于对话框按钮的识别。

类型常量说明

https://blog.csdn.net/qq_35040828/article/details/51690413

对话框的类型常量可由按钮组合、缺省按钮、显示图标、运行模式四种常量组合而成。

(1)按钮组合常量

1
2
3
4
5
6
MB_OK = $00000000;               // 一个确定按钮
MB_OKCANCEL = $00000001; // 一个确定按钮,一个取消按钮
MB_ABORTRETRYIGNORE = $00000002; // 一个异常终止按钮,一个重试按钮,一个忽略按钮
MB_YESNOCANCEL = $00000003; // 一个是按钮,一个否按钮,一个取消按钮
MB_YESNO = $00000004; // 一个是按钮,一个否按钮
MB_RETRYCANCEL = $00000005; // 一个重试按钮,一个取消按钮

(2)缺省按钮常量

1
2
3
4
MB_DEFBUTTON1 = $00000000;     // 第一个按钮为缺省按钮
MB_DEFBUTTON2 = $00000100;     // 第二个按钮为缺省按钮
MB_DEFBUTTON3 = $00000200;     // 第三个按钮为缺省按钮
MB_DEFBUTTON4 = $00000300;     // 第四个按钮为缺省按钮

(3)图标常量

1
2
3
4
5
6
7
8
9
MB_ICONHAND = $00000010;               // “×”号图标
MB_ICONQUESTION = $00000020; // “?”号图标
MB_ICONEXCLAMATION = $00000030;     // “!”号图标
MB_ICONASTERISK = $00000040;      // “i”图标
MB_USERICON = $00000080; // 用户图标
MB_ICONWARNING = MB_ICONEXCLAMATION; // “!”号图标
MB_ICONERROR = MB_ICONHAND; // “×”号图标
MB_ICONINFORMATION = MB_ICONASTERISK; // “i”图标
MB_ICONSTOP = MB_ICONHAND; // “×”号图标

(4)运行模式常量

1
2
3
4
MB_APPLMODAL = $00000000;    // 应用程序模式,在未结束对话框前也能切换到另一应用程序
MB_SYSTEMMODAL = $00001000; // 系统模式,必须结束对话框后,才能做其他操作
MB_TASKMODAL = $00002000; // 任务模式,在未结束对话框前也能切换到另一应用程序
MB_HELP = $00004000; // Help Button

函数返回值说明

1
2
3
4
5
6
7
8
0                //对话框建立失败
IDOK = 1 //按确定按钮
IDCANCEL = 2 //按取消按钮
IDABOUT = 3 //按异常终止按钮
IDRETRY = 4 //按重试按钮
IDIGNORE = 5 //按忽略按钮
IDYES = 6 //按是按钮
IDNO = 7 //按否按钮

封装接口实现

.h文件

1
2
3
4
5
6
7
8
9
10
#pragma once
#include <Windows.h>
#include "KvStrCvt.h"

namespace KvMsgBox {
int info(const std::string& text, const std::string& title = "消息");
int warning(const std::string& text, const std::string& title = "警告");
int error(const std::string& text, const std::string& title = "失败");
int question(const std::string& text, const std::string& title = "问题");
}

.cpp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "KvMsgBox.h"

int KvMsgBox::info(const std::string& text, const std::string& title)
{
return MessageBox(NULL, KvStrCvt::s2ws(text).c_str(),
KvStrCvt::s2ws(title).c_str(), MB_OKCANCEL | MB_ICONINFORMATION);
}

int KvMsgBox::warning(const std::string& text, const std::string& title)
{
return MessageBox(NULL, KvStrCvt::s2ws(text).c_str(),
KvStrCvt::s2ws(title).c_str(), MB_OKCANCEL | MB_ICONWARNING);
}

int KvMsgBox::error(const std::string& text, const std::string& title)
{
return MessageBox(NULL, KvStrCvt::s2ws(text).c_str(),
KvStrCvt::s2ws(title).c_str(), MB_OKCANCEL | MB_ICONERROR);
}

int KvMsgBox::question(const std::string& text, const std::string& title)
{
return MessageBox(NULL, KvStrCvt::s2ws(text).c_str(),
KvStrCvt::s2ws(title).c_str(), MB_OKCANCEL | MB_ICONQUESTION);
}

补充:KvStrCvt::s2ws()函数实现:

1
2
3
4
5
6
7
8
9
10
11
std::wstring KvStrCvt::s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, NULL, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}