CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Dialog Window Property Question?

  1. #1
    Join Date
    Mar 2008
    Posts
    21

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: Dialog Window Property Question?

    GetForegroundWindow - ?

    From what tool/application are you going to determine which dialog window is on top?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2008
    Posts
    21

    Re: Dialog Window Property Question?

    Hi Victor,

    Thanks for helping.

    I am using Visual Studio 2008.

  4. #4
    Join Date
    Mar 2008
    Posts
    21

    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?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: Dialog Window Property Question?

    Victor Nijegorodov

  6. #6
    Join Date
    Mar 2008
    Posts
    21

    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?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: Dialog Window Property Question?

    Quote Originally Posted by tboygee View Post
    Thanks, yes, I looked into GetForegroundWindow. It works. It gives me a handle of type CWnd*.
    No. It gives you the HWND .
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2008
    Posts
    21

    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?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,118

    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++17 Compiler: Microsoft VS2019 (16.6.1)

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,118

    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++17 Compiler: Microsoft VS2019 (16.6.1)

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,280

    Re: Dialog Window Property Question?

    Quote Originally Posted by tboygee View Post
    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.

  13. #13
    Join Date
    Mar 2008
    Posts
    21

    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?

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,118

    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++17 Compiler: Microsoft VS2019 (16.6.1)

  15. #15
    Join Date
    Mar 2008
    Posts
    21

    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?

Page 1 of 2 12 LastLast

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)