Retrieve HWND from HDC

hello forum.
i am currently porting an application from mac to windows. the app uses a library which creates a window and an opengl context on initialization. i can retrieve the opengl context (HGLRC) and drawable (HDC) from the library methods, but i also need to retrieve the HWND window handle associated with that drawable.

on the mac, it was a a few lines of code to achieve this from an existing drawable (HDC on win, AGLDrawable on mac):

OpaqueWindowPtr* myWindow = (OpaqueWindowPtr*)drawable;
ControlRef theContentView;
GetRootControl(myWindow, &theContentView);

the windows version of ControlRef (HWND) is what i’m trying to retrieve.

does anyone have any ideas how i can achieve this on windows?
many thanks for any tips.
-rob

On Windows platform, you can acquire device context (HDC) of a window (HWND) by calling GetDC WinAPI function, but AFAIK you can’t do this the other way around. Your library should have created window before getting a DC, by using CreateWindow or CreateWindowEx. Check your library documentation if it contains a function to retrieve its handle.

HWND WindowFromDC(HDC hdc) should do the trick.

Ah! That function I didn’t know it existed!

AHA! don’t know why i couldn’t find that function.
many thanks.

-rob

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.