Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have Win32 MessageBox appear over other programs

I've recently started learning Python and wrote a little script that informs me when a certain website changes content. I then added it as a scheduled task to Windows so it can run every 10 minutes. I'd like to be informed of the website changing right away so I added a win32ui MessageBox that pops up if the script detects that the website has changed. Here's the little code snippet I'm using for the MessageBox (imaginative text, I know):

win32ui.MessageBox("The website has changed.", "Website Change", 0)

My issue is this, I spend most of my time using remote desktop so when the MessageBox does pop up it sits behind the remote desktop session, is there any way to force the MessageBox to appear on top of it?

On a similar note when the script runs the command line opens up very briefly over the remote desktop session which I don't want, is there any way of stopping this behaviour?

I'm happy with Windows specific solutions as I'm aware it might mean dealing with the windowing manager or possibly an alternative way to inform me rather than using a MessageBox.

like image 835
Peanut Avatar asked Dec 21 '22 18:12

Peanut


2 Answers

When you start anything from Task Scheduler, Windows blocks any "easy" ways to bring your windows or dialogs to top.

  1. First way - use MB_SYSTEMMODAL (4096 value) flag. In my experience, it makes Msg dialog "Always on top".

    win32ui.MessageBox("The website has changed.", "Website Change", MB_SYSTEMMODAL)
    
  2. Second way - try to bring your console/window/dialog to the front with Following calls. Of course, if you use MessageBox you must do that (for your own created window) before calling MessageBox.

    SetForegroundWindow(Wnd);
    BringWindowToTop(Wnd);
    SetForegroundWindow(Wnd);
    

As for flickering of the console window, you may try to start Python in a hidden state. For example, use ConEmu, ‘HidCon’ or cmdow. Refer to their parameters, something like:

ConEmu -basic -MinTSA -cmd C:\Python27\python.exe C:\pythonScript.py
    or
CMDOW /RUN /MIN C:\Python27\python.exe C:\pythonScript.py
like image 110
Maximus Avatar answered Dec 23 '22 06:12

Maximus


Avoiding the command window flash is done by naming the script with a pyw extension instead of simply py. You might also use pythonw.exe instead of python.exe, it really depends on your requirements.

See http://onlamp.com/pub/a/python/excerpts/chpt20/index.html?page=2

like image 21
dash-tom-bang Avatar answered Dec 23 '22 08:12

dash-tom-bang