Dialog Window Property Question?
Hi,
I'm new to Visual Studio and have been tasked with taking over someone else's C++ code. The Win32 application in question has several complicated dialogs open simultaneously. What is the best way to determine which dialog window is on top, i.e. the window that has been clicked on (anywhere in the window) most recently? Example code would be very helpful. Thanks in advance, cheers!
Tim
Re: Dialog Window Property Question?
GetForegroundWindow - ?
From what tool/application are you going to determine which dialog window is on top?
Re: Dialog Window Property Question?
Hi Victor,
Thanks for helping.
I am using Visual Studio 2008.
Re: Dialog Window Property Question?
Ok, I looked into GetForegroundWindow. It returns a type CWnd*. How do I get a CWnd* type handle for each of my 3 open windows in order to compare and find out which one is the one on top? I found GetSafeHwnd but it returns HWND type so cannot use. Do you have an example of how to get the handles do a comparison?
Re: Dialog Window Property Question?
Re: Dialog Window Property Question?
Thanks, yes, I looked into GetForegroundWindow. It works. It gives me a handle of type CWnd*. So now my question is how do I use it? I need to know the CWnd* type handles of my open windows so I can compare to know which one is on top but I can't find a way to do that. Can you provide an example?
Re: Dialog Window Property Question?
Quote:
Originally Posted by
tboygee
Thanks, yes, I looked into GetForegroundWindow. It works. It gives me a handle of type CWnd*.
No. It gives you the HWND . :cool:
Re: Dialog Window Property Question?
This compiles successfully for me:
CWnd *CWnd_hdl;
CWnd_hdl= GetForegroundWindow();
This gives "error C2440: '=' : cannot convert from 'CWnd *' to 'HWND *'"
HWND *HWND_hdl;
HWND_hdl = GetForegroundWindow();
I guess we have different libraries?
In any case, how do I get handles of my other open windows? Can you provide an example?
Re: Dialog Window Property Question?
Quote:
to know which one is on top
There is only one foreground window. The HWND handle returned from GetForegroundWindow() is the handle to the current foreground window.
Quote:
I looked into GetForegroundWindow. It works. It gives me a handle of type CWnd*
This is the MFC version from https://msdn.microsoft.com/en-us/library/b52w40kc.aspx This is not the WIN32 version referenced in post #5.
You stated in post #1 that this is a WIN32 application - but does it use the WIN32 APIs or does the application use the MFC framework?
Re: Dialog Window Property Question?
Please, read the documentation carefully!
The GetForegroundWindow() Win32 API function returns HWND.
Note: HWND. Neither HWND*, nor CWnd *.
PS: if your application is an MFC one then either use
Code:
HWND HWND_hdl = ::GetForegroundWindow();
or
Code:
CWnd* pWnd = GetForegroundWindow();
HWND HWND_hdl = pWnd->GetSafeHwnd();
Re: Dialog Window Property Question?
Code:
HWND HWND_hdl = GetForegroundWindow();
GetForegroundWindow() for WIN32 doesn't return a pointer like the MFC version - it returns a value.
Re: Dialog Window Property Question?
Quote:
Originally Posted by
tboygee
The Win32 application in question has several complicated dialogs open simultaneously. What is the best way to determine which dialog window is on top, i.e. the window that has been clicked on (anywhere in the window) most recently?
I don't mean to sound flip, but why would you care?
I say this because unless you are trying to automate the app, having to determine which window is on top could be an indication of a design flaw.
Re: Dialog Window Property Question?
Sorry for the confusion. Like I said earlier, I'm new to Visual Studio. My background is as a C programmer using LabWindows/CVI. I didn't write this code, I was given the assignment to modify it. Since only the CWnd version of GetForegroundWindow() compiles for me, I guess that means I have an MFC app.
That being the case, my earlier question stands. How do I use the obtained handle? I need to know the CWnd* type handles of my open windows so I can somehow compare to know which one is on top but I can't find a way to do that. Can you provide an example?
Re: Dialog Window Property Question?
Have you done any WIN32/MFC windows programming before?
Re: Dialog Window Property Question?
Wow.
Please, is there anyone willing to help me without judging my programming skills or motivation for daring to ask a question in the first place?
You've help a bunch Victor, I just need the rest of the puzzle - how do I use the newly obtained handle? I can't find a way to obtain handles of my other windows do a comparison. Can you give a quick example?
Re: Dialog Window Property Question?
Quote:
Originally Posted by
tboygee
Wow.
Please, is there anyone willing to help me without judging my programming skills or motivation for daring to ask a question in the first place?
You've help a bunch Victor, I just need the rest of the puzzle - how do I use the newly obtained handle? I can't find a way to obtain handles of my other windows do a comparison. Can you give a quick example?
The piece you seem to be missing here is the difference between a HWND and a CWnd or CWnd*. Windows keep track of its resources, so a HWND is the identifier that you would would supply or get from Windows to know which resource you're referring to.
Visual Studio comes with MFC which is a class library that encapsulates a lot of the Windows API. If you see a CWnd or a pointer to one, it's an MFC object that represents a window. CWnd has a HWND operator, so you can pass a CWnd object to any function that's expecting an HWND, or you can use GetSafeHwnd() or m_hWnd if you need the Windows handle.
Re: Dialog Window Property Question?
Quote:
Originally Posted by
tboygee
Wow.
Please, is there anyone willing to help me without judging my programming skills or motivation for daring to ask a question in the first place?
It isn't about questioning your skills - it's about trying to help you solve the problem. If we understand what you are attempting to do in more detail, we might be able to help you solve it in a simple manner (without relying on something like GetForegroundWindow).
Re: Dialog Window Property Question?
Quote:
Originally Posted by
tboygee
Wow.
Please, is there anyone willing to help me without judging my programming skills or motivation for daring to ask a question in the first place?
As already been said, the question was about understanding the best way to help you. Now, consider this your question:
Quote:
Originally Posted by
tboygee
I need to know the CWnd* type handles of my open windows so I can somehow compare to know which one is on top but I can't find a way to do that.
There is no such a thing like "CWnd* type handle". And dialog window handle has the same HWND type as the regular window handle does. You cannot distinguish your windows by handle types.
In case you need to distinguish your windows, you can do that by window text for example (GetWindowText() is the way to get that). The window text is what you can see in its caption.
In case you need to distinguish your windows by class (I mean window class, not C++ class), you can GetClassName(). The standard dialog window class name is "#32770".
You can inspect your window properties by means of Spy++ tool and choose one property, or combination of those, to rely on.
Re: Dialog Window Property Question?
Thank you very much CGDEF and Igor. The insight you provided has allowed me to solve my problem. :)
Re: Dialog Window Property Question?
Quote:
Originally Posted by
2kaud
[...]
GetForegroundWindow() for WIN32 doesn't return a pointer like the MFC version - it returns a value.
Indeed, Windows API GetForegroundWindow function does not return a pointer to an MFC object (as CWnd::GetForegroundWindow does) but returns a window handle (HWND) which IS A POINTER, as well.
HWND may be defined as void* or as a pointer to a structure. The second is currently used in actual Windows SDKs for STRICT Type Checking to detect at compile time, errors like for example passing a HBRUSH handle to a function that requires HWND.
See also: Visual C++: Why Watch window shows 'unused' for a HWND variable?