2017-08-16 78 views
-2

我正在創建一個小型遊戲,調整主窗口大小以適合不斷增長的棋盤。我想重新定位窗口,以保持調整大小後在鼠標下單擊的按鈕。目前,點擊時,電路板變寬,並將按鈕從鼠標移開。調整鼠標位置後重新定位WPF窗口

當我移動窗口時,如何將按鈕定義爲定位點?

+0

請添加您的當前代碼。 – MJK

回答

0

我不知道是否有方法將按鈕用作定位點。但經過一番思考,我想出了一個可以爲你工作的代碼。 基本上,我使用鼠標相對於調整大小前後的按鈕位置來相應地移動窗口。我希望它有幫助。

private void Btn1Click(object sender, RoutedEventArgs e) 
{ 
    int widthGrowth = 50; 
    int heightGrowth = 80; 

    Button btn = sender as Button; 
    Point oldMousePosition = Mouse.GetPosition(btn); 

    Width += widthGrowth; 
    Height += heightGrowth; 

    Point newMousePosition = Mouse.GetPosition(btn); 

    Left += newMousePosition.X - oldMousePosition.X; 
    Top += newMousePosition.Y - oldMousePosition.Y; 
}