Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SendMessage from child thread to main window

6 views
Skip to first unread message

remove_this_b...@hotmail.com

unread,
Jan 16, 2002, 2:36:43 AM1/16/02
to
I have a problem with the SendMessage function... I am trying to create a
splash screen for my application so I decide to create a thread to do the
job while my main thread does some initialisation work. The splash screen
thread is supposed to use SendMessage to notify the main window that the it
(the splash) is done so that the main window can appear. So far I know that
my spash screen thread sends the message to the main window but the message
is never caught by the window. I'll post the relevant code below..

#define WM_CUSTOM_THREAD_GONE (WM_APP + 2)

DWORD WINAPI SplashScreen(LPVOID param)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
/*GetModuleHandle(NULL)*/ appInst, NULL, NULL, NULL,
MAKEINTRESOURCE( IDR_MENU1 ),
"MyApp", NULL };

RegisterClassEx( &wc );

// Create the application's window.
hWnd = CreateWindow( "MyApp", "MyApp",
WS_OVERLAPPEDWINDOW, 100, 100, 600, 600,
GetDesktopWindow(), NULL, wc.hInstance,
NULL );

hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SplashScreen, NULL,
0, &threadID);


InitialiseDirectX8(hWnd);
ready = true;
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);

MSG msg;
while(1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CUSTOM_THREAD_GONE:
MessageBeep(1);
MessageBox(hWnd, "Eureka!","",MB_OK);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
visible = true;
SetTimer(hWnd, THE_TIMER, 1000, NULL);
break;
}
}

DWORD WINAPI SplashScreen(LPVOID param)
{
WNDCLASSEX splashWC = { sizeof(WNDCLASSEX), CS_CLASSDC, SplashProc, 0L,
0L,
appInst, NULL, NULL, (HBRUSH)COLOR_WINDOW, NULL,
"Splash", NULL };
RegisterClassEx( &splashWC );
hSplashScreen = CreateWindow( "Splash", "Splash",
0, 100, 100, 600, 600,
hWnd, NULL, splashWC.hInstance, NULL );

ShowWindow(hSplashScreen, SW_SHOW);
UpdateWindow(hSplashScreen);

// The message loop.
MSG msg;
while(1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}

LRESULT WINAPI SplashProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam )
{

switch( msg )
{
case WM_DESTROY:
PostMessage(GetParent(hSplashScreen), WM_CUSTOM_THREAD_GONE,
0,0);
SetForegroundWindow(hWnd);
PostQuitMessage(0);
return 0;
case WM_CREATE:
SetTimer(hWnd, SPLASH_TIMER, 5000, NULL);
return 0;
case WM_PAINT:
ValidateRect( hWnd, NULL );
return 0;
case WM_TIMER:
switch(wParam) {
case SPLASH_TIMER:
SendMessage(hSplashScreen, WM_DESTROY, 0, 0);
break;
}
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}


Jugoslav Dujic

unread,
Jan 16, 2002, 10:11:24 AM1/16/02
to
"~blastin...@hotmail.com" <remove_this_b...@hotmail.com> wrote
in message news:f4a18.1114$aV1.1...@news-nb00s0.nbnet.nb.ca...

| I have a problem with the SendMessage function... I am trying to create a
| splash screen for my application so I decide to create a thread to do the
| job while my main thread does some initialisation work. The splash screen
| thread is supposed to use SendMessage to notify the main window that the it
| (the splash) is done so that the main window can appear. So far I know that
| my spash screen thread sends the message to the main window but the message
| is never caught by the window. I'll post the relevant code below..

Allow me to disagree with your logic. An application *really*
seldom has to have more than one message loop and, even more
seldom, more than one GUI thread. I have a similar code, with
the (big) difference that I create splash in WinMain and create
a *worker* thread (no windows, no message loop, just
loading&processing data). Before that thread dies, it sends
a custom message to the main frame to hide splash and enable all
GUI stuff (in the meantime, it also updates a progress bar
on the splash). In this way, splash screen is visible only as long
as the data are really loading, no bloody timer.

Your code could probably be fixed but, again, I dislike the
approach. Possibly the problem is that for SendMessage() to
work accross threads, the other thread's message loop has to
be active, but it's still initializing so it won't reach
the parent. Furthermore, you're creating a splash window
with hWnd as parent but 0 (WS_OVERLAPPED) as style, while
the parent is in another thread??? Maybe it works but it's
odd at best.

Jugoslav
________________________
www.geocities.com/jdujic

remove_this_b...@hotmail.com

unread,
Jan 17, 2002, 1:08:01 AM1/17/02
to
Actually I have to keep the main thread doing the initialisation stuff
because it's the only one that is allowed to call the DirectX functions..
R.

"Jugoslav Dujic" <jdu...@yahoo.com> wrote in message
news:a2451c$uessg$1...@ID-106075.news.dfncis.de...

0 new messages