1.调用win32的api
task.py
from data import *
from code import *
class Win32_Api(object):
def __init__(self):
self.__KERNEL32=windll.kernel32
self.__USER32=windll.user32
self.__GDI32=windll.gdi32
def dump_dict(self, structure):
"""
传进来一个结构体,返回字典
目前只解析单一结构体和嵌套结构体
:param structure:
:return:
"""
info = {}
if '_fields_' in dir(structure):
for k, v in structure._fields_:
av = getattr(structure, k)
if type(v) == type(Structure):
av = self.dump_dict(av)
info[k] = av
return info
def number_msg(self,number,msg):
try:
data=msg["code"].value[str(number)]
return data
except:
...
#############################################################
def GetTimeZoneInformation(self):
"""
检索当前的时区参数。这些参数控制协调世界时(UTC)与本地时间之间的转换。
:return:
"""
GetTimeZoneInformation = self.__KERNEL32.GetTimeZoneInformation
t = TIME_ZONE_INFORMATION()
res = GetTimeZoneInformation(pointer(t))
print("res", res)
# 第一种取值方法
# print("Bias",t.Bias)
# print("StandardName",t.StandardName)
# data = {
# "wYear": t.StandardDate.wYear,
# "wMonth": t.StandardDate.wMonth,
# "wDayOfWeek": t.StandardDate.wDayOfWeek,
# "wDay": t.StandardDate.wDay,
# "wHour": t.StandardDate.wHour,
# "wMinute": t.StandardDate.wMinute,
# "wSecond": t.StandardDate.wSecond,
# "wMilliseconds": t.StandardDate.wMilliseconds
# }
# print("StandardDate",data)
# print("StandardBias",t.StandardBias)
# print("DaylightName",t.DaylightName)
# data={
# "wYear": t.DaylightDate.wYear,
# "wMonth": t.DaylightDate.wMonth,
# "wDayOfWeek": t.DaylightDate.wDayOfWeek,
# "wDay": t.DaylightDate.wDay,
# "wHour": t.DaylightDate.wHour,
# "wMinute": t.DaylightDate.wMinute,
# "wSecond": t.DaylightDate.wSecond,
# "wMilliseconds": t.DaylightDate.wMilliseconds
# }
# print("DaylightDate",data)
# print("DaylightBias",t.DaylightBias)
# 第二种取值方法(有对比,才有伤害,更能显出其他方法的便利)
data = self.dump_dict(t)
print("Data", data)
def AnyPopup(self):
"""
AnyPopup功能表示屏幕上是否存在拥有,
可见,顶级弹出窗口或重叠窗口。
该函数搜索整个Windows屏幕,而不仅仅是呼叫应用程序的客户端区域。
:return:
如果弹出窗口存在,则返回值不为零,即使弹出窗口被其他窗口完全覆盖。
如果弹出窗口不存在,返回值为零。
"""
AnyPopup=self.__USER32.AnyPopup
res=AnyPopup()
print("res",res)
def GetACP(self):
"""
:return:
"""
res = self.__KERNEL32.GetACP()
print("res:", res)
msg=self.number_msg(res,GetACP_code)
print(msg)
# 936
2.最好让结构体和程序分开
创建data.py
from ctypes import *
from ctypes.wintypes import *
class SYSTEMTIME(Structure):
"""
SYSTEMTIME结构表示使用个人成员进行月,日,工作日,小时,分钟,秒和毫秒的日期和时间
"""
_fields_ = [
("wYear", WORD),
("wMonth", WORD),
("wDayOfWeek", WORD),
("wDay", WORD),
("wHour", WORD),
("wMinute", WORD),
("wSecond", WORD),
("wMilliseconds", DWORD)
]
class TIME_ZONE_INFORMATION(Structure):
"""
TIME_ZONE_INFORMATION结构指定特定于时区的信息
"""
_fields_=[
("Bias",LONG),
("StandardName",WCHAR*32),
("StandardDate",SYSTEMTIME),
("StandardBias",LONG),
("DaylightName",WCHAR*32),
("DaylightDate",SYSTEMTIME),
("DaylightBias",LONG)
]
实现原理:你给他想要的数据结构,他把得到的数据填充到你给的数据结构当中,你再取值,就是这么简单
3.取完数据找到对应信息,创建code.py
from enum import Enum,unique
@unique
class GetACP_code(Enum):
code={
"874":"泰国",
"932":"日本",
"936":"中国人(中国,新加坡)",
"949":"朝鲜的",
"950":"中文(台湾,香港)",
"1200":"Unicode(ISO 10646的BMP)",
"1250":"Windows 3.1东欧",
"1251":"Windows 3.1西里尔文",
"1252":"Windows 3.1 Latin 1(美国,西欧)",
"1253":"Windows 3.1希腊语",
"1254":"Windows 3.1土耳其语",
"1255":"希伯来语",
"1256":"阿拉伯",
"1257":"波罗的海的"
}
4.创建main.py,代码这样看着就很简洁
from task import Win32_Api
def main():
cc = Win32_Api()
cc.GetTimeZoneInformation()
cc.AnyPopup()
cc.GetACP()
if __name__ == '__main__':
main()