0
internal class NativeMethods
{
     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern int MessageBox(
         IntPtr windowHandle,
         [param: MarshalAs(UnmanagedType.LPWStr)]
         string message,
         [param: MarshalAs(UnmanagedType.LPWStr)]
         string title,
         uint type);
}

messageBoxResult = NativeMethods.MessageBox(
    (IntPtr)0, 
    text, 
    "Title", 
    (int)(NativeMethods.MB_OK | NativeMethods.MB_ICONINFORMATION | NativeMethods.MB_TOPMOST));

I call the MessageBox twice, the first one does not show up TOPMOST, the second one does show up TOPMOST (after I clicked on the first one). I already tried to set the windowHandle to NULL and add the MB_SETFOREGROUND flag. My application does not have a separate window.

It is always the same window which is in front of the first MessageBox. It is a c# client which communicates with my application. If e.g. Visual Studio is visible the first MessageBox works like intended. My assumption is that the client application somehow has a higher priority than my first MessageBox. Do I need to set the MessageBox to foreground with SetForegroundWindow after showing it?

Any idea why my first MessageBox is hidden behind the application but the second one isn't?

0lli.rocks
  • 1,027
  • 1
  • 18
  • 31
  • 2
    Does this address your question? https://stackoverflow.com/questions/16105097/why-isnt-messagebox-topmost –  Apr 21 '21 at 14:17
  • I alread read this question, but I couldn't find a solution for my problem. But maybe I don't unterstand the problem. – 0lli.rocks Apr 21 '21 at 14:20
  • You can make it super-topmost :) Add `MB_SYSTEMMODAL` which has the value `0x00001000` – Andy Apr 21 '21 at 14:25
  • See following : https://stackoverflow.com/questions/6228089/how-do-i-bring-an-unmanaged-application-window-to-front-and-make-it-the-active?force_isolation=true – jdweng Apr 21 '21 at 14:26
  • @Andy Is that a valid way? Microsoft says: Use system-modal message boxes to notify the user of serious, potentially damaging errors that require immediate attention. This is not the case here, just a normal message. – 0lli.rocks Apr 21 '21 at 14:34
  • it's saying that the messagebox will be displayed in front of every window no matter what state they are in. It works the same as TOPMOST, but can even go in front of other TOPMOST windows. – Andy Apr 21 '21 at 14:36
  • 2
    It's probably overkill. What's probably wrong is you need to pass in the parent window handle as your first parameter in MessageBox. You are setting it to NULL. – Andy Apr 21 '21 at 14:39

1 Answers1

0

My main issue was that there was always one specific application in front of the MessageBox. My solution therefore is getting the MainWindowHandle of the application and pass it to the MessageBox.

Remark: Why do I check if there is exact one process? The application can only be opened once, if there are more than one it indicates a bigger problem.

var processes = Process.GetProcessesByName("application name");
IntPtr windowhandle = IntPtr.Zero;
if (processes.Length == 1)
{
   foreach (var process in processes )
   {
       windowhandle = process.MainWindowHandle;
   }
}

messageBoxResult = NativeMethods.MessageBox(
   windowhandle, 
   text, 
   "Title", 
   (int)(NativeMethods.MB_OK | NativeMethods.MB_ICONINFORMATION | NativeMethods.MB_TOPMOST));
0lli.rocks
  • 1,027
  • 1
  • 18
  • 31