Get HWND of each Window?
You mixed up ctypes
and win32gui
.
The hwnd
you've got is obtained via ctypes
and is a LP_c_long
object. That's why win32gui.MoveWindow
didn't accept it. You should pass it to
ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)
If you want to use win32gui.MoveWindow
, you can use python function as callback directly.
For example,
import win32gui
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
win32gui.MoveWindow(hwnd, 0, 0, 760, 500, True)
win32gui.EnumWindows(enumHandler, None)
How to get the hWnd of Window instance?
WindowInteropHelper
is your friend. It has a constructor that accepts a Window
parameter, and a Handle
property that returns its window handle.
Window window = Window.GetWindow(this);
var wih = new WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;
Get all window handles for a process
Pass IntPtr.Zero
as hWnd
to get every root window handle in the system.
You can then check the windows' owner process by calling GetWindowThreadProcessId
.
How to get handle for a specific application window in Python using pywin32?
You can use EnumWindows()
,this will search all the window,Read it in MSDN doc:
import win32gui
def getShell():
thelist = []
def findit(hwnd,ctx):
if win32gui.GetWindowText(hwnd) == "Windows PowerShell": # check the title
thelist.append(hwnd)
win32gui.EnumWindows(findit,None)
return thelist
b = getShell()
print(b) # b is the list of hwnd,contains those windows title is "Windows PowerShell"
pywin32: how to get window handle from process handle and vice versa
Enumerate windows and then get the process handle for each window
You need these APIs:
win32gui.EnumWindows()
to enumerate all top-level windows (that is no child windows aka controls)win32process.GetWindowThreadProcessId()
to get process ID from window handlewin32api.OpenProcess()
to get process handle from process ID
Enumerate processes and then get the main application window handle
for each process
You need these APIs:
win32process.EnumProcesses()
to enumerate all processeswin32api.GetWindowLong()
with argumentGWL_STYLE
to get window styles andGWL_EXSTYLE
to get extended window styleswin32gui.GetParent()
to determine unowned windows
By filtering the result of EnumWindows()
using GetWindowThreadProcessId()
you can get all windows that belong to a given process.
Determining the main window can be tricky as there is no single window style that would designate a window as the main window. After all, an application might have multiple main windows.
Best you could do is to use the same rules that the taskbar uses to determine application windows, because that's what the user perceives as main windows:
The Shell places a button on the taskbar whenever an application
creates an unowned window—that is, a window that does not have a
parent and that has the appropriate extended style bits.
To ensure that the window button is
placed on the taskbar, create an unowned window with the
WS_EX_APPWINDOW extended style. To prevent the window button from
being placed on the taskbar, create the unowned window with the
WS_EX_TOOLWINDOW extended style. As an alternative, you can create a
hidden window and make this hidden window the owner of your visible
window.
Use GetParent()
and GetWindowLong()
to determine the unowned windows that have the right window styles according to these rules.
Related Topics
How to Find Out Whether a File Is at Its 'Eof'
Importing Flask.Ext Raises Modulenotfounderror
Flask-Sqlalchemy Import/Context Issue
How to Form Tuple Column from Two Columns in Pandas
Restart Python-Script from Within Itself
Why Doesn't Print Work in a Lambda
Subsampling Every Nth Entry in a Numpy Array
Attributeerror: Module 'Cv2.Cv2' Has No Attribute 'Createlbphfacerecognizer'
How to Insert a Jpeg Image into a Python Tkinter Window
How to Copy Over an Excel Sheet to Another Workbook in Python
What's the Difference Between "Update" and "Update_Idletasks"
Saving the State of a Program to Allow It to Be Resumed
Python "Extend" for a Dictionary
Overriding "+=" in Python? (_Iadd_() Method)
How to Tell Distutils to Use Gcc
Why Is Adding Attributes to an Already Instantiated Object Allowed