13

Is it to check for the WS_CHILD bit:

    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    int isTopLevel = !(style & WS_CHILD);

Or is there a better way?

Assume: Straight C calling only existing Windows API code.

0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
  • 1
    Checking for the absence of the `WS_CHILD` style is the way to do it - any window that isn't a child is a top-level window. – Jonathan Potter Jun 06 '13 at 23:58
  • @Jonathan - however, there are top-level with WS_CHILD (child of DesktopWindow) – kero Jun 07 '13 at 01:56
  • All windows are children of the desktop in the window hierarchy, but only those with `WS_CHILD` set are actually child windows. – Jonathan Potter Jun 07 '13 at 02:16
  • Once again, if you do not understand: there are top-level with WS_CHILD. – kero Jun 07 '13 at 02:35
  • Conversely, the window can be not WS_CHILD but thus be not top-level – kero Jun 07 '13 at 10:30
  • Thanks for your comments, kero. That's why I asked the question -- it seems this rather simple seeming problem is quite tricky in practise! – 0xbe5077ed Jun 07 '13 at 20:33
  • Please provide an example of a "top-level with WS_CHILD" window, as that makes no sense to me. – Jonathan Potter Jun 08 '13 at 08:28
  • Please: for example, take any standard ComboBox (with CBS_DROPDOWNLIST or CBS_DROPDOWN) and test its dropdown list (ComboLBox). – kero Jun 08 '13 at 10:11
  • I'm unsure what I'm meant to look at - its dropdown list has WS_CHILD set, so it's a child window. What in your mind makes it a top-level window? – Jonathan Potter Jun 08 '13 at 10:24
  • Please concentrate and read MSDN's Remark to "EnumWindows": "The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style". – kero Jun 08 '13 at 10:44
  • @kero: that sounds like an edge case to me. Depending on what the programmer actually wants to know for, those edge cases might need to be treated as parent windows, as child windows, or it might not matter. – Harry Johnston Jun 09 '13 at 22:07
  • Why did you tag this `C++` if the question states "Assume: Straight C"? – Miroslav Policki Sep 15 '20 at 02:35

3 Answers3

17
  • Way #1: Test hWnd==GetAncestor(hWnd,GA_ROOT)

  • Way #2: Use IsTopLevelWindow (user32 Win7, undocumented)

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
kero
  • 678
  • 3
  • 10
1

The GetParent() API returns the parent or owner of the current window.

jwismar
  • 12,164
  • 3
  • 32
  • 44
  • 1
    The first line of the doc I linked to says "Retrieves a handle to the specified window's parent or owner." And talks about retrieving the owner window later in the article. – jwismar Jun 06 '13 at 23:47
  • @chris: The docs say: "If the window is a child window, the return value is a handle to the parent window. If the window is a top-level window with the WS_POPUP style, the return value is a handle to the owner window." – Remy Lebeau Jun 06 '13 at 23:47
  • @jwismar, I'm blind today. I read everything about `GetWindow` and `GetAncestor` and somehow missed the first remarks paragraph. – chris Jun 06 '13 at 23:48
  • 2
    @jwismar, GetParent is completely useless. – kero Jun 07 '13 at 10:23
  • 1
    @kero Can you explain why? – jwismar Jun 07 '13 at 16:25
  • @jwismar, here is my old demo just on this topic: http://files.rsdn.ru/42164/parentowner.zip – kero Jun 07 '13 at 19:50
0

Your method is possible (But I think you had better to check if it is a overlapped/popup window too). There is maybe another way -- That is to enum all top level windows and to check if your windows is included in result.

Renjie
  • 41
  • 2