c# WPF移动窗口

本文关键字:窗口 移动 WPF | 更新日期: 2023-09-27 18:14:18

我已经为我的Window添加了以下参数:

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

现在我不能移动Window,所以我添加了以下部分代码到我的Window:

#region Window: Moving
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
}
#endregion

我还必须指定我的Window中的XAML代码如下(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>

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

c# WPF移动窗口

我使用事件MouseDown:

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

的代码:

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

找到一个例子:http://cloudstore.blogspot.com.br/2008/06/moving-wpf-window-with-windowstyle-of.html

无论如何,移动窗口在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;
    }
}

我尝试了另一种解决方案并工作(不确定它是否最正确)

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

其中GridOfWindow是网格的名称

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

好代码的答案,但bug。它会让你的动作失去控制。

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);
    }
}
(///△///)

@Marcio没有Windows。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;
  }
}

亲切问候

Lex

我不得不修复上面的一点正常工作…现在就像魅力一样工作!使用:MouseLeftButtonDown在你的主窗口

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