C# WPF 移动窗口 [英] C# WPF Move the window

查看:95
本文介绍了C# WPF 移动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have added the following parameters to my Window:

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

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

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

此外,我必须指定 Window 中的 XAML 代码如下(Window 看起来像 Polygon代码>):

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>

我想知道我应该怎么做才能使 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).

推荐答案

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

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

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

这篇关于C# WPF 移动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆