Go to Python Win32API operation

Source: Internet
Author: User

A few days ago, because of the need, with Python wrote a small plug-in, through the WIN32API Access to External programs window and do a little work.

Because the original is not familiar with Win32API so had to call for help. There's a qq:32034767 in the group [email protected]

This guy's good, he's hot.

Choose the right tool before you start. (since it is based on Win32API of course to use Microsoft's dedicated Spyxx.exe, to get started is quite simple.) More professional, can find out all the window handles and types on the current desktop. VC's that green version of Spy + + effect is not strong (Au3Info.exe is the same), can not track multiple sub-windows. Here is the simple code: (in fact, the hook principle can be found on the network)

Import Win32ui,win32con,pyhook,pythoncom,win32gui, WX

def hookhandle (event):

If event. Keyid==9: #tab键值

Try

Pwin=win32ui. FindWindow (' AfxMDIFrame70 ', None) #主窗口 AfxMDIFrame70 is the window class name you found with #spyxx.exe.

Pwin1=win32ui. FindWindowEx (Pwin,none, ' mdiclient ', None)

Pwin2=win32ui. FindWindowEx (Pwin1,none, ' AfxFrameOrView70 ', None)

Pwin3=win32ui. FindWindowEx (Pwin2,none, "AfxOleControl70", None)

Pwin4=win32ui. FindWindowEx (Pwin3,none,none, "Patientcardcontrol Frame") #可以根据窗口 # title name find Title name general add-ins are immutable and class names sometimes change, such as machine restart shutdown

Pwin5=win32ui. FindWindowEx (Pwin4,none, "AfxMDIFrame70", None)

Pwin6=win32ui. FindWindowEx (Pwin5,none, "AfxWnd70", None)

Pwin7=win32ui. FindWindowEx (Pwin6,none, "#32770", None)

Textbox=pwin7. GetDlgItem (222) #获取控件的ID

Textbox2=pwin7. GetDlgItem (224)

buf= ' 0x0 ' *1024

Buf2= ' 0x0 ' *1024

Textbox. SendMessage (Win32con. Wm_settext, "") #先清空控件内容

TextBox2. SendMessage (Win32con. Wm_settext, "")

Oldlen=textbox. SendMessage (Win32con. WM_GETTEXT,BUF)

Oldlen2=textbox2. SendMessage (Win32con. WM_GETTEXT,BUF2)

Textbox. SendMessage (Win32con. Wm_settext,buf[0:oldlen]+str (BLH)) #发送消息 Note that you cannot replace the # #用%s so you typically need global variables to replace

TextBox2. SendMessage (Win32con. WM_SETTEXT,BUF2[0:OLDLEN]+STR (name))

Except

Wx. LogMessage (' No Windows found available! Please make sure the program is running ')

HM = Pyhook.hookmanager () #初始实例

Hm. Keydown=hookhandle

Hm. Hookkeyboard ()

Pythoncom. Pumpmessages #据说是winctype的新功能 Delay It's not clear exactly what it is.

Hm. Unhookkeyboard () #释放键盘捕捉 as if the effect is not obvious

#for i in range (0,40):

#win32api. Sleep (20)

#if Win32ui. Pumpwaitingmessages (0,-1):

Win32API. PostQuitMessage (0) #

Exit monitoring messages It is critical that you use the effect # with Sys.setrecursionlimit (4000) #才明显 Otherwise the program cannot exit.

#raise exceptions. Systemexit

def OnClose (self, event):

Sys.setrecursionlimit (4000) #相当关键

Self. Close (True)

Self. Destroy ()

Go to Python Win32API operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.