C# WPF 移动窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16608523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:27:22  来源:igfitidea点击:

C# WPF Move the window

c#wpfresizewindow

提问by user2194683

I have added the following parameters to my Window:

我已将以下参数添加到我的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:

现在我无法移动Window,所以我在我的 中添加了以下代码部分Window

#region Window: Moving

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

#endregion

Also I must specify that my XAMLcode in my Windowis the following (the Windowlook like the Polygon):

此外,我必须指定我的XAML代码Window如下(Window看起来像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 Windowchange 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).

我想知道我应该怎么做才能Window改变它在鼠标拖动时的位置以及添加什么来调整窗口大小,条件是我将添加的控件和其他东西也会调整大小(我发现这个代码可以调整大小和我想知道这里是否好)。

采纳答案by gariel

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

找到一个例子: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:

无论如何,要在 WinForms 中移动窗口,我在项目中使用了以下代码,如果您遇到问题,可能会很有用:

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

回答by nyconing

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.(///▽///)

它会让你的移动棒停留在你的光标上。(////▽///)

回答by Goodies

@Marcio there is no Windows.Forms in WPF.

@Marcio WPF 中没有 Windows.Forms。

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

我让这个版本与 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

莱克斯

回答by Rubens

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

其中 GridOfWindow 是网格的名称

<Grid x:Name="GridOfWindow" MouseLeftButtonDown="GridOfWindow_MouseLeftButtonDown">

回答by Sandro Z.

I used the event MouseDown:

我使用了事件 MouseDown:

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

with this code:

使用此代码:

  private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  {
     if(e.ChangedButton == MouseButton.Left)
        this.DragMove();
  }