C#WPF移动窗口

use*_*683 6 c# wpf resize window

我已将以下参数添加到我的Window:

WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
ResizeMode="NoResize" Background="Transparent" 
Run Code Online (Sandbox Code Playgroud)

现在我无法移动Window,所以我将以下部分代码添加到我的Window:

#region Window: Moving

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

#endregion
Run Code Online (Sandbox Code Playgroud)

另外我必须指定我的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>
Run Code Online (Sandbox Code Playgroud)

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

小智 10

我使用了事件 MouseDown:

<Window .....
     MouseDown="Window_MouseDown"  >
Run Code Online (Sandbox Code Playgroud)

使用此代码:

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


小智 5

找到了一个示例: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;
    }
}
Run Code Online (Sandbox Code Playgroud)