I spent a lot of time trying to find out why "Message Box'
made my program act like it crashed. Originally I had to
bring up the Task Manager every time I allowed "Message
Box' to run to shut down the program. After much
experimentation I found that if I hit the "Alt" key,
the MB would show on the screen. "OK"ing the MB allowed
my program to proceed normally. All along I suspected
a focus problem but since MB is a black box, I had no
idea what was happening. By pure chance, I put a little
TextOut code snippet into the function. I do this to
see what's going on inside functions quite a bit. With
these four lines of code, MB started working normally.
This is exactly the same solution to the SetTimer
problem I had posted a thread about in this site a week
or so back. It bothers me that I need a BeginPaint and
EndPaint entry to prevent strange things from happening
in my programs. I can force things to work right with
a fudge but that's not right. I'd sure like one of you
experts to tell me why this is occurring. I'm
running AMD with SP1. Compiling with either "cl" or
BCC55 gives me the same results. I'll include a small
program with the code in it so you can compile and
test to see what I'm talking about.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <commctrl.h>
#define ID_FILE_EXIT 400
#define ID_FILE_LOAD 401
//***** START Prototypes
int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow);
LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg , WPARAM wParam , LPARAM lParam);
HWND DoMenu(HWND hwndMainFrame);
HWND DoLoad(HWND hwndMainFrame);
//*******END Prototypes
HWND hwndMainFrame;
WPARAM wParam;
LPARAM lParam;
HINSTANCE hInstance;
WNDCLASSEX wc;
UINT uMsg;
MSG msg;
MSG message;
PAINTSTRUCT paintStruct;
HDC hDC;
HMENU hMyMenu;
HWND Menubar;
HMENU hFileMenu;
MENUITEMINFO mii;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
(void)UNREFERENCED_PARAMETER(hPrevInstance);
(void)UNREFERENCED_PARAMETER(lpCmdLine);
(void)UNREFERENCED_PARAMETER(nCmdShow);
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName="MyClass";
wc.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
if(!RegisterClassEx(&wc))
return 0;
hwndMainFrame = CreateWindowEx(NULL,"MyClass","MAINFRAME",WS_OVERLAPPEDWINDOW |
WS_VSCROLL | WS_VISIBLE | WS_CLIPCHILDREN,0,0,1024,740,
NULL,NULL,hInstance,NULL);
if (!hwndMainFrame)
return 0;
ShowWindow(hwndMainFrame, nCmdShow);
UpdateWindow(hwndMainFrame);
while (GetMessage(&message,NULL,0,0)>0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return msg.wParam;
} // ***************** END OF WINMAIN
LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ // ****************************** START MAIN WIND PROCEDURE 1
switch(uMsg)
{ // ********************** START SWITCH 2
case WM_CREATE:
DoMenu(hwndMainFrame);
break;
case WM_PAINT:
break;
case WM_SIZE:
break;
case WM_CLOSE:
PostQuitMessage(0);
DestroyWindow(hwndMainFrame);
break;
case WM_DESTROY:
PostQuitMessage(0);
DestroyWindow(hwndMainFrame);
break;
case WM_COMMAND:
switch(wParam)
{ // ***************** START MAIN WM_COMMAND
case ID_FILE_EXIT:
SendMessage(hwndMainFrame,WM_DESTROY,0,0);
break;
case ID_FILE_LOAD:
DoLoad(hwndMainFrame);
break;
} // ***************************************** END MAIN WM_COMMAND
break;
default:
return DefWindowProc(hwndMainFrame,uMsg, wParam, lParam);
} // ********************** END SWITCH 2
return 0;
} // **************************** END MAIN WIND PROCEDURE 1
// ************************************************************************ START OF FUNCTION GROUP
HWND DoMenu(HWND hwndMainFrame)
{ // ****************** START MENU
HMENU hMyMenu = CreateMenu();
IsMenu(hMyMenu);
if(hMyMenu == NULL)
MessageBox(hwndMainFrame,"No Menu","MBOX3",MB_OK);
AppendMenu(hMyMenu,MF_STRING,ID_FILE_LOAD,"LOAD");
AppendMenu(hMyMenu,MF_STRING,ID_FILE_EXIT,"EXIT");
SetMenu(hwndMainFrame,hMyMenu);
DrawMenuBar(hwndMainFrame);
UpdateWindow(hwndMainFrame);
return 0;
} // ******************* END MENU
// ***************************************************** START DO LOAD
HWND DoLoad(HWND hwndMainFrame)
{
/* hDC = GetDC(hwndMainFrame);// ------------------------------ TEST
BeginPaint(hwndMainFrame, &paintStruct);
TextOut(hDC,1,180,"Load Test Works",15);
EndPaint(hwndMainFrame, &paintStruct);
ReleaseDC(hwndMainFrame, hDC); */
MessageBox(hwndMainFrame,"No Menu","MBOX3",MB_OK);
return 0;
}
// ****************************************************** END DO LOAD
// ****************************************************** END PROGRAM
Well I thought I made it fairly clear in your previous thread TBH.
The timer doesn't work without the paint code in MyTimerFunc() because you don't handle WM_PAINT, so the window is not validated and the high frequency (and priority) of WM_PAINT messages blocks the low priority WM_TIMER message.
Just handle WM_PAINT as suggested and your timer will work without the misplaced painting.
I don't understand why you were getting crashes when calling MessageBox() however.
I do now understand why you were getting crashes with MessageBox() though - you weren't! It wasn't crashing at all, just unable to display the box due to app spending all it's time servicing WM_PAINT.
You must service WM_PAINT to validate the window, if you don't you will get little else done. Just do yourself a favour and add this to MainWndProc():
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwndMainFrame, &ps);
// Do your painting
EndPaint(hwndMainFrame, &ps);
return 0L;
}
OK, I'll try that. When you explained it to me in the timer thread, I thought you were suggesting that I leave the paint code right at the timer code. I'll try your suggestion now and put it in the paint case.
Just to let everyone know. The suggestion works great. That was my problem.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.