我在 Windows.Forms 中使用 SetBounds 方法而不是 Left、Top、Width、Height 属性分配,因为每次我分配更改位置属性的值时 - 窗口都会更改其位置。 Left、Top、Width、Height 赋值导致窗口移动 4 次,而 SetBounds 移动窗口一次(更好的 UI 体验,没有窗口犹豫)。
当我迁移到 WPF 时,我发现没有 SetBounds 方法,看起来我必须逐步更改窗口大小和位置。
在一个窗口移动中更改 WPF 窗口位置的最佳方法是什么?
SetBounds
在 WPF 中不可用,但您可以轻松 P/调用SetWindowPos
API:
private IntPtr _handle;
private void SetBounds(int left, int top, int width, int height)
{
if (_handle == IntPtr.Zero)
_handle = new WindowInteropHelper(this).Handle;
SetWindowPos(_handle, IntPtr.Zero, left, top, width, height, 0);
}
[DllImport("user32")]
static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
uint uFlags);
The Left
, Top
, Width
and Height
依赖属性将自动更新以反映新的边界。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)