2013-05-17 132 views
5

我加入下列參數到我的WindowC#WPF移動窗口

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

現在我不能移動Window,所以我已經添加下面的代碼部分我Window

#region Window: Moving 

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

#endregion 

此外,我必須指定我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> 

我想知道我應該怎麼做才能使Window改變它在鼠標拖動上的位置,以及添加什麼來調整窗口的大小,以及控件和其他我要添加的東西的大小也將調整(我有發現這個代碼調整大小,我想知道如果是好的here)。

回答

4

發現了一個例子: 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; 
    } 
} 
2

好的代碼答案,但越野車。它會讓你的行動失去控制。

試試我的修改:

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

它將讓您的移動棒光標(///▽///)

3

@Marcio存在WPF沒有Windows.Forms的。

我得到這個版本的工作(穩定)與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; 
    } 
} 

親切的問候,

萊克斯

0

我嘗試另一種解決方案和工作(不知道這是否是儘管最正確)

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">