[問題] 請教關於繪圖時的控制事件問題!
各位好..想請教一個問題,
我在Windows Form上利用一個PictureBox來顯示圖片,之後要再PictureBox的圖片上面畫出
矩型..一開始的做法如下:
**開啟圖檔**
---------------------------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
ClassFileFunction tempObject = new ClassFileFunction();
Graphics g = pictureBox1.CreateGraphics();
tempObject.OpenFile();
txt_OpenImagePath.Text = tempObject.OpenFileName;
pictureBox1.Image = Image.FromFile(txt_OpenImagePath.Text);
//g.DrawImage(Image.FromFile(txt_OpenImagePath.Text), 0, 0, 400, 600);
//g.DrawImage(new Bitmap(txt_OpenImagePath.Text), 0, 0,400,600);
txt_SystemStatus.Text = "開啟圖檔已完成!";
}
----------------------------------------------------------------------------------------
**繪出矩形**
----------------------------------------------------------------------------------------
private void button2_Click(object sender, EventArgs e)
{
ClassDrawFunction tempDraw = new ClassDrawFunction();
tempDraw.SetPen(Color.FromArgb(255,200,0,0));
tempDraw.SetRectPositon(192, 341, 290, 341, 192, 396, 290, 396, 4, 4);
tempDraw.DrawRect(ref pictureBox1);
flag = true;
}
-----------------------------------------------------------------------------------------
但我發現我在按下視窗的最小化,再把他最大化之後..我所在PictureBox上畫的矩形會不見..
為了解決這個問題..我覆寫Form的Paint事件...來重新畫矩形..
如下:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.WindowState.ToString() == "Maximized" && flag == true)
{
ClassDrawFunction tempDraw = new ClassDrawFunction();
tempDraw.SetPen(Color.FromArgb(255, 200, 0, 0));
tempDraw.SetRectPositon(192, 341, 290, 341, 192, 396, 290, 396, 4, 4);
tempDraw.DrawRect(ref pictureBox1);
};
}
----------------------------------------------------------------------------------------
這樣我在重新按下最大化後,矩形會繪出沒問題..但我發現..Onpaint事件執行了好幾次..
也就是他的順序是: 重繪Form_Onpaint ==>重繪PictureBox==>再重繪Form_Onpaint...
所以我Form_Onpaint的程式碼執行了好幾次...
我目前的想法是想找到可以判斷PictureBox已經重繪完成或是可以判斷PictureBox已經載入圖片的方法
或是有哪一個事件可以判斷Form裡所有的控制
為了效能的問題..Onpaint的程式碼不想重覆執行..
麻煩你們了!謝謝大家!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.117.59.49
推
08/27 10:27, , 1F
08/27 10:27, 1F
→
08/28 15:34, , 2F
08/28 15:34, 2F
→
08/28 15:35, , 3F
08/28 15:35, 3F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章