Rectangle _rect; private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Point p = new Point(100, 100); string str = "Test1001"; Font font = new Font("宋体", 12); SizeF s = g.MeasureString(str, font); _rect = new Rectangle(p, new Size((int)s.Width, (int)s.Height)); g.DrawString(str, font, Brushes.Red, p); } private void Form1_MouseMove(object sender, MouseEventArgs e) { Point p = new Point(e.X, e.Y); if (_rect.Contains(p)) { MessageBox.Show("Test"); } }
你在画数值的时候可以把该数值所处的Rectange存下来
每隔一定时间判断鼠标位置是否在该Rectange里面
{
Graphics g = e.Graphics; Point p = new Point(100, 100); string str = "Test1001";
Font font = new Font("宋体", 12);
SizeF s = g.MeasureString(str, font);
_rect = new Rectangle(p, new Size((int)s.Width, (int)s.Height)); g.DrawString(str, font, Brushes.Red, p);
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
if (_rect.Contains(p))
{
MessageBox.Show("Test");
}
}