Skip to main content
GameDev.net gamedev.net
🔒 Locked

hwnd question

Started by sethiroth Aug 4, 2002 at 2:05 AM 8 replies 600+ views
Original Post
sethiroth
sethiroth
hi, if in visual c++ i wanted to get the window handle, is there a function like GetHwnd(HWND hWnd); or similar i can use to get my window handle....... actually im like 99.99999% sure it can be done i just dont know how and i been looking and couldnt find nothing from www.msdn.microsoft.com/library so i figure i would just ask... thanks
Qoy
Qoy
CreateWindow returns an HWND, and you just have to keep it somewhere.
hemihua
hemihua
look up the function GetWindow in the SDK
thuned
thuned
If you''re using MFC then you can use (HWND)*AfxGetMainWnd()
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Dean Harding
Dean Harding
Um, you''re passing a HWND in to your function there... that''s a window handle... what would the GetHwnd() function do?

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

sethiroth
sethiroth
ok i thank you all for your replys.. not really what i was after, but its my fault i shoulda explained it better.. well what i had was i created my window and everything.. ie hWnd = CreateWindow(......) ok this was fine and all hWnd was global coz i wanted to access it from some other functions in a different cpp file in the same project and for some reason it just wasnt working.. i tried to test with MessageBox(hWnd, ....) to see if it working properly and it wasnt working for some reason, no idea why but when i placed that message box STRAIGHT after i created my window it worked and yeah it compiled without error or warnings so it wasnt anything to do with not being able to access hWnd for those who think i might not have declared properly.. so i dunno, i managed to work it out somehow, dont know exactly what i did but i think i put my function that creates the window in the same cpp file.... its just a game engine i decided to start on, its my first attempt.. but if the problem with the hWnd was a caused with having my window creation stuff in a different cpp file then it will surve a problem when i need to do input and other stuff..... if anyone knows what im crapping on about and knows maybe what the problem MAY be then something would be nice but if not dont waste your valuable time stuffing around im sure you all have better things to do. Sorry to all those that cant understand what im writting about which is probalby most of you all, i cant explain things very well.. sorry, and thanks again for helping out
Dean Harding
Dean Harding
I''m guessing you have something like this:

File 1.cpp:
static HWND hwnd;
...
hwnd = CreateWindow(...);


File 2.cpp:
static HWND hwnd;
...
MessageBox( hwnd, ... );


That would compile, but it would work as you desribe (i.e. it wouldn''t work)

This is how you do it:

File 1.cpp:
HWND hwnd;
...
hwnd = CreateWindow( ... );


File 2.cpp:
extern HWND hwnd;
...
MessageBox( hwnd, ... );


Note the extern''s. extern means "declared in another file".

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

sethiroth
sethiroth
//<----- FILE.CPP----->

#include <windows.h>


HWND hWnd;
HINSTANCE hInstance;

void TestHWND();
int CreateMyWindow();


LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam)
{

switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);

return(0);
}
break;
default:break;
}

return DefWindowProc(hwnd, msg, wparam, lparam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
CreateMyWindow();
TestHWND();

return 0;
}


int CreateMyWindow()
{

WNDCLASSEX wc;
MSG msg;


wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
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(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = Window_Class_Name;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);


if (!RegisterClassEx(&wc))
return(0);


if (!(hWnd = CreateWindowEx(NULL,Window_Class_Name,
"Regale Ent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 400, 400, NULL, NULL, hInstance,NULL)))

return(0);

TestHWND(); //This test works fine


while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


return(msg.wParam);
}
--------------------------------------

//<----- FILE2.CPP----->
#include <windows.h>


extern HWND hWnd;

void TestHWND()
{
MessageBox(hWnd, "Its working", "Success", MB_OK);
}

--------------------------------------



ok thats my code to it... i have a TestHWND() function in my CreateMyWindow function just after i have created the window to show it works there but it wont work from anywhere else in my program what am i doing wrong, prolly something i should know already but just slipped my mind or something

[edited by - Sethiroth on August 5, 2002 3:29:17 AM]

[edited by - Sethiroth on August 5, 2002 3:30:29 AM]
Dean Harding
Dean Harding
Your problem is this:

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}


This is an (almost) infinite loop, which only ends when you close/destroy the window. That means, for example, that the TestHWND() in your WinMain won''t get called until that loop ends, which only happens after the window has been destroyed.

Move that section of code to after the call to TestHWND in your WinMain function and it''ll work.

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

sethiroth
sethiroth
ahhh yes, i thank you greatly Dean Harding, much help, thanks heaps. keep up the good work

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.