Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the main window's handle in Python?

In python, I enumerate top-level windows through EnumWindows, and also I enumerate the processes through EnumProcesses.

Then in the python script, I put all the window handles which belongs to the same pid into one list (I did this through GetWindowThreadProcessId).

Later I found out something: there are 3 window handles which belong to notepad.exe, but I only open one text file.

Why?

Besides, I tried to set the text window as the foreground window through SetForegroundWindow, I passed the three window handles to this function, and two work.

How could this be ?

like image 660
Alcott Avatar asked Aug 16 '11 09:08

Alcott


2 Answers

Processes sometimes create invisible windows for their own purposes. You should ignore them (use IsWindowVisible function).

like image 154
hamstergene Avatar answered Oct 27 '22 00:10

hamstergene


To investigate this kind of things your best friend is Spy++, that comes with several versions of Visual Studio, if you can get it.

According to it, notepad.exe creates three top-level windows:

  1. The visible main window, class name "Notepad", overlapped.
  2. A hidden, disabled, pop-up window, class name "MSCTFIME UI", caption "M".
  3. Another hidden, disabled, pop-up window, class name "IME", caption "Default IME".

The two hidden windows are used internally by notepad to implement the IME (Input Method Editor), the GUI to type complex scripts.

Many programs create top-level hidden windows for a lot of things. For what you intend, you can ignore them all and use only the visible ones.

like image 42
rodrigo Avatar answered Oct 27 '22 01:10

rodrigo