Click to See Complete Forum and Search --> : Main Window handle, When to load user data?


UnfitElf
December 11th, 2006, 03:54 AM
Hi people,

I have a global HWND variable (gHwnd) which is assigned from a call to CreateWindowEx. I have a timer variable stored in a file which i am reading it. I then create the timer (in the WM_CREATE event) and pass it the gHwnd as the paramater.

The problem is the timer message (im not using a timer proc, but the WM_TIMER message) never gets send to my window.

However, if i pass the timer function the hwnd varibale passed into the windows proc on the WM_CREATE message the timer message is sent to my window.

In short, the gHwnd is not correct or invalid or somthing, why?

Some code will help explane :)


int WINAPI WinMain (... etc

{
INITCOMMONCONTROLSEX init = { sizeof(INITCOMMONCONTROLSEX), ICC_STANDARD_CLASSES };
//ICC_STANDARD_CLASSES | ICC_LISTVIEW_CLASSES };
InitCommonControlsEx(&init);

MSG messages; // Here messages to the application are saved
WNDCLASSEX wincl; // Data structure for the windowclass

// The Window structure
// windows class setup etc..
// Register the window class, and if it fails quit the program
if (!RegisterClassEx (&wincl))
return 0;

// gHwnd Assigned here!

// The class is registered, let's create the program
gHwnd = CreateWindowEx (
0, // Extended possibilites for variation
szClassName, // Classname
"CheckIt v2.0", // Title Text
WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, //WS_OVERLAPPEDWINDOW, // default window
CW_USEDEFAULT, // Windows decides the position
CW_USEDEFAULT, // where the window ends up on the screen
650, // The programs width
270, // and height in pixels
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
hThisInstance, // Program Instance handler
NULL // No Window Creation data
);

// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&messages, NULL, 0, 0))
{
// Translate virtual-key messages into character messages
TranslateMessage(&messages);
// Send message to WindowProcedure
DispatchMessage(&messages);
}
// The program return-value is 0 - The value that PostQuitMessage() gave
return (int)messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) // handle the messages
{
case WM_CREATE:
{
// SetTimer(gHwnd, someValue); // My main window doesn't recieve messages
SetTimer(hwnd, someValue); // Works!!

// Shouldnt gHwnd and hwnd be the same :S ???

}
break;

// etc etc..


The problem is when do i create the timer if i need a valid handle to my main window? Normally my timer creation (along with other stuff is in a function) I dont really want to pass the hwnd to the function as it doesnt make 'natural sence' for the function name etc. (fussy i know lol).

I want my global !! :(

Any help?

wildfrog
December 11th, 2006, 04:15 AM
The problem is the timer message (im not using a timer proc, but the WM_TIMER message) never gets send to my window.

However, if i pass the timer function the hwnd varibale passed into the windows proc on the WM_CREATE message the timer message is sent to my window.

In short, the gHwnd is not correct or invalid or somthing, why?
The CreateWindow/CreateWindowEx function hasn't yet returned when the WM_CREATE message is handled in the windows procedure. So, your global HWND isn't yet initialized.

- petter

Krishnaa
December 11th, 2006, 04:23 AM
The CreateWindow/CreateWindowEx function hasn't yet returned when the WM_CREATE message is handled in the windows procedure. So, your global HWND isn't yet initialied.

- petter

Exactly :thumb:

UnfitElf
December 11th, 2006, 08:42 PM
Cheers, i thought it must have been something like that.

I guess my question now then is, what is the 'hwnd' varibale that gets passed into the WM_CREATE message a handle to?

Secondly, if the main window handle is needed to load userdata what event would you recomend loading the data in on?

Thanks v. much :)

wildfrog
December 12th, 2006, 01:49 AM
I guess my question now then is, what is the 'hwnd' varibale that gets passed into the WM_CREATE message a handle to?Well, it is a handle to the window being created (and it is the same handle as gHwnd eventually will become).

Secondly, if the main window handle is needed to load userdata what event would you recomend loading the data in on?That depends on when the userdata needs to be loaded. If it's needed before the window 'pops up' you can do it at WM_CREATE (passing hwnd) or maybe after the CreateWindow has returned (passing gHwnd). Or, you can do it in response to button clicks etc.

- petter

UnfitElf
December 12th, 2006, 04:33 AM
Thanks very much Petter, you have helped me out on numerious occations! :)

I guess ill do a cheep fix of:

// ...
case WM_CREATE:
gHwnd = hwnd;
LoadData();
// etc. etc.
break;


Thanks again :)

P.S. if you want to checkout the end result Click here (http://www.codeguru.com/forum/showthread.php?t=408916) ;)