.net C#Forms-如何在Form#U Paint方法之外绘制?
我制作了一个Windows窗体应用程序,并举办了一次绘画活动:.net C#Forms-如何在Form#U Paint方法之外绘制?,.net,forms,paint,draw,.net,Forms,Paint,Draw,我制作了一个Windows窗体应用程序,并举办了一次绘画活动: private void Form1_Paint(object sender, PaintEventArgs e) { Font title = new Font("Calibari", 40); e.Graphics.DrawString("Hello World!", title, new SolidBrush(Color.Blue), new Point(200, 200));
private void Form1_Paint(object sender, PaintEventArgs e)
{
Font title = new Font("Calibari", 40);
e.Graphics.DrawString("Hello World!", title, new SolidBrush(Color.Blue), new Point(200, 200));
}
我还有一个计时器,它每5毫秒滴答一次,每次滴答一次。Refresh()就会这样做(以防我画出包含变量的字符串,然后我必须更新它们)
但我可以在绘画活动之外画线吗
例如,当用户单击按钮时绘制字符串。我该怎么做呢?为了充实我的评论,这是我建议创建一个类来存储信息的第一种方法。如果字符串的数量是动态的,这将起作用。否则,第二种选择更容易
public partial class Form1 : Form
{
List<MyStringInformation> myTextInfo = new List<MyStringInformation>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyStringInformation info = new MyStringInformation();
info.myFont = new Font("Calibari", 40);
info.myText = "Hello World";
info.myLocation = new Point(200, 200);
info.myColor = Color.Blue;
myTextInfo.Add(info);
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (var item in myTextInfo)
{
e.Graphics.DrawString(item.myText, item.myFont, new SolidBrush(item.myColor), item.myLocation);
}
}
}
public class MyStringInformation
{
public Font myFont { get; set; }
public string myText { get; set;}
public Point myLocation { get; set;}
public Color myColor { get; set;}
}
是的,您可以,但是一旦表单无效或刷新,它们就会消失。任何你需要坚持到你的形式的东西都需要在绘画活动中,但这是一个问题。。。例如,若我想在按下按钮后显示一个字符串,那个么我如何在绘制事件中执行这样的操作呢?我将构建一个您试图使用的信息类,创建一个列表,然后在绘制事件中遍历它。我将试着举一个简单的例子。我以前也用过隐藏标签,只是把它们的信息画在屏幕上,我没想过。我绝对要用它。非常感谢你!
public partial class Form1 : Form
{
Label myTitle = new Label();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myTitle.Font = new Font("Calibari", 40);
myTitle.Text = "Hello World";
myTitle.Location = new Point(200, 200);
myTitle.ForeColor = Color.Blue;
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(myTitle.Text, myTitle.Font, new SolidBrush(myTitle.ForeColor), myTitle.Location);
}
}