CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] [win32] - how get the main window inside a class? and some advices

    how can i get the main window without use the GetForegroundWindow()?
    what i need is, when i create the instance of label, is get the parent automatic(in these case the main window).

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

    Re: [win32] - how get the main window inside a class? and some advices

    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)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - how get the main window inside a class? and some advices

    Quote Originally Posted by 2kaud View Post
    i'm sorry, i mean before create the control

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

    Re: [win32] - how get the main window inside a class? and some advices

    Have a look at GetGuiThreadInfo(). It can return window info about the current thread.
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    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)

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: [win32] - how get the main window inside a class? and some advices

    I don't see how GetGuiThreadInfo may be better than GetForegroundWindow.

    The thing OP needs to understand is: there's no "main window" thing in Windows program. A program is allowed to have any number of threads owning any number of top level windows each. And only program developer knows for sure what window matters more than others.

    Bottom line is: control design that tries to make any assumptions about "mainness" of particular window is bad design.

    More than that, I really fail to understand how main window, whatever it is, may have anything to do with controls. Maybe we need to get a definition of main window here to understand the problem better.
    Last edited by Igor Vartanov; January 29th, 2014 at 01:39 PM.
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - how get the main window inside a class? and some advices

    Quote Originally Posted by Igor Vartanov View Post
    I don't see how GetGuiThreadInfo may be better than GetForegroundWindow.

    The thing OP need to understand is: there's no "main window" thing in Windows program. A program is allowed to have any number of threads owning any number of top level windows each. And only program developer knows for sure what window matters more than others.

    Bottom line is: control design that tries to make any assumptions about "mainness" of particular window is bad design.

    More than that, I really fail to understand what main window, whatever it is, may have anything to do with controls. Maybe we need to get a definition of main window here to understand the problem better.
    sorry: 'main window', like i have seen is the 1st window... but maybe you have right.
    when i execute the program, 1 window(form) is created. i call it the 'main window'. it's the child controls\windows parent. what i wanted is get the hwnd of the parent window, without use the GetForegroundWindow() or GetParent() or use the hwnd of the child control
    Last edited by Cambalinho; January 29th, 2014 at 01:50 PM.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [win32] - how get the main window inside a class? and some advices

    If the program creating the windows is your own (you're writing the source).

    Then the proper solution is to pass the window pointer/handle/reference, along from the thread that created that window along to the thread(s) that need it.

    If the program creating the window is not under your control...
    then "it's all just a big bunch of guesses and assumptions", and your guesses and assumptions are as good as anyone else's.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: [win32] - how get the main window inside a class? and some advices

    Quote Originally Posted by Cambalinho View Post
    what i wanted is get the hwnd of the parent window, without use the GetForegroundWindow() or GetParent() or use the hwnd of the child control
    The only reliable approach is: your control class design makes user to explicitly specify the parent window handle.

    Code:
    class MyCoolestStatic
    {
    private:
        HWND m_hwndParent;
    
    public:
        // constructor requires parent window handle
        MyCoolestStatic(HWND hwndParent): m_hwndParent(hwndParent) ...
    
        // no default constructor allowed
    . . .
    };
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - how get the main window inside a class? and some advices

    i see what you(both) mean. thanks for all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured