Message Box to Foreground (TopMost)

Welcome Forums General PowerShell Q&A Message Box to Foreground (TopMost)

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 4 days ago.

  • Author
    Posts
  • #113009

    Participant
    Points: 0
    Rank: Member

    I cannot find a way to have my message box come to the front of the screen (on top of all other windows). I am launching the script as a Windows Scheduled Task via vbscript (the only way I could find to run the powershell script silently without an additional window coming up like cmd, powershell, etc). However, the message appears to come up BEHIND other windows. I cannot find a way to get the message to the front unless I don't use a message box and just create a custom form with a message and the "TopMost" command. Thoughts/suggestions?

    
    Add-Type -AssemblyName System.Windows.Forms
    
    $result = [System.Windows.Forms.MessageBox]::Show("MESSAGE", "TITLE", 'YesNo', 'Warning')
    
    If ($result -eq 'Yes'){DO SOMETHING}
    
    

    Thanks!

  • #113020

    Participant
    Points: 0
    Rank: Member

    I don't think that's possible.
    This code:

    $result = [System.Windows.Forms.MessageBox]::Show("MESSAGE", "TITLE", 'YesNo', 'Warning', "Button1", "RightAlign" )

    Sets the YES button as default and right-aligns the text.
    According to
    this article an option "ServiceNotification" should set the MessageBox on top, however it doesn't work (neither does the "DefaultDesktopOnly" option).
    I'd create a message box myself, sorry.

    • #113021

      Participant
      Points: 0
      Rank: Member

      Hold your horses: My .Net Framework is 4.7.03056, the article I linked to specifies 4.7.2 so maybe it'll work on that version of .Net Framework.

  • #113029

    Participant
    Points: 10
    Rank: Member

    By design, message box always open as the model window. So it will remain on top if your application is set to be the top most....

    Therefore your can try these..

    1) Just before popping up the Messagebox just make your application got focus and set it to be top most... Than open your message box..

    or ...

    2) simply make your own Message box Dialog form and set the form as top most and than pop up that custom message box....

    or ... call the original Win32 MessageBox API:

    [DllImport("user32.dll")]
    public static extern int MessageBox(int hWnd, String text, String caption, uint type);
    

    Once this is done you could include the MB_TOPMOST constant in the type to get it to be topmost.

    or ...

    http://community.idera.com/powershell/powertips/b/tips/posts/displaying-msgbox-topmost

  • #113030

    Participant
    Points: 20
    Rank: Member

    You can use comobject here, create a wscript.shell object to use the Appactive() method.

    $App = New-Object -ComObject wscript.shell
    $App.AppActivate('TTILE')
    
    • #113036

      Participant
      Points: 0
      Rank: Member

      I tried this already. However, it is not working. I am basically launching a hidden powershell script and then launching  the message box. I have this listed before the message box section:

      (New-Object -ComObject WScript.Shell).AppActivate((get-process powershell -ea SilentlyContinue).MainWindowTitle)

      This vbscript method MIGHT work. It has been the only option that has had positive results with coming to the front of the screen...

      Add-Type -AssemblyName Microsoft.VisualBasic
      
      $result = [Microsoft.VisualBasic.Interaction]::MsgBox('MESSAGE','YesNo,SystemModal,Information', 'TITLE')
      
      $result

You must be logged in to reply to this topic.