-
January 21st, 2015, 08:05 AM
#1
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
-
January 21st, 2015, 08:59 AM
#2
Re: Dialog Window Property Question?
GetForegroundWindow - ?
From what tool/application are you going to determine which dialog window is on top?
Victor Nijegorodov
-
January 21st, 2015, 09:14 AM
#3
Re: Dialog Window Property Question?
Hi Victor,
Thanks for helping.
I am using Visual Studio 2008.
-
January 21st, 2015, 10:11 AM
#4
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?
-
January 21st, 2015, 10:14 AM
#5
Re: Dialog Window Property Question?
Victor Nijegorodov
-
January 21st, 2015, 12:11 PM
#6
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?
-
January 21st, 2015, 12:15 PM
#7
Re: Dialog Window Property Question?
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 .
Victor Nijegorodov
-
January 21st, 2015, 01:00 PM
#8
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?
-
January 21st, 2015, 01:07 PM
#9
Re: Dialog Window Property Question?
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.
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 21st, 2015, 01:10 PM
#10
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();
Last edited by VictorN; January 21st, 2015 at 01:14 PM.
Reason: PS was added
Victor Nijegorodov
-
January 21st, 2015, 01:10 PM
#11
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 21st, 2015, 04:45 PM
#12
Re: Dialog Window Property Question?
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.
-
January 22nd, 2015, 07:07 AM
#13
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?
-
January 22nd, 2015, 07:51 AM
#14
Re: Dialog Window Property Question?
Have you done any WIN32/MFC windows programming before?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 22nd, 2015, 11:41 AM
#15
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?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|