c锐图(图形) [英] c sharp drawing (graphic)
本文介绍了c锐图(图形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在面板上创建图形时..如果我最小化和最大化窗体,则图形将被删除
如何防止清晰的图纸
我测试了flush()
和dispose()
-----------
When i create drawing on panel .. if i minimize and maximize the form, drawing will be erased
how can i prevent clear drawing
i test flush()
, and dispose()
-----------
private void button1_Click(object sender, EventArgs e)
{
priceValue();
Diagrams f2 = new Diagrams();
f2.Show();
Graphics g1 = f2.panel1.CreateGraphics();
g1.Clear(f2.panel1.BackColor);
Pen pen1 = new Pen(Color.Black,1);
g1.DrawLine(pen1, 100, 400, 600, 400);
g1.DrawLine(pen1, 100, 50, 100, 400);
Font myFont = new System.Drawing.Font("Ubuntu_Titling_Rg", 10, FontStyle.Bold);
Brush myBrush = new SolidBrush(System.Drawing.Color.Gold);
int i=0;
do
{
g1.DrawLine(pen1, 95, 50 + i, 100, 50 + i);
i += 70;
}
while (i <= 350);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
g1.DrawString(comboBox1.Text + " items cost schedule", myFont, myBrush, 15, 120, drawFormat);
g1.DrawString("0", myFont, myBrush, 70, 390);
g1.DrawString(lastPrice.ToString(), myFont, myBrush, 65, 320);
g1.DrawString(midP3rice.ToString(), myFont, myBrush, 60, 250);
g1.DrawString(midPrice.ToString(), myFont, myBrush, 57, 180);
g1.DrawString(mid2Price.ToString(), myFont, myBrush, 50, 110);
g1.DrawString(firstPrice.ToString(), myFont, myBrush, 50, 40);
Pen pen2 = new Pen(Color.FromArgb(30,60,153), 5);
Rectangle myRectangle = new Rectangle(5, 5, 658, 540);
g1.DrawRectangle(pen2, myRectangle);
RectangleF rect = new RectangleF(140, 110, 20, 290);
g1.DrawString("Main Items", myFont, myBrush, 300, 500);
int pos = 160;
for (int j = 0; j < treeView1.Nodes.Count; j++)
{
if(treeView1.Parent.HasChildren == true)
g1.DrawString(treeView1.Nodes[j].Text, myFont, myBrush, pos, 400, drawFormat);
pos += 460/4;
}
g1.Save();
g1.Dispose();
}
推荐答案
处理面板的Paint
事件,并将所有绘图代码放入该处理程序中.
Handle thePaint
event for your panel, and put all your drawing code inside that handler.
public Form1()
{
InitializeComponent();
//handle the Paint event for the panel
panel1.Paint += (sender, e) =>
{
//put you drawing code here
//and use e.Graphics instead of CreateGraphics
};
}
或不带lambda表达式:
Or without lambda expressions:
public Form1()
{
InitializeComponent();
//handle the Paint event for the panel
panel1.Paint += panel1_Paint;
}
//the painting function for panel1
private void panel1_Paint(object sender, PaintEventArgs e)
{
//put you drawing code here
//and use e.Graphics instead of CreateGraphics
e.Graphics.DrawString(...);
e.Graphics.DrawRectangle(...);
}
Olivier的答案的补充:
如果无法使用lambda(由于v.2.0),请使用不带lambda的匿名程序.它几乎具有lambda的所有优点,仅应明确指定参数类型:
Addition to the answer by Olivier:
If lambda cannot be used (because of v.2.0), use anonymous without lambda. It will have almost all the benefits of lambda, only parameters type should be explicitly specified:
panel1.Paint += delegate(object sender, System.EventArgs eventArgs)
{
//... same thing...
};
这篇关于c锐图(图形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文