簡體   English   中英

使用PictureBox.CreateGraphics在PictureBox中創建矩形

[英]Creating Rectangle in PictureBox using PictureBox.CreateGraphics

在按鈕點擊功能下面的代碼我想在picureBox中創建一個矩形,但是當我第一次執行它時它什么都沒畫,但第二次點擊矩形出現,為什么?

if語句在兩個時間都是真的。

    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();

    }

因為您在Click事件上執行此操作。 您應該在Paint事件上執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM