[英].NET MessageBox with MB_TOPMOST shows up behind other application
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 标志。 我的应用程序没有单独的 window。
在第一个 MessageBox 前面总是相同的 window。 它是一个与我的应用程序通信的 c# 客户端。 例如,如果 Visual Studio 可见,则第一个 MessageBox 会按预期工作。 我的假设是客户端应用程序在某种程度上比我的第一个 MessageBox 具有更高的优先级。 显示后是否需要使用 SetForegroundWindow 将 MessageBox 设置为前台?
知道为什么我的第一个 MessageBox 隐藏在应用程序后面,而第二个不是吗?
我的主要问题是 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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.