我的 Visual C++ Win32 中有一个无法关闭的 MessageBox!到底是怎么回事?
I have a MessageBox in my Visual C++ Win32 that cannot be closed! What is going on?
我有一个无法关闭的消息框,我不确定为什么。它是在我的 window 过程中为一个对话框创建的,它是这样创建的:
hwnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, WndProc);
下面是有问题的WndProc代码。问题出在 MessageBox 调用上,OK 按钮没有任何作用,点击该消息框上的 "X" 也没有。
BOOL CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH bgBrush;
switch (msg)
{
case WM_INITDIALOG:
bgBrush = CreateSolidBrush(RGB(65, 65, 66));
return TRUE;
case WM_CTLCOLORDLG:
return (INT_PTR)(bgBrush);
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
SetTextColor((HDC)wParam, RGB(200, 200, 200));
return (INT_PTR)(bgBrush);
case WM_DEVICECHANGE:
USBEnumerateDev();
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_SELECTPROFILE:
switch (HIWORD(wParam))
{
case BN_CLICKED:
int res = CommonFileDialog.BasicFileOpen(hwnd);
if (res == TRUE)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITWLAN));
if (len > 0)
{
int i;
wchar_t* buf;
buf = (wchar_t*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(hwnd, IDC_EDITWLAN, buf, len + 1);
GlobalFree((HANDLE)buf);
}
}
break;
}
break;
case IDC_PROGRAM:
switch (HIWORD(wParam))
{
case BN_CLICKED:
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITWLAN));
if (len > 0)
{
int i, fileSize;
LPSTR XMLfilename, buf;
FILE *fXMLfile;
XMLfilename = (LPSTR)GlobalAlloc(GPTR, len + 1);
GetDlgItemTextA(hwnd, IDC_EDITWLAN, XMLfilename, len + 1);
fopen_s(&fXMLfile, XMLfilename, "r");
fseek(fXMLfile, 0L, SEEK_END);
fileSize = ftell(fXMLfile);
fseek(fXMLfile, 0L, SEEK_SET);
buf = (LPSTR)GlobalAlloc(GPTR, fileSize);
fread(buf, 1, fileSize, fXMLfile);
DBG("fileSize: %d", fileSize);
if (USBSetProfile(buf, fileSize))
MessageBox(hwnd, L"Programmed!", L"PC -> USB", MB_OK | MB_ICONINFORMATION);
GlobalFree((HANDLE)XMLfilename);
GlobalFree((HANDLE)buf);
//
}
}
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
什么给了?我试过用 window 句柄和 NULL 调用 MessageBox,同样的事情。
您的 WndProc()
正在呼叫 DefWindowProc()
,但 documentation 明确表示不要那样做:
Although the dialog box procedure is similar to a window procedure, it must not call the DefWindowProc function to process unwanted messages. Unwanted messages are processed internally by the dialog box window procedure.
这是 window 程序和对话程序的混合体。这些是不同的并且有不同的规则。
还要注意 return 值。你应该 return 对你处理的案件为真,对你没有处理的案件为假。你总是 return 0 (FALSE)。
我有一个无法关闭的消息框,我不确定为什么。它是在我的 window 过程中为一个对话框创建的,它是这样创建的:
hwnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, WndProc);
下面是有问题的WndProc代码。问题出在 MessageBox 调用上,OK 按钮没有任何作用,点击该消息框上的 "X" 也没有。
BOOL CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH bgBrush;
switch (msg)
{
case WM_INITDIALOG:
bgBrush = CreateSolidBrush(RGB(65, 65, 66));
return TRUE;
case WM_CTLCOLORDLG:
return (INT_PTR)(bgBrush);
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
SetTextColor((HDC)wParam, RGB(200, 200, 200));
return (INT_PTR)(bgBrush);
case WM_DEVICECHANGE:
USBEnumerateDev();
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_SELECTPROFILE:
switch (HIWORD(wParam))
{
case BN_CLICKED:
int res = CommonFileDialog.BasicFileOpen(hwnd);
if (res == TRUE)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITWLAN));
if (len > 0)
{
int i;
wchar_t* buf;
buf = (wchar_t*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(hwnd, IDC_EDITWLAN, buf, len + 1);
GlobalFree((HANDLE)buf);
}
}
break;
}
break;
case IDC_PROGRAM:
switch (HIWORD(wParam))
{
case BN_CLICKED:
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITWLAN));
if (len > 0)
{
int i, fileSize;
LPSTR XMLfilename, buf;
FILE *fXMLfile;
XMLfilename = (LPSTR)GlobalAlloc(GPTR, len + 1);
GetDlgItemTextA(hwnd, IDC_EDITWLAN, XMLfilename, len + 1);
fopen_s(&fXMLfile, XMLfilename, "r");
fseek(fXMLfile, 0L, SEEK_END);
fileSize = ftell(fXMLfile);
fseek(fXMLfile, 0L, SEEK_SET);
buf = (LPSTR)GlobalAlloc(GPTR, fileSize);
fread(buf, 1, fileSize, fXMLfile);
DBG("fileSize: %d", fileSize);
if (USBSetProfile(buf, fileSize))
MessageBox(hwnd, L"Programmed!", L"PC -> USB", MB_OK | MB_ICONINFORMATION);
GlobalFree((HANDLE)XMLfilename);
GlobalFree((HANDLE)buf);
//
}
}
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
什么给了?我试过用 window 句柄和 NULL 调用 MessageBox,同样的事情。
您的 WndProc()
正在呼叫 DefWindowProc()
,但 documentation 明确表示不要那样做:
Although the dialog box procedure is similar to a window procedure, it must not call the DefWindowProc function to process unwanted messages. Unwanted messages are processed internally by the dialog box window procedure.
这是 window 程序和对话程序的混合体。这些是不同的并且有不同的规则。
还要注意 return 值。你应该 return 对你处理的案件为真,对你没有处理的案件为假。你总是 return 0 (FALSE)。