我正在将子对话框设置为现有的应用程序窗口。我希望对话框出现在主机窗口的右下角。因此,我在注释行的帮助下设置对话框的顶部和左侧。代码在多监视器系统的主屏幕上正常工作,但在辅助屏幕上不起作用。顶部和左侧值的变化由它们分配的数量不同。 作为测试,我手动硬编码值以检查值是否保持相同而不是。左边495更改为-2416,dtop更改为643到760.屏幕分辨率为1920 X 1080.我无法理解为什么会发生这种情况以及如何正确设置窗口以使窗口出现在主机的右下角多显示屏幕上的窗口。
dialog.DataContext = opsViewModel;
var window = Window.GetWindow(dialog);
var wih = new WindowInteropHelper(window);
var childWindowHandle = wih.Handle;
var returnHandle = NativeMethods.SetParent(childWindowHandle, parentWindowHandle);
dialog.Show();
// dialog.Left = docView.ClientRectangle.Right - dialog.Width;
// dialog.Top = docView.ClientRectangle.Bottom - dialog.Height;
dialog.Left = -495;
dialog.Top = 643;
答案 0 :(得分:0)
对此进行更新 1)它将辅助监视器的分辨率宽度添加到对话框的X坐标。 2)然而,扣除分辨率宽度也无济于事。 3)因此最终我避免设置dialog.Left和dialog.Top值,而是使用Windows Native方法。
/// <summary>
/// Changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <param name="x">The new position of the left side of the window. </param>
/// <param name="y">The new position of the top of the window. </param>
/// <param name="nWidth">The new width of the window. </param>
/// <param name="nHeight">The new height of the window. </param>
/// <param name="bRepaint">Indicates whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.</param>
/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
[DllImport(USER32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);