突出显示面板区域

时间:2012-02-10 17:14:54

标签: .net winforms graphics gdi+ graphics2d

我有一个VB.NET WinForms应用程序。 我用GDI +以编程方式在面板上绘制了一堆形状。 现在我想以某种方式高亮显示该面板的一个小矩形部分。突出显示的区域将移动很多。我不想重绘整个面板,因为涉及的处理太多而且会导致闪烁。 这是最好的方法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以将透明面板添加为现有面板的子面(并将其停靠),然后在透明面板上进行高光显示吗?

这是一个非常基本的例子:

public partial class Form1 : Form
{
    private Panel pan = new Panel();
    private Panel trans = new Panel();

    private bool clicked { get; set; }
    private Point mouse { get; set; }

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        pan.Dock = DockStyle.Fill;
        pan.BackColor = Color.White;

        trans.Dock = DockStyle.Fill;
        trans.BackColor = Color.FromArgb(0, 0, 0, 0);

        pan.Paint += new PaintEventHandler(DrawSomeShapes);
        trans.MouseClick += new MouseEventHandler(trans_MouseClick);
        trans.Paint += new PaintEventHandler(DrawHighlight);

        pan.Controls.Add(trans);
        this.Controls.Add(pan);

        trans.BringToFront();
    }

    public void DrawSomeShapes(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;

        var p = new Pen(Color.Black, 1F);
        g.DrawRectangle(p, new Rectangle(0, 0, 100, 100));
        g.DrawRectangle(p, new Rectangle(20, 20, 50, 50));
        g.DrawRectangle(p, new Rectangle(10, 50, 100, 25));
        g.DrawRectangle(new Pen(Color.Red, 1F), new Rectangle(new Point(50, 50), new Size(50, 50)));
    }

    public void DrawHighlight(object sender, PaintEventArgs e)
    {
        if (clicked)
        {
            var g = e.Graphics;
            var b = new SolidBrush(Color.FromArgb(50, 50, 50, 50));
            g.FillRectangle(b, mouse.X, mouse.Y, 50, 50);

            clicked = false;
        }
    }

    public void trans_MouseClick(object sender, MouseEventArgs e)
    {
        mouse = e.Location;
        trans.Invalidate();
        clicked = true;
    }
}