首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PictureBox使用PictureBox.CreateGraphics创建矩形

使用PictureBox使用PictureBox.CreateGraphics创建矩形
EN

Stack Overflow用户
提问于 2013-10-29 17:04:52
回答 1查看 2.7K关注 0票数 0

在下面的代码中,在按钮单击函数中,我想在picureBox中创建一个矩形,但是当我第一次执行它时,它没有绘制任何东西,但是第二次单击矩形会出现,为什么?

if语句在两段时间都是正确的。

代码语言:javascript
复制
    private void btnChamber_Click(object sender, EventArgs e)
    {

        //pictureBox1.Visible = true;

        if (cmbShowResult.SelectedIndex == 0 && ChamberType.SelectedIndex == 0)
        {
            chart1.Visible = false;
            chart2.Visible = false;
            chart3.Visible = false;
            chart4.Visible = false;
            chart5.Visible = false;
            pictureBox1.Visible = true;
            //pictureBox1.Enabled = true;
            Graphics gg = pictureBox1.CreateGraphics();

            //gg.ClipBounds.X = 0;
            //gg.ClipBounds.Y = 0;
            //AddChartStyle(gg);
            chartArea1.X = 0;
            chartArea1.Y = 0;
            chartArea1.Height = 298;
            chartArea1.Width = 450;


            plotArea.X = 40;
            plotArea.Y = 10;
            plotArea.Height = 258;
            plotArea.Width = 400;



            xLimMin = -(float.Parse(txtWidth.Text)) / 2;
            xLimMax = (float.Parse(txtWidth.Text)) / 2;
            yLimMin = -(float.Parse(txtLeft.Text)) * 3f;
            yLimMax = (float.Parse(txtLeft.Text)) * 3f;

            Pen aPen = new Pen(chartBorderColor, 1f);
            SolidBrush aBrush = new SolidBrush(chartBorderColor);
            gg.FillRectangle(aBrush, chartArea1);
            gg.DrawRectangle(aPen, chartArea1);
            aPen = new Pen(plotBorderColor, 1f);
            aBrush = new SolidBrush(plotBackColor);
            gg.FillRectangle(aBrush, plotArea);

            gg.DrawRectangle(aPen, plotArea);
            //AddChartStyle(g);

            //pictureBox1.Visible = true;
            //gg = pictureBox1.CreateGraphics();


            //gg.Flush();
            //gg.Save();
            //pictureBox1.Show();
            //pictureBox1.Invalidate();
            //chart6.Visible = true; 

        }
        pictureBox1.Visible = true;
        //gg.Flush();

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-29 17:21:42

因为你是在点击事件上这么做的。你应该在油漆事件上这么做。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19664718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档