📄 10.3.txt
字号:
Listing 10.3 Drawing Lines and Polygons in Response to Clicking on a Panel Control
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace _6_LinesAndPolygons
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel1;
private Point lastPoint = new Point(-1,-1);
private ArrayList alPoints;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
alPoints = new ArrayList();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer generated code
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void panel1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Graphics g = panel1.CreateGraphics();
Random rand = new Random( DateTime.Now.Millisecond );
int rc = rand.Next(255);
int gc = rand.Next(255);
int bc = rand.Next(255);
if( e.Button == MouseButtons.Left )
{
g.FillRectangle( new SolidBrush(Color.Black), e.X, e.Y, 5, 5 );
if( lastPoint.X != -1 && lastPoint.Y != -1 )
{
g.DrawLine( new Pen(new SolidBrush(Color.Black)),
lastPoint, new Point(e.X, e.Y ));
}
lastPoint.X = e.X;
lastPoint.Y = e.Y;
alPoints.Add( lastPoint );
}
else if( e.Button == MouseButtons.Right )
{
Point[] points = (Point[]) alPoints.ToArray( typeof(Point));
if( rand.Next(2) == 0 )
{
g.DrawPolygon( new Pen( new SolidBrush(
Color.FromArgb(rc,gc,bc ))), points );
}
else
{
g.FillPolygon( new SolidBrush( Color.FromArgb(rc,gc,bc )),
points );
}
alPoints.Clear();
lastPoint.X = -1;
lastPoint.Y = -1;
}
g.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -