1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Threading.Tasks; using System.Windows.Forms;
namespace rubberInGDIplus { public partial class rubberEffectForm : Form { Pen rubPen = new Pen(Color.SpringGreen, 2); Point readPoi; bool useRubber = true; Graphics gp,gh; private Bitmap bitmap = null; public List<Point> poilst = new List<Point>();
public rubberEffectForm() { InitializeComponent(); this.Paint += new PaintEventHandler(this.rubberEffectForm_Paint); this.MouseClick += new MouseEventHandler(this.rubberEffectForm_Click); this.MouseMove += new MouseEventHandler(this.rubberEffectForm_MouseMove); this.KeyUp += new KeyEventHandler(this.rubberEffectForm_KeyUp); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true);
}
private void rubberEffectForm_Load(object sender, EventArgs e) {
} private void rubberEffectForm_Paint(object sender, PaintEventArgs e) { gh = e.Graphics; bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
gp = Graphics.FromImage(bitmap); gp.Clear(this.BackColor); gp.SmoothingMode = SmoothingMode.AntiAlias; if (useRubber && readPoi != null) { int plct = poilst.Count; if (plct == 0) { } else if (plct == 1) { gp.DrawLine(rubPen, poilst[0], readPoi); } else { for (int i = 0; i < plct; i++) { if (i == plct - 1) { gp.DrawLine(rubPen, poilst[0], readPoi); gp.DrawLine(rubPen, poilst[i], readPoi); } else { gp.DrawLine(rubPen, poilst[i], poilst[i + 1]); } } } } else if (useRubber == false && readPoi != null) { int plct = poilst.Count; if (plct == 0 | plct == 1) { } else { for (int i = 0; i < plct; i++) { if (i == plct - 1) { gp.DrawLine(rubPen, poilst[0], poilst[i]); } else { gp.DrawLine(rubPen, poilst[i], poilst[i + 1]); } } } }
gh.DrawImage(bitmap, 0, 0);
}
private void rubberEffectForm_Click(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) { useRubber = true; Point readPoint = this.PointToClient(Control.MousePosition); readPoi = readPoint; intimePoiLbl.Text = readPoint.ToString(); poilst.Add(readPoint);
} else if (e.Button == MouseButtons.Right) { useRubber = false; this.Refresh();
} else if (e.Button == MouseButtons.Middle) { int plast = poilst.Count - 1; poilst.RemoveAt(plast); useRubber = true; this.Refresh(); }
}
private void rubberEffectForm_MouseMove(object sender, MouseEventArgs e) { readPoi = this.PointToClient(Control.MousePosition); Graphics gw = this.CreateGraphics(); if (useRubber) { gw.Clear(BackColor); int plct = poilst.Count; if (plct == 0) { } else if (plct == 1) { gw.DrawLine(rubPen, poilst[0], readPoi); } else { for (int i = 0; i < plct; i++) { if (i == plct - 1) { gw.DrawLine(rubPen, poilst[i], readPoi); gw.DrawLine(rubPen, poilst[0], readPoi); } else { gw.DrawLine(rubPen, poilst[i], poilst[i + 1]); } } } } else { } intimePoiLbl.Text = readPoi.ToString(); }
private void rubberEffectForm_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { poilst.Clear(); this.Refresh(); } else if (e.KeyCode == Keys.Back | e.KeyCode == Keys.Escape) { int plast = poilst.Count - 1; poilst.RemoveAt(plast); useRubber = true; this.Refresh(); }
}
#region 可用可不用的函数 private void drawVertex(Graphics g, Point poi) { Size sz = new Size(4, 4); g.FillEllipse(Brushes.Red, new Rectangle(poi, sz)); } private void drawRim() { if (poilst.Count == 0) return; Point[] poi = new Point[poilst.Count]; for (int i = 0; i < poilst.Count; i++) { poi[i] = poilst[i]; } gp.DrawPolygon(new Pen(Color.Blue, 2), poi); } #endregion
} }
|