我有一个WindowStyle =“None”和AllowTransparency =“True”的Window元素,因此它没有标题栏并支持透明度.
我希望用户能够通过左键单击窗口中的任意位置并拖动窗口将窗口移动到屏幕上的任何位置.只要按下鼠标左键,窗口就会随鼠标拖动.
我能够得到这个功能有一个例外:当鼠标移动到窗口外面(例如当鼠标左键靠近窗口边缘并且鼠标被移动时),窗口不再捕捉鼠标位置,并且不会随鼠标拖动.
public Point MouseDownPosition { get; set; } public Point MousePosition { get; set; } public bool MouseIsDown { get; set; } private void window_MyWindowName_MouseLeftButtonDown(object sender,MouseButtonEventArgs e) { MouseDownPosition = e.GetPosition(null); MouseIsDown = true; } private void window_MyWindowName_MouseMove(object sender,MouseEventArgs e) { if (MouseIsDown) { MousePosition = e.GetPosition(null); Left += MousePosition.X - MouseDownPosition.X; Top += MousePosition.Y - MouseDownPosition.Y; } } private void window_MyWindowName_MouseLeftButtonUp(object sender,MouseButtonEventArgs e) { MouseIsDown = false; }
解决方法
我相信你正在重塑轮子.搜索“Window.DragMove”.
例:
private void title_MouseLeftButtonDown(object sender,MouseButtonEventArgs e) { this.DragMove(); }