PDA

View Full Version : Check mouse in a region



zhiivn
18-03-2004, 00:39
Mình có viết một chương trình kiểm tra chuột có trong Region không? Kiểm tra thì được nhưng hình còn bị chớp nhiều quá! Mình sửa hoài không được.
Xin các bạn chỉ giáo.
Sau đây là source code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MouseInRegion
{
class DrawForm:Form
{
Point point = new Point();

protected override void OnMouseMove(MouseEventArgs mea)
{
point.X = mea.X;
point.Y = mea.Y;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush solidBrush = new SolidBrush(Color.Black);
Region region1 = new Region(new Rectangle(50, 0, 50, 150));
Region region2 = new Region(new Rectangle(0, 50, 150, 50));

// Create a plus-shaped region by forming the union of region1 and region2.
// The union replaces region1.
region1.Union(region2);

if(region1.IsVisible(point, e.Graphics))
{
// The point is in the region. Use an opaque brush.
solidBrush.Color = Color.FromArgb(255, 255, 0, 0);
}
else
{
// The point is not in the region. Use a semitransparent brush.
solidBrush.Color = Color.FromArgb(64, 255, 0, 0);
}
e.Graphics.FillRegion(solidBrush, region1);
}
public static void Main()
{
Application.Run(new DrawForm());
}
}
}