wil*_*ace 2 c# panel .refresh repaint
我试图调用panel1 paint方法用橙色线重新绘制面板(它用蓝线启动).
我已经尝试过invalidate(),update()和refresh(),但似乎没有任何东西可以调用panel1的paint事件......
paint事件处理程序已添加到panel1:
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 testForm = new Form1();
Application.Run(testForm);
testForm.drawNewLine();
}
}
Run Code Online (Sandbox Code Playgroud)
public partial class Form1 : Form
{
bool blueLine = true;
bool orangeLine = false;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (blueLine == true)
{
Pen bluePen = new Pen(Color.Blue, 3);
g.DrawLine(bluePen, 30, 50, 30, 250);
}
else if (orangeLine == true)
{
Pen orangePen = new Pen(Color.Orange, 3);
g.DrawLine(orangePen, 30, 50, 30, 250);
}
g.Dispose();
}
public void drawNewLine()
{
blueLine = false;
orangeLine = true;
//panel1.Invalidate();
//panel1.Update();
panel1.Refresh();
}
}
Run Code Online (Sandbox Code Playgroud)
Application.Run(testForm);阻止窗体关闭,所以当drawNewLine()调用时 - 窗体不再存在(创建一个按钮,在点击时调用它并检查自己,代码正常工作).Invalidate()应该工作得很好.
此外,您不应该处理Graphics在paint事件中传递给您的代码的对象.你不负责创建它,所以让创建它的代码销毁它.
此外,处理Pen对象,因为您正在创建它们.
| 归档时间: |
|
| 查看次数: |
11777 次 |
| 最近记录: |