Windows opaque UserControl不刷新对其进行的任何图形更改

时间:2012-11-02 04:51:59

标签: c# graphics grid

我创建了一个Windows UserControl。它实际上使用Grid绘制Graphics(即垂直和水平线)。用户可以更改每个单元格的高度和宽度,并根据Grid进行刷新。覆盖我创建网格的OnPaint事件。我使用SetStyle(ControlStyles.Opaque, true)使其透明。我在窗体上使用了这个控件,然后从那里我改变了单元格高度和宽度的值,但由于不透明,新网格在前一个网格上重叠并使其变得笨拙。我该如何解决这个问题?

UserControl代码:

public partial class Grid : UserControl
{
    public Grid()
    {
        InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
    }
    private float _CellWidth = 10, _CellHeight = 10;
    private Color _GridColor = Color.Black;
    public float CellWidth
    {
        get
        {
            return this._CellWidth;
        }
        set
        {
            this._CellWidth = value;
        }
    }
    public float CellHeight
    {
        get
        {
            return this._CellHeight;
        }
        set
        {
            this._CellHeight = value;
        }
    }
    public Color GridColor
    {
        get
        {
            return this._GridColor;
        }
        set
        {
            this._GridColor = value;
        }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g;
        float iHeight = this.Height;
        float iWidth = this.Width;
        g = e.Graphics;  
        Pen myPen = new Pen(GridColor);
        myPen.Width = 1;
        if (this.CellWidth > 0 && this.CellHeight > 0)
        {
            for (float X = 0; X <= iWidth; X += this.CellWidth)
            {
                g.DrawLine(myPen, X, 0, X, iHeight);
            }
            for (float Y = 0; Y <= iHeight; Y += this.CellHeight)
            {
                g.DrawLine(myPen, 0, Y, iWidth, Y);
            }
        }
    }
    public override void Refresh()
    {
        base.ResumeLayout(true);
        base.Refresh();
        ResumeLayout(true);            
    }
}

表单代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }        

    private void Form1_Load(object sender, EventArgs e)
    {   

    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
        try
        {
            if (ofdImage.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pbImage.Image = Image.FromFile(ofdImage.FileName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnShowGrid_Click(object sender, EventArgs e)
    {
        if (grid1.Visible)
        {
            grid1.Visible = false;
            btnShowGrid.Text = "Show";
        }
        else
        {
            grid1.Visible = true;
            btnShowGrid.Text = "Hide";
        }
    }

    private void btnGridCellMaximize_Click(object sender, EventArgs e)
    {
        grid1.CellHeight += 1;
        grid1.CellWidth += 1;            
        grid1.Refresh();
    }

    private void btnGridCellMinimize_Click(object sender, EventArgs e)
    {
        grid1.CellHeight -= 1;
        grid1.CellWidth -= 1;            
        grid1.Refresh();
    }
}

0 个答案:

没有答案