In the code below as indicated the global assignment of the handle returned from the call to CreateWindowW() seemed to go out of scope. I could not access it outside of InitInstance(). When I moved the assignment into WinProc(), it remained in scope in other functions that I accessed it in.
HWND hWndTop; // Handle to top window
.
.
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
.
.
HWND hWnd = CreateWindowW(szWindowClass,....
hWndTop = hWnd; // This did not work.
.
.
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
hWndTop = hWnd; // This did work.
.
.
}
CodePudding user response:
...It's just that your
WndProcis trying to usehWndTopbeforeInitInstancecan set its value. In other words, the issue is not "my assignment tohWndTopis being undone". The issue is "my assignment tohWndTophasn't happened yet." – Raymond Chen
CreateWindow()will send a few messages toWndProc(), such asWM_(NC)CREATE,WM_GETMINMAXINFO, etc, before exiting toInitInstance(). So,hWndTopwon't have been set yet for every message thatWndProc()receives – Remy Lebeau
