How can I determine if, given a window handle, the window handle is part of the Windows UI as opposed to an application?

Sidney

I'm writing a script (in C#, but almost everything is handled via pinvoke) that finds the window handle under the mouse pointer and allows the user to take some action on it (primarily custom-bounded-window-snapping related functionality).

I've got it to the point where it works really really well! It can move the desktop and taskbar. This is of course, something I don't actually want it to do. I only want it to work on windows the user has brought into being; things like Chrome, Word, Notepad, or Teams.

Is there some handy delineation I can look at? I've tried looking at window styles, or PID, but I don't see an obvious pattern on how to detect "this 'window' is owned by the Windows shell".

I realized -- I have checked to see if the window handle belongs to explorer.exe, but that doesn't work as it prevents the user from moving regular file browsers.

Sidney

After some digging, I found this question which references the "GetDesktopWindow()" and "GetShellWindow()" pinvoke functions. Using the GetShellWindow() api and it's pid, I was able to determine the process ID of the windows shell, and compare it to the process ID of the application I was currently moving. Finally, since file explorer windows are part of the explorer process, I checked to see if the window has "File Explorer" as it's title, or if any of it's parent windows do.

[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr GetShellWindow();

[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);

public bool IsPartOfWindowsUI
{
  get
  {
      var desktopwindow = new WindowRef(GetShellWindow());
      return (desktopwindow.ThreadProcessID == ThreadProcessID &&
             //Check to see if title of this window or it's parents are
             //Basic file explorer windows 
             !TitleTree.Contains("File Explorer"));
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can i handle an event to open a window in react js?

How can I open a console application with a given window size?

How to get a handle of a label in a window

How to handle window scroll efficiently

How to handle with popup window in Protractor

how to handle "Launch Application" pop up window in firefox using autoIT

Can I know if my window handle is requested by another process?

How to handle Json response and How Can I sperate value from this Json response in my terminal window

How can I handle File drag and drop with a window overlay and multiple drop zones?

Get window handle of SDL 2 application

Handle the closing of the workbench window in Java RCP application

How do I send a message to a window handle using AutoHotKey?

How can I determine when my application's console window gets or loses focus?

How to Handle closing of outlook Mail Window(Desktop App) window and switch back to web application in selenium using java

Qt5 ui, multiple windows: how can I access the Ui objects in Window 2 from Window 1

How can I show a window with the a name given as parameter in Windows in C++

Ways to handle window scroll

Python pyautogui window handle

Handle terminal window closing

How do I create a WPF application that can handle the URL:callto and URL:tel protocols in Windows 10?

How to handle window scroll event in Angular 4?

How to get the window handle for a VBA MsgBox?

How to handle popup window by using python automation

How to get handle of popup window (WebdriverIO)

How to handle, when tkinter window gets focus

pywinauto -how to handle alert window control

gWidgets how to handle invalid reference if window is closed

How to handle intermittent pop-up window

How should I determine which scene is going to be opened by UIKit so I can setup my UI in the proper window? (iOS 13+)

TOP Ranking

HotTag

Archive