31

I design a WPF form with Window Style=None. So I Cannot see the drag bar in the form. How can I move the Form with WindowStyle=None Property?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jawahar BABU
  • 1,055
  • 2
  • 9
  • 7

5 Answers5

76

I am using a main window to hold pages (creating a navigation style program), and in the code behind of my main window, I inserted this...

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonDown(e);

    // Begin dragging the window
    this.DragMove();
}

... and it works like a charm. This is with windowstyle=none. Its nice in the sense that you can click anywhere on the app and move it instead of just being limited to a top bar.

brainimus
  • 10,586
  • 12
  • 42
  • 64
  • 1
    "like a charm" as you said :) – Damian K. Mar 10 '19 at 12:20
  • Great solution, works a treat. One thing to note, this does not work with the right mouse button, it results in "Can only call DragMove when primary mouse button is down." – Mr L Jun 25 '20 at 13:36
8

See this question.

Basically you use the Window.DragMove method for this.

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
8

In our application we have Windows with WindowStyle set to "none", we implemented the functionality to drag the Window, but only from the header rather than from any point in the Window. We did this by adding a Border as a header, then adding a Thumb to fill the entire Border. We then handle the DragDelta method on the Thumb in the code-behind for the Window.

<Border 
        Name="headerBorder" 
        Width="Auto" 
        Height="50" 
        VerticalAlignment="Top"
        CornerRadius="5,5,0,0" 
        DockPanel.Dock="Top" 
        Background="{StaticResource BackgroundBrush}" 
        BorderThickness="1,1,1,1"
        BorderBrush="{StaticResource BorderBrush}">
        <Grid>
            <Thumb 
                x:Name="headerThumb" 
                Opacity="0" 
                Background="{x:Null}" 
                Foreground="{x:Null}" 
                DragDelta="headerThumb_DragDelta"/>
        </Grid>
    </Border>

Then in the code-behind we have the following event handler...

private void headerThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
    Left = Left + e.HorizontalChange;
    Top = Top + e.VerticalChange;
}

I don't know if this is any better than the other method, it's just the way we did it.

TabbyCool
  • 2,829
  • 1
  • 24
  • 36
4

either inside the windows on load function or inside the grid's on load function use a deligate to trigger the DragMove() method on Mouse Click

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
            this.MouseLeftButtonDown += delegate{DragMove();};
}
Pranavan Maru
  • 481
  • 5
  • 10
2

If you simply add this.DragMove(); and you are using Bing Maps, then you will get some frustrating behavior when trying to pan the map.

Using TabbyCool's answer was a good solution however, you can not drag the window against the top of the screen to maximise it.

My solution was just checking that the position.Y of my click relative to my top bar grid was less than a suitable amount.

 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        Point pt = e.GetPosition(topBar);

        Debug.WriteLine(pt.Y);

        if (pt.Y < topBar.ActualHeight)
        {
            DragMove();
        }
    }
Steve
  • 4,372
  • 26
  • 37