using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void DrawLinePoint(PaintEventArgs e)
{ // Create pen.
Pen blackPen = new Pen(Color.Red, 3); // Create points that define line.
Point point1 = new Point(100, 100);
Point point2 = new Point(500, 100); // Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2);
}
}
}这是写的有问题的程序
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void DrawLinePoint(PaintEventArgs e)
{ // Create pen.
Pen blackPen = new Pen(Color.Red, 3); // Create points that define line.
Point point1 = new Point(100, 100);
Point point2 = new Point(500, 100); // Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2);
}
}
}这是写的有问题的程序
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawLinePoint(e);
}
在 窗体的 onpaint 事件中调用。但是什么时候通知窗体重绘不好确定,在界面上添加一个按钮,在事件处理程序中添加:
this.Invalidate (); 强制窗体重新绘制,点击后立即可以查看运行效果。