【问题标题】:.NET MessageBox with MB_TOPMOST shows up behind other application带有 MB_TOPMOST 的 .NET MessageBox 显示在其他应用程序后面
【发布时间】:2021-04-21 14:16:06
【问题描述】:
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));

我调用 MessageBox 两次,第一个 显示 TOPMOST,第二个 确实 显示 TOPMOST(在我单击第一个之后)。我已经尝试将 windowHandle 设置为 NULL 并添加 MB_SETFOREGROUND 标志。我的应用程序没有单独的窗口。

第一个 MessageBox 前面始终是同一个窗口。它是一个与我的应用程序通信的 C# 客户端。如果例如Visual Studio 是可见的,第一个 MessageBox 按预期工作。我的假设是客户端应用程序在某种程度上比我的第一个 MessageBox 具有更高的优先级。显示后是否需要使用 SetForegroundWindow 将 MessageBox 设置为前台?

知道为什么我的第一个 MessageBox 隐藏在应用程序后面,而第二个没有?

【问题讨论】:

  • 这是否解决了您的问题? stackoverflow.com/questions/16105097/…
  • 我已经阅读了这个问题,但我找不到解决问题的方法。但也许我不明白这个问题。
  • 你可以让它成为超级顶级的:) 添加MB_SYSTEMMODAL,它的值是0x00001000
  • 这可能是矫枉过正。可能有问题的是您需要将父窗口句柄作为您在 MessageBox 中的第一个参数传递。您将其设置为 NULL。

标签: c# messagebox


【解决方案1】:

我的主要问题是 MessageBox 前面总是有一个特定的应用程序。因此,我的解决方案是获取应用程序的 MainWindowHandle 并将其传递给 MessageBox。

备注:为什么要检查是否有确切的一个进程?该应用程序只能打开一次,如果有多个则表明问题更大。

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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多