Win32 MessageBox没有出现 [英] Win32 MessageBox doesn't appear
问题描述
我陷入一个奇怪的问题.我正在VC ++ 2008中制作Win32应用程序,制作了一个类来封装大多数工作,以便在调用MessageBox
时易于重复.消息框已创建(我认为),但除非按Alt键,否则不会显示!
I'm stuck with a strange problem. I'm making a Win32 application in VC++ 2008, making a class to encapsulate most of the work for easy repetition when calling a MessageBox
. The message box` is created (I think) but doesn't show up unless I press the Alt key!
到底发生了什么?
-
我运行程序
I run the program
按Enter
主窗口失去焦点
当我在主窗口上单击时发出蜂鸣声,好像存在模式消息框
give beep sound when i click on the main window as if a modal MessageBox is present
要么按Escape ...键,要么获得焦点,或者按Alt键,然后出现消息框,同时按下alt键(即,菜单将掉落)!!!!!!
either press Escape... focus is gained OR press Alt then the MessageBox appear with alt key pressed (i.e. menu will drop )!!!!!!
P.S.一切正常,但突然发生了这种情况.我没有发现任何不同-我什至做了一个新项目!
P.S. It was working fine but suddenly this happened. I didn't find any difference - I even made a new project!
这应该是主程序:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
CWnd cMainWindow(TEXT("DentoMan"), TEXT("Bejkoman")); // pass The class name and window name to the constructor
cMainWindow.CreateDef(); //Create the Window
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
虽然这是Class文件
While This is the Class file
CWnd::CWnd() {
};
CWnd::CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName) {
CWnd::lpszClassName = lpszClassName;
CWnd::lpszWindowName = lpszWindowName;
};
CWnd::~CWnd() {
};
// Create the window with default parameters
HWND CWnd::CreateDef(void) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = StaticWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = (HINSTANCE)GetModuleHandle(NULL);
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 4);
wcex.lpszMenuName = 0;
wcex.lpszClassName = lpszClassName;
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
g_hWnd = CreateWindowEx(0,lpszClassName, lpszWindowName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, this);
hInst = wcex.hInstance; //Store hInstance in the class hInst variable
if (!g_hWnd) return false;
ShowWindow(g_hWnd, SW_SHOW);
UpdateWindow(g_hWnd);
return g_hWnd;
}
LRESULT CALLBACK CWnd::StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
/* The Only Message we take here so we store the 'this' pointer within the window to identify messages
comming from it by the 'this' pointer*/
if ( Message == WM_CREATE ) {
SetWindowLong( hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams);
}
/* Store the window pointer in the class pointer we just created in order to run the right public WndPRoc */
CWnd *Destination = (CWnd*)GetWindowLong( hWnd, GWL_USERDATA );
// If the hWnd has a related class, pass it through
if (Destination) {
return Destination->WndProc( hWnd, Message, wParam, lParam );
}
// No destination found, defer to system...
return DefWindowProc( hWnd, Message, wParam, lParam );
};
LRESULT CWnd::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
// Determine message type
switch (Message) {
case WM_LBUTTONDOWN:
{
/* this is a common trick for easy dragging of the window.this message fools windows telling that the user is
actually dragging the application caption bar.*/
SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION,NULL);
break;
}
/*case WM_CREATE:
break;
*/
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
UnregisterClass(lpszClassName, hInst);
PostQuitMessage(0);
break;
case WM_KEYDOWN: //KeyBoard keys
// Which key was pressed?
switch (wParam) {
case VK_ESCAPE: //close through escape key
PostQuitMessage(0);
return 0;
case VK_RETURN:
MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), NULL);
return 0;
} // End Switch
break;
case WM_COMMAND:
/*switch(LOWORD(wParam))
{
}*/
break;
case WM_PAINT:
break;
default:
return DefWindowProc(hWnd, Message, wParam, lParam);
} // End Message Switch
return 0;
};
类标题:
class CWnd {
public:
CWnd();
CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName);
virtual ~CWnd();
virtual HWND CreateDef(void); // Create the window with default parameters
virtual LRESULT WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam );
private:
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
HWND g_hWnd; //Global window handle for this window
HINSTANCE hInst; //Global instance for this window
LPTSTR lpszClassName;
LPTSTR lpszWindowName;
};
P.S.我包括了所有需要的头文件,除MessageBox之外一切正常
P.S. I included all needed header files, everything goes fine except MessageBox
这也是此处
推荐答案
Ohhhhhhh我终于找到了解决此问题的方法……每个人都可以从WndProc(.......) WM_PAINT消息我在其中写了一些代码,并删除了所有代码以及BeginPaint和EndPaint函数,因此一旦在该消息上绘制了任何内容(包括MessageBox),该程序便进入冻结期,但仅在按Alt时显示,我认为该控件是转移到系统以显示系统菜单(我认为)
Ohhhhhhh finally i found the solution of this problem ... and for everyone to benefit the problem was in WndProc(.......) at the WM_PAINT Message i wrote some code in it and removed all the code along with BeginPaint and EndPaint functions so the program enter a freeze period once anything is being painted over it including that MessageBox but it only show when i press Alt i think coz the control is transfered to the system in that step to show the system Menu (i think)
该解决方案要么删除WM_PAINT消息处理程序,要么添加普通的BeginPaint和EndPaint函数
the solution either remove the WM_PAINT message handler or add the normal BeginPaint and EndPaint functions
感谢所有回答我的问题的人
Thanks for everyone who passed on my question
这篇关于Win32 MessageBox没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!