我希望在其父窗体的中心显示我的Messagebox。如果我移动窗体并显示消息框,它总是显示在桌面的中心。我希望它与表单一起出现。你能给我一些窍门和建议吗?
发布于 2009-10-27 16:42:25
要做到这一点,最好的方法是使用Window Hooks并自己将消息框居中。有一篇完美的文章展示了这种用法。
你可以在这里找到它:http://www.codeproject.com/KB/dialog/CenterDialog.aspx
您还可以在应用程序中使用该类,而不必深入了解它的实际工作原理。
发布于 2011-04-11 15:12:46
我基于Windows窗体的一个类创建了这个类,我在其他地方找到了一些窗体。
只需将类添加到您的WPF项目中,并将"this“作为参数提供给helper方法,如下所示:
MessageBoxHelper.PrepToCenterMessageBoxOnForm(this)" 然后显示消息框:
MessageBox.Show("Hello there!");
/// <summary>
/// This class makes it possible to center a MessageBox over the parent dialog.
/// Usage example:
/// MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
/// MessageBox.Show("Hello there!);
/// </summary>
public static class MessageBoxHelper
{
public static void PrepToCenterMessageBoxOnForm(Window window)
{
MessageBoxCenterHelper helper = new MessageBoxCenterHelper();
helper.Prep(window);
}
private class MessageBoxCenterHelper
{
private int messageHook;
private IntPtr parentFormHandle;
public void Prep(Window window)
{
NativeMethods.CenterMessageCallBackDelegate callBackDelegate = new NativeMethods.CenterMessageCallBackDelegate(CenterMessageCallBack);
GCHandle.Alloc(callBackDelegate);
parentFormHandle = new WindowInteropHelper(window).Handle;
messageHook = NativeMethods.SetWindowsHookEx(5, callBackDelegate, new IntPtr(NativeMethods.GetWindowLong(parentFormHandle, -6)), NativeMethods.GetCurrentThreadId()).ToInt32();
}
private int CenterMessageCallBack(int message, int wParam, int lParam)
{
NativeMethods.RECT formRect;
NativeMethods.RECT messageBoxRect;
int xPos;
int yPos;
if (message == 5)
{
NativeMethods.GetWindowRect(parentFormHandle, out formRect);
NativeMethods.GetWindowRect(new IntPtr(wParam), out messageBoxRect);
xPos = (int)((formRect.Left + (formRect.Right - formRect.Left) / 2) - ((messageBoxRect.Right - messageBoxRect.Left) / 2));
yPos = (int)((formRect.Top + (formRect.Bottom - formRect.Top) / 2) - ((messageBoxRect.Bottom - messageBoxRect.Top) / 2));
NativeMethods.SetWindowPos(wParam, 0, xPos, yPos, 0, 0, 0x1 | 0x4 | 0x10);
NativeMethods.UnhookWindowsHookEx(messageHook);
}
return 0;
}
}
private static class NativeMethods
{
internal struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
internal delegate int CenterMessageCallBackDelegate(int message, int wParam, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnhookWindowsHookEx(int hhk);
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("kernel32.dll")]
internal static extern int GetCurrentThreadId();
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int hook, CenterMessageCallBackDelegate callback, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
}
}发布于 2009-10-27 15:27:48
将消息框窗口的所有者设置为窗口(使用.Show()的第一个参数),而不是不设置所有者。
有关参考,请参阅here。
https://stackoverflow.com/questions/1629213
复制相似问题