C#鼠标拖动图片控件中的图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private Image capturedImage = null;
private Point capturedImageLocation;
private Point capturedImageCursorLocation;
private bool capturedImageIsMouseDown = false;
private bool capturedImageIsFullDisplayed = false;
private void dataGridViewRecord_CellClick(object sender, DataGridViewCellEventArgs e)
{
	string str = dataGridViewRecord.Rows[e.RowIndex].Cells["pic1"].Value.ToString();
	capturedImage = System.Drawing.Image.FromFile(str);
	capturedImageLocation = new Point(0, 0);
	pictureBoxCaptured.Invalidate();
}
 
 
private void pictureBoxCaptured_DoubleClick(object sender, EventArgs e)
{
	if (capturedImageIsFullDisplayed)
		capturedImageIsFullDisplayed = false;
	else
		capturedImageIsFullDisplayed = true;
	pictureBoxCaptured.Invalidate();
}
private void pictureBoxCaptured_MouseDown(object sender, MouseEventArgs e)
{
	if (capturedImageIsFullDisplayed)
		return;
 
	capturedImageIsMouseDown = true;
	capturedImageCursorLocation = new Point(e.X - capturedImageLocation.X, e.Y - capturedImageLocation.Y);
}
private void pictureBoxCaptured_MouseUp(object sender, MouseEventArgs e)
{
	capturedImageIsMouseDown = false;
}
private void pictureBoxCaptured_MouseMove(object sender, MouseEventArgs e)
{
	if (capturedImageIsFullDisplayed)
		return;
	if (!capturedImageIsMouseDown)
		return;
	capturedImageLocation = new Point(e.X - capturedImageCursorLocation.X, e.Y - capturedImageCursorLocation.Y);
	pictureBoxCaptured.Invalidate();
}
private void pictureBoxCaptured_Paint(object sender, PaintEventArgs e)
{
	if (null == capturedImage)
		return;
 
	if (capturedImageIsFullDisplayed)
	{
		e.Graphics.DrawImage(capturedImage, pictureBoxCaptured.ClientRectangle, //e.ClipRectangle,
			new Rectangle(0, 0, capturedImage.Width, capturedImage.Height), GraphicsUnit.Pixel);
	}
	else
	{
		e.Graphics.DrawImage(capturedImage, pictureBoxCaptured.ClientRectangle,
			new Rectangle(-capturedImageLocation.X, -capturedImageLocation.Y, pictureBoxCaptured.Width, pictureBoxCaptured.Height), GraphicsUnit.Pixel);
	}
}

Leave a Comment