下面代碼中的按鈕點擊函數我想在picureBox中創建一個矩形,但是當我第一次執行它時什麼都不畫,但是第二次單擊矩形會出現,爲什麼?在PictureBox中使用PictureBox.CreateGraphics創建矩形
if語句在兩個時間都是true。
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();
}
CreateGraphic是一個*臨時*圖形 - 如果您最小化窗口,它很容易被擦除。使用繪畫事件並使用提供的「e.Graphics」對象來繪製。 – LarsTech
如何從button_click()中調用它? – abdolah
你使控件無效:'pictureBox1.Invalidate();' – LarsTech