python 調用win32 api

剛學python,前幾天在java中調用了win32api,給eclipse窗口來了個抖動,也想拿python實現下。 java

在網上找python調用win32api的資料,清一色的是win32api模塊,我暈。 python

做爲一個新手,我也知道python調用c仍是很方便的,我也不想去sourceforge上下載模塊安裝,因而翻遍了google, api

加上本身的「冷靜思考,理性分析」 哈哈!!終於經過兩種方法實現 eclipse

摘錄在此,以備本人以及其餘python新人查閱
python2.7

(個人環境是 python2.7) 函數


這裏咱們實現的是:枚舉全部窗口,輸出窗口的titile google


第一種,經過ctypes的windll,user32實現。 spa


#coding=utf-8
from ctypes import *
from ctypes.wintypes import BOOL, HWND, LPARAM

#定義回調函數
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def print_title(hwnd,extra):
    title = create_string_buffer(1024)
	#根據句柄得到窗口標題
    windll.user32.GetWindowTextA(hwnd,title,255)
    title = title.value
    if title!="":
        print title
    return 1

#枚舉窗口
windll.user32.EnumWindows(print_title,0)

第二種方法,直接載入 user.dll code

#coding=utf-8
from ctypes import *
from ctypes.wintypes import BOOL, HWND, LPARAM

#加載user32.dll
user32 = windll.LoadLibrary("user32")

#定義回調函數
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def print_title(hwnd,extra):
    title = create_string_buffer(1024)
	#根據句柄得到窗口標題
    user32.GetWindowTextA(hwnd,title,255)
    title = title.value
    if title!="":
        print title
    return 1
	
#枚舉窗口
user32.EnumWindows(print_title,0)
相關文章
相關標籤/搜索