2

My current situation is that I open a process, which opens in a random location (thats how this process works).

I have the process PID so I need to somehow focus this window, and move it to the center of my screen. Im doing something wrong as I can't even set focus on that window... tried it with different apps and got the same result...

The way I select the window -

appl = pywinauto.application.Application()               
appl.connect(process=824)
app_dialog = appl.top_window_()
app_dialog.Minimize()
app_dialog.Maximize()

##app_dialog.SetFocus() ##doesn't work aswell
##pywinauto.win32functions.SetForegroundWindow(app_dialog)## doesn't work

Thanks for reading :)

Ben
  • 793
  • 2
  • 13
  • 29

2 Answers2

0

Can't say why it doesn't work with pywinauto... Got it to work with win32gui as the answer here- Python Window Activation

long yet efficient ;)

Ben
  • 793
  • 2
  • 13
  • 29
  • 1
    Checked the code from my answer with WireShark and found that it works but `.set_focus()` really doesn't. Going to fix it in 0.6.3. Thanks for reporting the problem. – Vasily Ryabov Jun 21 '17 at 20:17
0

Method app_dialog.set_focus() should work in pywinauto 0.6.2. If not, it might be a bug. Is your application publicly available somehow? I'd like to reproduce it on my side. Are you trying to activate a background window while you have modal dialog on top of it?

Second case is a wrong usage of SetForegroundWindow(...). It should give a handle, but you pass WindowSpecification object app_dialog. Right way is the following:

handle = app_dialog.wrapper_object().handle
pywinauto.win32functions.SetForegroundWindow(handle)
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78