python win32gui.findwindow_Python:win32gui.SetForegroundWindow

I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I cant get Python to click "Snapshot" button, as the new window is not in focus. So I am planning to use Win32gui module's win32gui.FindWindow and win32gui.SetForegroundWindow functionality. But it gives me error- invalid handle. My app name is "DMCap"

Here is code snippet in Python:

handle = win32gui.FindWindow(0, "DMCap") //paassing 0 as I dont know classname

win32gui.SetForegroundWindow(handle) //put the window in foreground

Can anyone help me? Is this Python code correct? Can I send handle directly like this?

解决方案

Your code should run just fine as-is, IF there is truly a window titled "DMCap." To get a list of handles and titles, run the code below:

import win32gui

def window_enum_handler(hwnd, resultList):

if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':

resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def get_app_list(handles=[]):

mlst=[]

win32gui.EnumWindows(window_enum_handler, handles)

for handle in handles:

mlst.append(handle)

return mlst

appwindows = get_app_list()

for i in appwindows:

print i

This will produce a list of tuples containing handle, title pairs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值