6

I have added the following parameters to my Window:

WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
ResizeMode="NoResize" Background="Transparent" 

And now I can't move the Window, so I have added the following part of code to my Window:

#region Window: Moving

private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
}

#endregion

Also I must specify that my XAML code in my Window is the following (the Window look like the Polygon):

<Window Title="New Science"
    Height="588" Width="760" MinHeight="360" MinWidth="360"
    WindowStyle="None" WindowStartupLocation="CenterScreen"
    AllowsTransparency="True"
    ResizeMode="NoResize" Background="Transparent"
    xmlns:my="clr-namespace:Bourlesque.Lib.Windows.Media;assembly=Bourlesque.Lib.Windows.Media">
    <Grid>
        <my:UniPolygon DefaultRadiusIn="10" DefaultRadiusOut="10" Fill="#FF92C2F2" Name="m_tPlgOuter" Offset="0" Points="         0;26;;         10;19;10;;         10;0;;         265;0;20;;         290;20;20;;          -60,1;20;3;;         -60,1;5;10;;         -40,1;5;10;;         -40,1;20;2.5;;          -35,1;20;2.5;;         -35,1;5;10;;         -15,1;5;10;;         -15,1;20;3;;          0,1;20;;         0,1;0,1;;         0;0,1;;       " Stretch="None" Stroke="#FF535353" StrokeThickness="0.1" />
    </Grid>
</Window>

I would like to know what should I do to make the Window change it's position on mouse drag and what to add to resize the window with the condition that the controls and other things I will add will resize too(I have found this code to resize and I would like to know if is good here).

KyleMit
  • 30,350
  • 66
  • 462
  • 664
user2194683
  • 105
  • 2
  • 2
  • 9

6 Answers6

13

I used the event MouseDown:

<Window .....
     MouseDown="Window_MouseDown"  >

with this code:

  private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  {
     if(e.ChangedButton == MouseButton.Left)
        this.DragMove();
  }
Sandro Z.
  • 151
  • 1
  • 9
4

Found a example: http://cloudstore.blogspot.com.br/2008/06/moving-wpf-window-with-windowstyle-of.html

Anyway, to move a Window in WinForms I used in a project the following code, can be useful if you are having problems:

private bool clicado = false;
private Point lm = new Point();
void PnMouseDown(object sender, MouseEventArgs e)
{
    clicado = true;
    this.lm = MousePosition;
}

void PnMouseUp(object sender, MouseEventArgs e)
{
    clicado = false;
}

void PnMouseMove(object sender, MouseEventArgs e)
{
    if(clicado)
    {
        this.Left += (MousePosition.X - this.lm.X);
        this.Top += (MousePosition.Y - this.lm.Y);
        this.lm = MousePosition;
    }
}
gariel
  • 687
  • 3
  • 13
4

I've tried another solution and worked (not sure if it is the most correct though)

private void GridOfWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var move = sender as System.Windows.Controls.Grid;
        var win = Window.GetWindow(move);
        win.DragMove();
    }

where GridOfWindow is the name of the Grid

<Grid x:Name="GridOfWindow" MouseLeftButtonDown="GridOfWindow_MouseLeftButtonDown">
Rubens
  • 41
  • 2
3

good code to the answer, but buggy. it will get your moving out of control.

try my modify:

private bool clicado = false;
private Point lm = new Point();
void PnMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
{
    clicado = true;
    this.lm = System.Windows.Forms.Control.MousePosition;
    this.lm.X = Convert.ToInt16(this.Left) - this.lm.X;
    this.lm.Y = Convert.ToInt16(this.Top) - this.lm.Y;
}

void PnMouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
    clicado = false;
}

void PnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    if (clicado)
    {
        this.Left = (System.Windows.Forms.Control.MousePosition.X + this.lm.X);
        this.Top = (System.Windows.Forms.Control.MousePosition.Y + this.lm.Y);
    }
}

it will get your moving stick to your cursor.(///▽///)

Marcio Rinaldi
  • 3,305
  • 24
  • 23
nyconing
  • 1,092
  • 1
  • 10
  • 33
3

@Marcio there is no Windows.Forms in WPF.

I got this version to work (steady) with WPF,

private bool clicked = false;
private Point lmAbs = new Point();
void PnMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
{
  clicked = true;
  this.lmAbs = e.GetPosition(this);
  this.lmAbs.Y = Convert.ToInt16(this.Top) + this.lmAbs.Y;
  this.lmAbs.X = Convert.ToInt16(this.Left) + this.lmAbs.X;
}

void PnMouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
  clicked = false;
}

void PnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
  if (clicked)
  {
    Point MousePosition = e.GetPosition(this);
    Point MousePositionAbs = new Point();
    MousePositionAbs.X = Convert.ToInt16(this.Left) + MousePosition.X;
    MousePositionAbs.Y = Convert.ToInt16(this.Top) + MousePosition.Y;
    this.Left = this.Left + (MousePositionAbs.X - this.lmAbs.X);
    this.Top = this.Top + (MousePositionAbs.Y - this.lmAbs.Y);
    this.lmAbs = MousePositionAbs;
  }
}

Kind regards,

Lex

Goodies
  • 1,951
  • 21
  • 26
0

I had to fix above's a bit to work properly... working as charm now! Use this with: MouseLeftButtonDown at your main Window.

    private void EnableDrag(object sender, MouseButtonEventArgs e)
    {
        var move = sender as Window;
        if (move != null)
        {
            Window win = Window.GetWindow(move);
            win.DragMove();
        }
    }
Fedsson
  • 1
  • 1