4

My aim is to get the handle of a window I click. I'm using a global mouse hook and then GetCursorPos and WindowFromPoint). When I click on a button (or other controls) I get the button's handle. Using GetParent I can find the parent window, so I need to check if the handle I have is a window or not.

I have tried GetClassName but it's not what I'm looking for.

Any ideas appreciated (also using other approaches).

1 Answers1

3
if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) {
    // window is a child window
} else {
    // window is a top-level window
}
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • SetParent helps build a window without WS_CHILD but not top-level. (As logical complement to http://stackoverflow.com/questions/16973995/whats-the-best-way-do-determine-if-an-hwnd-represents-a-top-level-window/16975012#16975012)... – kero Mar 09 '14 at 06:15
  • Still harping on about the mysterious child-but-not-child windows I see? :) – Jonathan Potter Mar 09 '14 at 12:46
  • 1
    Cool solution ;) However to get the handle of the parent window I have also discovered this: `while(GetParent(Hwnd)!=0){ Hwnd = GetParent(Hwnd); }` –  Mar 09 '14 at 14:30
  • @Jonathan, I hope you liked this :-) – kero Mar 09 '14 at 21:29
  • @Scanzy, GetParent = hwndParent or hwndOwner or 0 (i.e. it may be top-level owned window without children). – kero Mar 09 '14 at 21:31
  • @kero what do you mean? –  Mar 09 '14 at 21:37
  • @kero got it ;) you're right but (I haven't said it) in case of popup window I wanted to get the owner's handle :p –  Mar 09 '14 at 21:44
  • how could this work if GetWindowLong returns an IntPtr not a boolean? neither does WS_CHILD – Leo Gurdian Feb 28 '21 at 05:13