Invalidate()函数是否自动触发Form1_Paint?namespace Tes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Point point1;
public Point point2; private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen myPen=new Pen(Color.Azure);
e.Graphics.DrawLine(myPen, point1, point2);
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
point1.X= e.X;
point1.Y = e.Y;
point2.X = e.X + 100;
point2.Y = e.Y + 100;
this.Invalidate();
int i = 0;
}
}
}我在this.Invalidate()处设断点,执行不到Form1_Paint(...)里去,为什么???
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Point point1;
public Point point2; private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen myPen=new Pen(Color.Azure);
e.Graphics.DrawLine(myPen, point1, point2);
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
point1.X= e.X;
point1.Y = e.Y;
point2.X = e.X + 100;
point2.Y = e.Y + 100;
this.Invalidate();
int i = 0;
}
}
}我在this.Invalidate()处设断点,执行不到Form1_Paint(...)里去,为什么???
只有当窗口的消息循环从消息队列中取出消息并进行处理,Form_Paint()才会得到执行。
由于队列中多个WM_PAINT消息会被合并成一个,多次调用Invalidate()可能最终只触发一次Form1_Paint。
没有整过Winform的
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)语法
Visual Basic(声明)
Protected Overridable Sub OnPaint ( _
e As PaintEventArgs _
)
Visual Basic(用法)
Dim e As PaintEventArgsMe.OnPaint(e)
C#
protected virtual void OnPaint(
PaintEventArgs e
)
Visual C++
protected:
virtual void OnPaint(
PaintEventArgs^ e
)
J#
protected void OnPaint(
PaintEventArgs e
)
JScript
protected function OnPaint(
e : PaintEventArgs
)
参数
e
类型:System.Windows.Forms..::.PaintEventArgs包含事件数据的 PaintEventArgs。备注
引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件。OnPaint 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。继承者说明:在派生类中重写 OnPaint 时,一定要调用基类的 OnPaint 方法,以便已注册的委托对事件进行接收。示例
下面的代码示例使用户能够将图像或图像文件拖到窗体上,并使它在放置点显示。每次绘制窗体时,都重写 OnPaint 方法以重新绘制图像;否则图像将保持到下一次重新绘制。DragEnter 事件处理方法决定拖到窗体中的数据的类型,并提供适当的反馈。如果 Image 可以从该数据中创建,则 DragDrop 事件处理方法就会在该窗体上显示此图像。因为 DragEventArgs..::.X 和 DragEventArgs..::.Y 值为屏幕坐标,所以示例使用 PointToClient 方法将它们转换成工作区坐标。
private Image picture;
private Point pictureLocation;public Form1()
{
// Enable drag-and-drop operations and
// add handlers for DragEnter and DragDrop.
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
}protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
if(this.picture != null && this.pictureLocation != Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}private void Form1_DragDrop(object sender, DragEventArgs e)
{
// Handle FileDrop data.
if(e.Data.GetDataPresent(DataFormats.FileDrop) )
{
// Assign the file names to a string array, in
// case the user has selected multiple files.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
try
{
// Assign the first image to the picture variable.
this.picture = Image.FromFile(files[0]);
// Set the picture location equal to the drop point.
this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
} // Handle Bitmap data.
if(e.Data.GetDataPresent(DataFormats.Bitmap) )
{
try
{
// Create an Image and assign it to the picture variable.
this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
// Set the picture location equal to the drop point.
this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
// Force the form to be redrawn with the image.
this.Invalidate();
}private void Form1_DragEnter(object sender, DragEventArgs e)
{
// If the data is a file or a bitmap, display the copy cursor.
if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
e.Data.GetDataPresent(DataFormats.FileDrop) )
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}// This example creates a PictureBox control on the form and draws to it.
// This example assumes that the Form_Load event handler method is
// connected to the Load event of the form.private PictureBox pictureBox1 = new PictureBox();
private void Form1_Load(object sender, System.EventArgs e)
{
// Dock the PictureBox to the form and set its background to white.
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.BackColor = Color.White;
// Connect the Paint event of the PictureBox to the event handler method.
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); // Add the PictureBox control to the Form.
this.Controls.Add(pictureBox1);
}private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics; // Draw a string on the PictureBox.
g.DrawString("This is a diagonal line drawn on the control",
new Font("Arial",10), System.Drawing.Brushes.Blue, new Point(30,30));
// Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, pictureBox1.Top,
pictureBox1.Right, pictureBox1.Bottom);
}