Link to home
Start Free TrialLog in
Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria

asked on

How to find the "handle" of a Microsoft Windows window?

Hello!
 
I am in trouble, as I have to make a bitmap image transparent in a GDI+ project. I confess that this is my first GDI+ project. I had never programmed any Windows applications before, an no other application for a game. Although the application I have to implement is not a game but a medical software, technically it is a simple 2D GDI+ game.

GENERAL:
The software has a core written in Microsoft Visual Basic, on which I do not have access. This core program enlists all the subprograms that can be started. The core part calls for each subprogramm DLLs that are all written in C++. All the windows are full-screen and have no border (with the standard minimize, maximaze & close buttons). The windows of the subprograms do appear on the task bar on the bottom of the screen, when you use Alt + Tab & select a running "normal" windows program or when you click on the "Start" button, like all other Windows applications. But when you click on these symbols, no window will appear. So, it seems to be an abnormal window... This may be the cause for my current problem.

----
Currently, I am working for a client of my company. As I am not experienced in this GDI subject, I am adapting an existing GDI project to the new functional specification.
 
Up to now, I have a background bitmap, where I place several ship bitmaps. But as the ship bitmaps are not transparent, the slightly changing water color of the background is not equal to the water color of the ship rectangle. So, I have to make the ship rectangle background transparent.

PROBLEM DESCRIPTION:
I searched the Internet & found a GDI Tutorial (oh, that's great!) at http://www.codeproject.com/KB/graphics/gditutorial.aspx?msg=3268035. The "GDI Tutorial 4" there would solve my problem, I guess..... But I have a problem when I try to get the windows handle (HWND) of this abnormal window. I desparately need the handle information of the current active window, as I need to request color and some other information of it.

I tried the following to get the HWND:

HWND hwnd = GetActiveWindow();
hwnd = GetForegroundWindow();
hwnd = GetTopWindow();

The Microsoft Visual Studio 2008 debugger shows that the HWND information is "unused". As I use this handle to get the HINSTANCE of the window and so on, it results soon in a crash, as some objects I want to get are uninitialized when I try it with an "unused" HWND.

My QUESTION (of a novice Windows and GDI+ programmer):
Are there windows under Windows that do not have any handle? If yes, how can I get the color information of such a window? Is there a way in GDI+ to get the handle from there?

Thank you for your help!
Avatar of js-profi
js-profi

the GetActiveWindow would retrieve the active window of the calling thread's message queue. that means your prog would need at least one window and a so-called message pump to get a valid return of that call.

you might call GetFocus() to get the window handle of the window which currently has the focus. By calling GetParent(...) you could retrieve the parent and grand parent windows until you got the top level window. you could iterate all top level windows by calling EnumWindows.
Avatar of Ahmet Ekrem SABAN

ASKER

OK, I will try soon. Interestingly, the hwnd in "GDI Tutorial 4", WinMain, mentioned above is also "unused". But a
ShowWindow(hwnd, 1);
immediately brings the window into the foreground. The same ShowWindow method does nothing in my project.

GDI-Tutorial-4--Unused-HWND.jpg
I tried the GetFocus() & got, again, an unused HWND. This would not be a problem, if I could get the resources with FindResource(hInstance, ..., ...). But it also returns an unused HRSRC is also unused (see below). :-(

VRAG--Unused-HWND--HINSTANCE-2-.jpg
waht do you mean by 'unused'? the hwnd is taht of another process and of course debugger cannot find it valid for your local environment.
ASKER CERTIFIED SOLUTION
Avatar of js-profi
js-profi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK. I will close this question, as what I need to no is beyond the HWND problem. Thank you for your correct information!
It turned out that my problem is with the resources. I learned from the answer that it is natural the that the HWND is "unknown" to the debuggers local environment.