【发布时间】:2016-07-25 13:57:52
【问题描述】:
我制作了一个自定义覆盖消息框(归结为无边框 WPF 窗口),其宽度跨越整个屏幕。然而,我已经实现了用户能够拖动消息框的逻辑,因为您可以将其配置为 bot 是一个覆盖,但一个正常大小的消息框。
使用叠加层,我想将拖动移动限制为仅包括垂直(上/下)变化,窗口不应水平拖动。
我将 MouseDown 事件连接到窗口的边框,以便拖动窗口:
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
if (msgType != MessageType.OverlayMessage && msgType != MessageType.OverlayMessageDialog) {
this.DragMove(); // drags the window normally
}
}
我尝试的是在鼠标按下事件上捕获光标,在 MouseMove 事件中执行拖动逻辑并在释放鼠标按钮时释放光标,但这不起作用 - 当我单击边框时,并且单击其他内容(远离窗口)并返回到边框,然后窗口会根据我的需要捕捉到光标(仅垂直移动),但是当我单击并拖动时应该会发生这种行为> 光标:
bool inDrag = false;
Point anchorPoint;
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
anchorPoint = PointToScreen(e.GetPosition(this));
inDrag = true;
CaptureMouse();
e.Handled = true;
}
private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
if (inDrag) {
ReleaseMouseCapture();
inDrag = false;
e.Handled = true;
}
}
private void Border_MouseMove(object sender, MouseEventArgs e) {
if (inDrag) {
Point currentPoint = PointToScreen(e.GetPosition(this));
this.Top = this.Top + currentPoint.Y - anchorPoint.Y; // only allow vertical movements
anchorPoint = currentPoint;
}
}
【问题讨论】: