Hallo Leute,
folgender Code soll , ohne PainEventArgs e zu verwenden, außerhalb einer PictureBox einen String auf ein WinForms-Formular zeichnen. Tut er aber nicht. Zumindest nicht dann, wenn die Methode im Konstruktor aufgerufen wird. Erst, wenn ich den Mauszeiger pushe,
wird der String gezeichnet. Woran könnte das liegen?
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp2 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.DrawString(); //erwirkt nix! Warum?
}
private void DrawString() {
Graphics formGraphics = this.CreateGraphics();
string drawString = "Demo Text 2Be Shown on screen";
Font drawFont = new Font("Arial", 22);
SolidBrush drawBrush = new SolidBrush(Color.Red);
float x = 270.0F;
float y = 150.0F;
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
lblX.Text = MousePosition.ToString();
lblX.Text = "X:" + e.X + " Y:" + e.Y;
this.DrawString(); //String wird gezeichnet
}
}
}