用 GDI+画图 啊
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GDIDemo
{
public partial class Form1 : Form
{
public Point CurPnt = new Point();
public Color VaneClr = Color.Blue;
private Point FirstPnt = new Point(-1, -1);
private List<UsrLine> UlList = new List<UsrLine>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tscbType.SelectedIndex = 0;
}
private void pnMain_MouseMove(object sender, MouseEventArgs e)
{
CurPnt.X = e.X;
CurPnt.Y = e.Y;
pnMain.Invalidate();
}
public void DrawUsrLine(Graphics g, UsrLine ul)
{
Pen p = new Pen(VaneClr, 1);
switch (ul.TypeUl)
{
case UsrLineType.Line:
g.DrawLine(p, ul.Pnt1, ul.Pnt2);
break;
case UsrLineType.Rect:
int x = ul.Pnt1.X < ul.Pnt2.X ? ul.Pnt1.X : ul.Pnt2.X;
int y = ul.Pnt1.Y < ul.Pnt2.Y ? ul.Pnt1.Y : ul.Pnt2.Y;
int w = Math.Abs(ul.Pnt1.X - ul.Pnt2.X);
int h = Math.Abs(ul.Pnt1.Y - ul.Pnt2.Y);
g.DrawRectangle(p, x, y, w, h);
break;
}
}
private void pnMain_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < UlList.Count; i++)
{
DrawUsrLine(e.Graphics, UlList[i]);
}
UsrLine ulX = new UsrLine(UsrLineType.Line, new Point(CurPnt.X, 0), new Point(CurPnt.X, pnMain.Height));
UsrLine ulY = new UsrLine(UsrLineType.Line, new Point(0, CurPnt.Y), new Point(pnMain.Width, CurPnt.Y));
DrawUsrLine(e.Graphics, ulX);
DrawUsrLine(e.Graphics, ulY);
if (FirstPnt.X == -1 && FirstPnt.Y == -1) return;
UsrLine ulCur = new UsrLine((UsrLineType)tscbType.SelectedIndex, FirstPnt, CurPnt);
DrawUsrLine(e.Graphics, ulCur);
}
private void pnMain_MouseDown(object sender, MouseEventArgs e)
{
FirstPnt.X = e.X;
FirstPnt.Y = e.Y;
}
private void pnMain_MouseUp(object sender, MouseEventArgs e)
{
if (tscbType.SelectedIndex == -1) return;
UlList.Add(new UsrLine((UsrLineType)tscbType.SelectedIndex, FirstPnt, e.Location));
FirstPnt.X = FirstPnt.Y = -1;
pnMain.Invalidate();
}
private void tsbtClear_Click(object sender, EventArgs e)
{
UlList.Clear();
pnMain.Invalidate();
}
}
public enum UsrLineType { Line, Rect,}
public class UsrLine
{
public UsrLineType TypeUl = UsrLineType.Line;
public Point Pnt1 = new Point();
public Point Pnt2 = new Point();
public UsrLine(UsrLineType tp, Point pt1, Point pt2)
{
TypeUl = tp;
Pnt1.X = pt1.X;
Pnt1.Y = pt1.Y;
Pnt2.X = pt2.X;
Pnt2.Y = pt2.Y;
}
}
}