用GraphicsPaht画一个园,Graphics填充这个园,用一个按钮来执行Invalidate(),怪现象产生了,点一下圆消失,再点一下又能出来,如此反复,不想让它消失,请教解决方案

解决方案 »

  1.   

    重写你的OnPaint,绘图的代码放这里面。
      

  2.   

    还是一样,直接填充的图形就没有这个问题,通过Path填充的就不对
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ControlsDraw;namespace frmDraw
    {
        public partial class Form1 : Form
        {
            ToolTip tip = new ToolTip();
            LableEx lblex = new LableEx("I am LableEx");
            RectangleEx rectex = new RectangleEx("I am Rect");
            EllipseEx ellex = new EllipseEx("I am Ellipse");
            CheckBoxEx chkex = new CheckBoxEx("I am CheckBox");
            EntityList elist = new EntityList();        int X;
            int Y;
            public Form1()
            {
                InitializeComponent();
                SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            }        private void Form1_Load(object sender, EventArgs e)
            {
                lblex.StartPoint = new Point(200, 10);
                elist.Add(lblex);
                elist.Add(rectex);
                elist.Add(ellex);
                elist.Add(chkex);
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                X = e.X;
                Y = e.Y;
                foreach (EntityBase eb in elist)
                {
                    if (eb.Contains(X, Y))
                    {
                        tip.Show(eb.Name, this);
                        return;
                    }
                }
                tip.Hide(this);
            }        private void Form1_MouseHover(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                //lblex.SetProperty();
                Invalidate();
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                lblex.DrawEntity(e);
                rectex.DrawEntity(e);
                ellex.DrawEntity(e);
                chkex.DrawEntity(e);
            }    }
    }//画圆形的类
    public class EllipseEx : EntityBase
        {
            GraphicsPath gp = new GraphicsPath();        public EllipseEx(string name)
                : base()
            {
                
                this.Name = name;
                this.StartPoint = new Point(120, 100);
                this.Size = new Size(30, 30);
            }        public override void DrawEntity(PaintEventArgs e)
            {
                using (brush = new SolidBrush(Color.Red))
                {
                    g = e.Graphics;
                    gp.AddEllipse(Rect);
                    g.FillPath(brush, gp);
                }
            }        public override bool Contains(int X, int Y)
            {
                return gp.IsVisible(X, Y);
            }        public override void SetProperty()
            {
                throw new NotImplementedException();
            }
        }