I have a strange problem. I am making a Win32 application in VC ++ 2008, creating a class to encapsulate most of the work for easy repetition on invocation MessageBox. A message box is created (I think), but does not appear if you do not press the Alt key!
What's happening:
I am running a program
press Enter
the main window will lose focus
gives a beep when I click on the main window, as if a modal MessageBox is present
either press Escape ... the focus is received OR press Alt, then a MessageBox will appear with the alt key pressed (that is, the menu will be reset).
PS It worked fine, but it happened all of a sudden. I did not find any difference - I even made a new project!
The main program is supposed to:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
CWnd cMainWindow(TEXT("DentoMan"), TEXT("Bejkoman"));
cMainWindow.CreateDef();
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
While this is a class file
CWnd::CWnd() {
};
CWnd::CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName) {
CWnd::lpszClassName = lpszClassName;
CWnd::lpszWindowName = lpszWindowName;
};
CWnd::~CWnd() {
};
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;
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) {
if ( Message == WM_CREATE ) {
SetWindowLong( hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams);
}
CWnd *Destination = (CWnd*)GetWindowLong( hWnd, GWL_USERDATA );
if (Destination) {
return Destination->WndProc( hWnd, Message, wParam, lParam );
}
return DefWindowProc( hWnd, Message, wParam, lParam );
};
LRESULT CWnd::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch (Message) {
case WM_LBUTTONDOWN:
{
SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION,NULL);
break;
}
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
UnregisterClass(lpszClassName, hInst);
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
case VK_RETURN:
MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), NULL);
return 0;
}
break;
case WM_COMMAND:
break;
case WM_PAINT:
break;
default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
};
Header Title:
class CWnd {
public:
CWnd();
CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName);
virtual ~CWnd();
virtual HWND CreateDef(void);
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;
HINSTANCE hInst;
LPTSTR lpszClassName;
LPTSTR lpszWindowName;
};
P.S. , , MessageBox