[win32] - how can i get the program HWND?

the window\form is created. now imagine that we don't know the HWND.
we want create a button on that window. so heres the question: how can i get the HWND program?
(i said program, because that window\form is the main window)
Last edited on
The HWND of main window you already have; it is what CreateWindow returns. You store that and you are done.


However, if for some reason you don't do that, use code like this to retrieve it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HWND hwndMain;

BOOL CALLBACK EnumWindowProc(HWND hwnd, LPARAM lParam)
{
    HINSTANCE hinst=(HINSTANCE)GetModuleHandle(NULL);

    if((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE)==hinst &&
        IsWindowVisible(hwnd))
    {
        hwndMain=hwnd;
        return FALSE;
    }
    else
        return TRUE;
}


Then call it like this:
1
2
hwndMain=NULL;
EnumWindows(EnumWindowProc, 0);




sorry, but i didn't get the HWND correct :(
the button isn't created :(
Last edited on
Then call GetLastError afterwards and see why it fails.
better see my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case WM_CREATE:

            hwndMain=NULL;
            EnumWindows(EnumWindowProc, 0);
            
            //create the button
            hButton=CreateWindowEx (WS_EX_TRANSPARENT ,TEXT("button"), TEXT(""),
            WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | WS_TABSTOP | BS_NOTIFY,
            100, 100,100, 50,hwndMain,NULL,hInstance, 0) ;

            hButton2=CreateWindowEx (WS_EX_TRANSPARENT ,TEXT("button"), TEXT("hello"),
            WS_CHILD | WS_VISIBLE | BS_TEXT | WS_TABSTOP | BS_NOTIFY,
            10, 10,100, 50,hwnd,NULL,hInstance, 0) ;

            altkey=GettingAltKey(caption);
            RegisterHotKey( hwnd, Hotkey_Message, MOD_ALT, altkey );
            SendMessage(hwnd, WM_UPDATEUISTATE, MAKEWPARAM(UIS_INITIALIZE, 0), 0);
            return 0;
Last edited on
So you create a transparent button and expect it to "see" it ? :)
the getlasterror, give 0(zero)
Yes, the button is created, but you can't see it because you used WS_EX_TRANSPARENT.

The HWND is stored in hButton and hButton2, use that if you want to.
the button can be created, but, i belive, in other HWND instead the mains window :(
that style is for make it transparent nothing more
Every window has it's own HWND, of course.


What you really want to do ? As your original question is already answered.
i'm building control class's. in construtor i must tell the parent(where the control will be). so, instead tell the parent, i want, by defauld, the parent be the main window
You already have the main windows HWND. It is called 'hwnd' in your case (the same thing you passed to CreateWindowEx 10-th argument in code you posted).


If is not clear, post your whole code (where you have created the 'main window' ). It is also returned by CreateWindow (or maybe this is a dialog application ?)
more 1 thing for i resolve in a diferent way ;)
but i will do it ;)
thanks for all
Topic archived. No new replies allowed.