在多监视器屏幕上定位主机 window 中的对话框时,左侧和顶部值发生变化
Left and top values change while positioning a dialog in host window on multi monitor screens
我正在为现有应用程序设置子对话框 window。我希望对话框出现在主机 window 的右下角。因此,我在注释行的帮助下设置对话框的顶部和左侧。该代码在多显示器系统的主屏幕上正常工作,但在辅助屏幕上不起作用。 top 和 left 值自行更改为与分配的数字不同的数字。
作为测试,我手动对值进行硬编码以检查这些值是否保持不变,而它们是否保持不变。对于左侧 495 更改为 -2416,dtop 从 643 更改为 760。屏幕分辨率为 1920 X 1080。我无法理解为什么会发生这种情况以及如何正确设置 window 以便 window 出现在主机 window 的右下角。
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;
关于此的更新
1) 它将辅助监视器的分辨率宽度添加到对话框的 X 坐标。
2)然而,扣除分辨率宽度也无济于事。
3) 因此最终我避免设置 dialog.Left 和 dialog.Top 值,而是使用 Windows 本机方法。
/// <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);
我正在为现有应用程序设置子对话框 window。我希望对话框出现在主机 window 的右下角。因此,我在注释行的帮助下设置对话框的顶部和左侧。该代码在多显示器系统的主屏幕上正常工作,但在辅助屏幕上不起作用。 top 和 left 值自行更改为与分配的数字不同的数字。 作为测试,我手动对值进行硬编码以检查这些值是否保持不变,而它们是否保持不变。对于左侧 495 更改为 -2416,dtop 从 643 更改为 760。屏幕分辨率为 1920 X 1080。我无法理解为什么会发生这种情况以及如何正确设置 window 以便 window 出现在主机 window 的右下角。
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;
关于此的更新 1) 它将辅助监视器的分辨率宽度添加到对话框的 X 坐标。 2)然而,扣除分辨率宽度也无济于事。 3) 因此最终我避免设置 dialog.Left 和 dialog.Top 值,而是使用 Windows 本机方法。
/// <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);