[C#] 纯文本查看 复制代码 public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
pictureBox1.Cursor = Cursors.SizeAll;
pictureBox1.MouseDown += PictureBox1_MouseDown;
pictureBox1.MouseMove += PictureBox1_MouseMove;
pictureBox1.MouseUp += PictureBox1_MouseUp;
}
private bool _isMouseDown = false;
private Point _startLocation;
private int _startX;
private int _startY;
private void PictureBox1_MouseDown(object? sender, MouseEventArgs e)
{
_isMouseDown = true;
pictureBox1.Capture = true;
_startX = e.X + pictureBox1.Left;
_startY = e.Y + pictureBox1.Top;
_startLocation = pictureBox1.Location;
}
private void PictureBox1_MouseMove(object? sender, MouseEventArgs e)
{
if (!_isMouseDown)
{
return;
}
var x = e.X + pictureBox1.Left;
var y = e.Y + pictureBox1.Top;
var diffX = x - _startX;
var diffY = y - _startY;
var left = diffX + _startLocation.X;
var top = diffY + _startLocation.Y;
pictureBox1.Left = left;
pictureBox1.Top = top;
}
private void PictureBox1_MouseUp(object? sender, MouseEventArgs e)
{
_isMouseDown = false;
pictureBox1.Capture = false;
}
private void Window_Load(object sender, EventArgs e)
{
}
} |