python 调用win32 api

刚学python,前几天在java中调用了win32api,给eclipse窗口来了个抖动,也想拿python实现下。

在网上找python调用win32api的资料,清一色的是win32api模块,我晕。

作为一个新手,我也知道python调用c还是很方便的,我也不想去sourceforge上下载模块安装,于是翻遍了google,

加上自己的“冷静思考,理性分析” 哈哈!!终于通过两种方法实现

摘录在此,以备本人以及其他python新人查阅

(我的环境是 python2.7)


这里我们实现的是:枚举所有窗口,输出窗口的titile


第一种,通过ctypes的windll,user32实现。


#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

#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)

转载于:https://my.oschina.net/fants/blog/89682

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值