7

Context:

I have a small script that alerts the user of an event by creating a message box using the Windows in built message box (Ref: MSDN MessageBox) which is imported using ctypes. This script is for Windows OS.

Problem:

Currently, the message box will appear on top of all other windows, but because it's such a small window, a user can easily click onto another window which could hide the message box.

What I want

I want to keep the message box always on top of other windows. If this can't be done, then alternatively is there a way to increase the dimensions of the message box?

Example Code:

import ctypes

ctypes.windll.user32.MessageBoxW(0, text, title, 0x00010000)
Kotosif
  • 338
  • 3
  • 12

1 Answers1

10
import ctypes

text = 'Using MB_SYSTEMMODAL'
title = 'Some Title'

ctypes.windll.user32.MessageBoxW(0, text, title, 0x1000)

MB_SYSTEMMODAL (0x1000) has the WS_EX_TOPMOST (0x40000) style.

The MessageBoxEx function seems to work good with using just the WS_EX_TOPMOST (0x40000) style.

import ctypes

text = 'Using WS_EX_TOPMOST'
title = 'Some Title'

ctypes.windll.user32.MessageBoxExW(0, text, title, 0x40000)

The MessageBox function has no parameters to change size. Perhaps an alternative like tkinter or another gui toolkit might be able to change messagebox size (though it maybe not if it just a wrapper for MessageBoxW) or you could perhaps create a custom window to use.

See MSDN MessageBox function for values to use.

See also MSDN MessageBoxEx function.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • 1
    `MB_SYSTEMMODAL` and `WS_EX_TOPMOST` will both create a message box on top of all windows, but it will not **stay** on top, which is the problem. A user can easily click away. – Kotosif Apr 29 '18 at 15:43
  • 2
    If you use the HWND parameter, then the messagebox will become a child using the parent window HWND which can help to prevent interaction with the parent while the messagebox shows. If not a parent window avaliable then usually WS_EX_TOPMOST is used. I am not sure how you can hide a top most messagebox. I am not sure of what "easily click away" accurately means. – michael_heath Apr 29 '18 at 16:02
  • My mistake, it looks like `WS_EX_TOPMOST` does work for me after all. It seems I had the wrong number of 0's in my last parameter all this time! – Kotosif Apr 29 '18 at 16:32