Skip to content

Instantly share code, notes, and snippets.

@dataserver
Created August 15, 2023 12:30
Show Gist options
  • Save dataserver/5419b4482edbf59b3dba98533178f100 to your computer and use it in GitHub Desktop.
Save dataserver/5419b4482edbf59b3dba98533178f100 to your computer and use it in GitHub Desktop.
Simple windows modal dialog box using ctypes
import ctypes
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxa
# Displays a modal dialog box that contains a system icon, a set of buttons, and
# a brief application-specific message, such as status or error information. The
# message box returns an integer value that indicates which button the user clicked.
NULL = 0
# buttons
MB_ABORTRETRYIGNORE = 0x00000002
MB_CANCELTRYCONTINUE = 0x00000006
MB_HELP = 0x00004000
MB_OK = 0x00000000
MB_OKCANCEL = 0x00000001
MB_RETRYCANCEL = 0x00000005
MB_YESNO = 0x00000004
MB_YESNOCANCEL = 0x00000003
# icon
MB_ICONEXCLAMATION = 0x00000030
MB_ICONWARNING = 0x00000030
MB_ICONINFORMATION = 0x00000040
MB_ICONASTERISK = 0x00000040
MB_ICONQUESTION = 0x00000020
MB_ICONSTOP = 0x00000010
MB_ICONERROR = 0x00000010
MB_ICONHAND = 0x00000010
# return values
IDABORT = 3
IDCANCEL = 2
IDCONTINUE = 11
IDIGNORE = 5
IDNO = 7
IDOK = 1
IDRETRY = 4
IDTRYAGAIN = 10
IDYES = 6
def main():
title = "Modal Dialog Box Title"
message = "Message to be displayed."
result = ctypes.windll.user32.MessageBoxExW(None, message, title, MB_OKCANCEL | MB_ICONINFORMATION)
if result == IDOK:
print("OK clicked")
elif result == IDCANCEL:
print("CANCEL clicked")
else:
print(f"{result=}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment