本文介绍了如何指定使用ctypes MessageBoxW单击“是/否”时实际发生的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def addnewunit(title, text, style):
ctypes.windll.user32.MessageBoxW(0, text, title, style)
我见过很多人显示此代码,但是没有人指定如何实际执行是/否工作。它们是按钮,它们在那里,但是如何指定当您单击或时的实际情况?
Ive seen a lot of people show this code, however nobody has ever specified how to actually make the Yes/No work. Theyre buttons, and they are there, however how does one specify what actually happens when you click either or?
推荐答案
类似这样的东西使用正确的ctype包装:
Something like this with proper ctypes wrapping:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ctypes
from ctypes.wintypes import HWND, LPWSTR, UINT
_user32 = ctypes.WinDLL('user32', use_last_error=True)
_MessageBoxW = _user32.MessageBoxW
_MessageBoxW.restype = UINT # default return type is c_int, this is not required
_MessageBoxW.argtypes = (HWND, LPWSTR, LPWSTR, UINT)
MB_OK = 0
MB_OKCANCEL = 1
MB_YESNOCANCEL = 3
MB_YESNO = 4
IDOK = 0
IDCANCEL = 2
IDABORT = 3
IDYES = 6
IDNO = 7
def MessageBoxW(hwnd, text, caption, utype):
result = _MessageBoxW(hwnd, text, caption, utype)
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return result
def main():
try:
result = MessageBoxW(None, "text", "caption", MB_YESNOCANCEL)
if result == IDYES:
print("user pressed ok")
elif result == IDNO:
print("user pressed no")
elif result == IDCANCEL:
print("user pressed cancel")
else:
print("unknown return code")
except WindowsError as win_err:
print("An error occurred:\n{}".format(win_err))
if __name__ == "__main__":
main()
请参见,以获取utype参数的各种值。
See the documentation for MessageBox for the various value of the utype argument.
这篇关于如何指定使用ctypes MessageBoxW单击“是/否”时实际发生的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!