Search code examples
pythonpython-2.7messageboxright-clickctype

Is it possible to disable the right click and close all on messagebox ctype python?


I have a piece of code showing a messagebox implemented using ctype lib in python. My question is there a way to cancel the close all or right click when creating multiple ctype messagebox?

def msgbox(self,hwnd,msg,thid,pid):
        MB_OK = 0x0
        MB_OKCXL = 0x01
        MB_YESNOCXL = 0x03
        MB_YESNO = 0x04
        MB_HELP = 0x4000
        ICON_EXLAIM=0x30
        ICON_INFO = 0x40
        ICON_STOP = 0x10
        MB_TOPMOST=0x40000
        MB_SYSTEMMODAL=0x1000
        """
                HEX VALUE LINK
        https://www.autoitscript.com/autoit3/docs/functions/MsgBox.htm
        """
        writeLogs = WriteLogs(
                    pathLog = app_config['path_logs'] +"\\"+strftime("%Y_%m_%d")+".log",
                    timedate = time.strftime("%m/%d/%Y %I:%M:%S %p")
                    )
        writeLogs.appendLogA(msg)
        ctypes.windll.user32.MessageBoxA(hwnd, msg+str(operatorMessage), "[Error]", MB_OK | ICON_STOP | MB_SYSTEMMODAL)

enter image description here


Solution

  • You can send win32con.WM_CLOSE to close a message box:

    import win32con
    ctypes.windll.user32.PostMessageA(hwnd, win32con.WM_CLOSE, 0, 0)
    

    To disable the right click on taskbar you can attach you messagebox to a window with no icon so it will be invisible on taskbar (No right click).

    import ctypes
    import win32con
    import win32gui
    
    
    style = win32con.MB_OK
    
    
    wc = win32gui.WNDCLASS()
    wc.lpszClassName = "TaskbarDemo"
    rclass = win32gui.RegisterClass(wc)
    
    hwnd = win32gui.CreateWindow(rclass, "Taskbar Demo", style, \
                    0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
                    0, 0, None, None)
    
    
    MessageBox = ctypes.windll.user32.MessageBoxA
    MessageBox(hwnd, 'Message', 'Window title', 0)
    

    EDIT:

    def main(name):
        try:
           ...        
           wc.lpszClassName = name
           ...
    

    Call it with:

    main("TaskbarDemo")
    main("zefaz")
    ...
    

    and so on...