1
votes

I was making a WPF application with Windows style = None, I managed to create and work exit button in my window but i don't know how to make it drag able while pressing left mouse button. I have created Mouse left button down event in .cs file as below:

private void see(object sender, MouseButtonEventArgs e)
{
   this.DragMove();
}

Then I added border in .xaml file to do dragging of the window as below:

<Grid>
   <Border BorderThickness="2" BorderBrush="Black" Height="120" Width="100" MouseLeftButtonDown="see" />
</Grid>

Now i don't understand what is the problem here? I will be very thankful if someone help me in this ?

2

2 Answers

2
votes

Use a similar pattern to this Window:

public class DragableWindowNoStyle : Window
{

    public DragableWindowNoStyle()
    {
        WindowStyle = WindowStyle.None;
        Grid grid = new Grid() { };
        _moveBorder = new Border()
        {
            BorderThickness = new Thickness(2),
            BorderBrush = Brushes.Red,
            Background = Brushes.Black,
            Width = 50,
            Height = 20,
            HorizontalAlignment= System.Windows.HorizontalAlignment.Center,
            VerticalAlignment = System.Windows.VerticalAlignment.Top,
        }; 
        grid.Children.Add(_moveBorder); 
        this.Content = grid;

        _moveBorder.PreviewMouseLeftButtonDown += _moveBorder_PreviewMouseLeftButtonDown;  
    }

    Point _startPoint;
    bool _isDragging = false;
    Border _moveBorder;

    private void _moveBorder_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (Mouse.Capture(this))
        {
            _isDragging = true;
            _startPoint = PointToScreen(Mouse.GetPosition(this));
        }
    }

    protected override void OnPreviewMouseMove(MouseEventArgs e)
    {
        if (_isDragging)
        {
            Point newPoint = PointToScreen(Mouse.GetPosition(this));
            int diffX = (int)(newPoint.X - _startPoint.X);
            int diffY = (int)(newPoint.Y - _startPoint.Y);
            if (Math.Abs(diffX) > 1 || Math.Abs(diffY) > 1)
            {
                Left += diffX;
                Top += diffY;
                InvalidateVisual();
                _startPoint = newPoint;
            }
        }
    }
    protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        if (_isDragging)
        {
            _isDragging = false;
            Mouse.Capture(null);
        }
    } 



}
0
votes

There is an example of how to create a custom window with resize, drag, minimize, restore and close functionality from scratch available here:

How to create a custom window in WPF: https://blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/

You could also customize a window while retaining its standard functionality using the WindowChrome class: https://msdn.microsoft.com/en-us/library/system.windows.shell.windowchrome(v=vs.110).aspx. Then you don't have to implement the resize and drag functionality yourself.