0
votes

I have made a custom overlay messagebox (comes down to a borderless WPF window) that spans the whole screen in width. I have however implemented logic for the user to be able to drag the messagebox around, as you can configure it to bot be an overlay, but a normal size message box.

With the overlay, I want to limit the drag movement to only include vertical (up/down) changes, the window should not be able to be dragged horizontally.

I hooked up the MouseDown event to the border in the window, in order to drag the window around:

    private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
        if (msgType != MessageType.OverlayMessage && msgType != MessageType.OverlayMessageDialog) {
            this.DragMove(); // drags the window normally
        }
    }

What I tried was to capture the cursor on the mouse down event, do the dragging logic in the MouseMove event and release the cursor when the mouse button is released, but that did not work - when I click on the border, and click on something else (away from the window) and go back to the border, then the window snaps to the cursor as i want it (Moving only vertically), but that behavior should happen when I click and drag the cursor:

    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;
        }
    }
1

1 Answers

1
votes

Add the following to your mouse move:

if (e.LeftButton != MouseButtonState.Pressed) return;

Also it sounds like something else could be swallowing your MouseUp event. Are you handling PreviewMouseDown/Up or just MouseDown/Up - you could try the former to get the tunneled event.