我想在其父表单的中心显示我的Messagebox。如果我移动表单并显示消息框,它总是显示在桌面的中心。我希望它与表单一起出现。 你能给我一些技巧和建议吗?
答案 0 :(得分:3)
执行此操作的最佳方法是使用窗口挂钩并自行居中消息框。有一篇完美的文章说明了这种用法。
你可以在这里找到它: http://www.codeproject.com/KB/dialog/CenterDialog.aspx
您也可以在应用程序中使用该类,而无需深入了解它的实际工作方式。
答案 1 :(得分:2)
将消息框窗口的所有者设置为您的窗口(使用.Show()
的第一个参数),而不是设置所有者。
请参阅here以获取参考。
答案 2 :(得分:1)
我之前在C#中做过这个。这是我记得的。
定义一个类:
public class IWindowWrapper : System.Windows.Forms.IWin32Window
{
public IWindowWrapper(IntPtr handle)
{
this.Handle= handle;
}
public IntPtr Handle
{
get;
set;
}
}
基于MessageBox定义一个类。 基于MessageBox创建一个类并创建一个新的Show方法:
public string Show(IWin32Window owner)
{
if(owner == null)
{
this.ShowDialog();
}
else
{
//parentWindow.
this.StartPosition = FormStartPosition.CenterParent;
this.ShowDialog(owner);
}
}
在你的调用代码中(这里假设它是一个winform而msgBox基于新的消息框类)调用新的Show方法并在Show eg上传递一个IWindowWrapper实例。
msgBox.Show(new IWindowWrapper(this.Handle))
答案 3 :(得分:1)
我根据Windows Forms的类创建了这个类,我在其他地方找到了它。
只需将该类添加到WPF项目中,并将“this”作为参数提供给辅助方法,如下所示:
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);
}
}
答案 4 :(得分:0)
这是一个非常易于使用的解决方案,它运行良好:
步骤:
(在UserControl
或Form
内)
MessageBoxEx.Show(this, "Please fix the validation errors before saving.", "Validation Errors");