1
我正在製作一個遊戲庫存系統,其中我有一個網格和邊框,佔據網格空間有一個圖標的背景圖像。例如,在網格位置0,0中,我有一個帶有健康藥水背景的邊框。我希望能夠將此邊框拖到任何其他網格位置。現在我正在努力點擊並拖動邊框來跟隨鼠標。WPF在屏幕上移動的邊框
我到目前爲止有些作品,但不是真的。如果我點擊圖標並緩慢移動,就會感到不適。不過,我可以快速移出圖標,但它不能跟上,所以它會停止移動,直到我回來。關於如何獲得這種功能的任何想法?
public partial class MainWindow : Window
{
TranslateTransform trans = new TranslateTransform();
Border border;
public MainWindow()
{
InitializeComponent();
// create a broder and set it's background image
border = new Border();
border.Visibility = System.Windows.Visibility.Visible;
var img = (Image)MasterGrid.FindResource("notepad");
var imgBrush = new ImageBrush(img.Source);
border.Background = imgBrush;
border.Margin = new Thickness(2.0);
// add the border to the grid
Grid.SetRow(border, 0);
Grid.SetColumn(border, 1);
Grid.SetRowSpan(border, 2);
Grid.SetColumnSpan(border, 1);
InvGrid.Children.Add(border);
// hook up events
_00.MouseUp += new System.Windows.Input.MouseButtonEventHandler(_00_MouseUp);
border.MouseDown += border_MouseDown;
border.MouseMove += border_MouseMove;
}
void border_MouseMove(object sender, MouseEventArgs e)
{
// make the border follow the mouse position
trans.X = e.GetPosition(border).X;
trans.Y = e.GetPosition(border).Y;
}
void border_MouseDown(object sender, MouseButtonEventArgs e)
{
border.RenderTransform = trans;
}
private void _00_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("test");
}
}
@ user441521它回答你的問題嗎? –