111,125
社区成员
发帖
与我相关
我的任务
分享
public partial class Form2 : Form
{
private Point m_ImgDrawPoint;
private Point m_ImgTmpPoint;
private Point m_MouseDownPoint;
private Image m_Image;
private bool m_MouseInImage;
public Form2()
{
InitializeComponent();
this.DoubleBuffered = true;
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
OpenFileDialog of = new OpenFileDialog();
of.Filter = "jpg文件;asdfsad;asdfsa|*.jpg|bmp文件|*.bmp|gif文件|*.gif";
if (of.ShowDialog(this) == DialogResult.OK)
{
m_ImgDrawPoint = Point.Empty;
if (this.m_Image != null)
{
this.m_Image.Dispose();
}
m_Image = Image.FromFile(of.FileName);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.m_Image != null)
{
e.Graphics.DrawImage(this.m_Image, this.m_ImgDrawPoint.X, this.m_ImgDrawPoint.Y, this.m_Image.Width, this.m_Image.Height);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
if (this.m_Image != null)
{
m_MouseDownPoint = e.Location;
this.m_ImgTmpPoint = this.m_ImgDrawPoint;
Rectangle rect = new Rectangle(this.m_ImgDrawPoint.X, this.m_ImgDrawPoint.Y, this.m_Image.Width, this.m_Image.Height);
m_MouseInImage = rect.Contains(e.Location);
if (m_MouseInImage)
{
Point msPoint = e.Location;
msPoint.Offset(-this.m_ImgDrawPoint.X, -this.m_ImgDrawPoint.Y);
this.Text = string.Format("鼠标在图片上的位置:{0}", msPoint.ToString());
}
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
if (m_MouseInImage)
{
Point pt = this.m_ImgTmpPoint;
pt.Offset(e.X - this.m_MouseDownPoint.X, e.Y - this.m_MouseDownPoint.Y);
this.m_ImgDrawPoint = pt;
this.Invalidate();
}
}
}
}