我正在使用C#開發一個簡單的工具,其中有一個面板內的圖片框。面板的屬性Autoscroll = true。如果該圖片框的圖像大於面板,則該面板具有滾動條滾動面板後恢復圖形
我可以在圖片框的繪畫事件上畫一個矩形。但是當我滾動,這個矩形消失。我知道在移動滾動條後需要重新繪製它,但我不知道如何重新恢復它。
x,y,width,heigth,zoom是全局變量,當使用點擊進入treenode時,它會有數據。
private void pictureBoxView_Paint(object sender, PaintEventArgs e)
{
if (choose == true)
{
Size newSize = new Size((int)(pictureBoxView.Image.Width * zoom),
(int)(pictureBoxView.Image.Height * zoom));
Graphics graphic = pictureBoxView.CreateGraphics();
Pen pen = new Pen(Color.Red, 3);
graphic.DrawRectangle(pen, x, y, width, height);
pen.Dispose();
}
}
private void treeViewTemplate_AfterSelect(object sender, TreeViewEventArgs e)
{
// refresh picturebox
pictureBoxView.Refresh();
// allow repaint
choose = true;
string[] value = treeViewTemplate.SelectedNode.Tag.ToString().Split(',');
x = Int32.Parse(value[0]);
y = Int32.Parse(value[1]);
width = Int32.Parse(value[2]);
height = Int32.Parse(value[3]);
zoom = Double.Parse(value[4]);
//MessageBox.Show("x = " + y + ", y = " + y + ", width = " + width + ", height = " + height + ", zoom = " + zoom);
// This call draw a rectangle again when I choose a value from TreeNode's Tag
pictureBoxView_Paint(this, null);
}
或者乾脆就是如何在圖片框上繪製矩形,即使面板被重新繪製? – 2013-02-20 08:16:01