实例介绍
【实例简介】
【实例截图】
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | private Main main; //直接对Main窗体进行控制 private APIMethod api = new APIMethod(); //调用API的完成类(将API的调用封装起来,是可以直接调用产生效果的类) private Rectangle area = Rectangle.Empty; //截图矩形 private Point op; //area起点位置 private Button b = new Button(); //CancelButton private int index = -1; private bool draw = false ; private bool inited = false ; //确定是否已经完成area的绘制,以确定是否要显示矩形大小 private bool drawing = false ; //确定是否进入画图状态,用来清除多余的GDI 图形 private int type = -1; //确定GDI 的类型 private Graphics graphics; //进行GDI 绘图 private Point sl; //初始位置 private bool md = false ; //判断是否截图,如果发生MouseDown事件则开始截图,值为true private bool isdrew = false ; //是否使用过GDI ,如果使用过则不能拖动area private Pen pen; //GDI 使用的画笔 GraphicsPath points; //画笔工具 private Image img; //GDI 绘图用的背景 private Image origin; //原始桌面 private bool fd = false ; //确定是否进行文字绘制 private bool drew = false ; //控制是否在重绘事件中绘制area public Snipping() { InitializeComponent(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager( typeof (Snipping)); ·imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11015" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11014" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11016" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11017" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11018" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11024" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "CameraDll_dll_11025" ))); imageList1.Images.Add((System.Drawing.Icon)(resources.GetObject( "comres_1_2857" ))); pictureBox1.Image = imageList1.Images[0]; pictureBox2.Image = imageList1.Images[1]; pictureBox3.Image = imageList1.Images[2]; pictureBox4.Image = imageList1.Images[3]; pictureBox5.Image = imageList1.Images[4]; pictureBox6.Image = imageList1.Images[5]; pictureBox7.Image = imageList1.Images[6]; pictureBox8.Image = imageList1.Images[7]; b.Click = new EventHandler(b_Click); this .CancelButton = b; } private void Snipping_Load( object sender, EventArgs e) { main = (Main) this .Owner; this .Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; this .BackgroundImage = origin = api.GetScreenSnapShot(); graphics = this .CreateGraphics(); pen = new Pen(label9.BackColor); points = new GraphicsPath(); comboBox1.SelectedIndex = 0; this .SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true ); this .UpdateStyles(); } private void b_Click( object sender, EventArgs e) { api.ReleaseCursor(); this .Dispose(); } #endregion #region DrawLine protected override void OnMouseDown(MouseEventArgs e) { base .OnMouseDown(e); if ( this .area == Rectangle.Empty && e.Button == MouseButtons.Left) { this .area.Location = new Point(e.X, e.Y); } this .op = new Point(e.X, e.Y); this .index = this .GetSelectedHandle( new Point(e.X, e.Y)); this .SetCursor(); draw = true ; if ( this .area.Contains(e.Location) && drawing) { sl = e.Location; md = true ; RECT rect = new RECT(); rect.left = this .area.Left; rect.right = this .area.Right; rect.top = this .area.Top; rect.bottom = this .area.Bottom; WindowsAPI.ClipCursor( ref rect); } if (textBox1.Visible) { graphics = Graphics.FromImage(origin); graphics.DrawString(textBox1.Text, textBox1.Font, new SolidBrush(label9.BackColor), new RectangleF( new Point(textBox1.Location.X, textBox1.Location.Y 2), textBox1.Size)); graphics = Graphics.FromImage(img); graphics.DrawString(textBox1.Text, textBox1.Font, new SolidBrush(label9.BackColor), new RectangleF( new Point(textBox1.Location.X, textBox1.Location.Y 2), textBox1.Size)); graphics = this .CreateGraphics(); graphics.DrawImage(img, new Point(0, 0)); textBox1.Text = "" ; textBox1.Visible = false ; api.ReleaseCursor(); fd = false ; } } protected override void OnMouseMove(MouseEventArgs e) { base .OnMouseMove(e); if (drawing && md) { DrawGDI(type, e.Location); if (type == 2) fd = true ; } else { if ( this .Capture) { this .MoveHandleTo( new Point(e.X, e.Y)); this .Invalidate(); } else { this .index = this .GetSelectedHandle( new Point(e.X, e.Y)); this .SetCursor(); } } } protected override void OnMouseUp(MouseEventArgs e) { if (! this .IsDisposed) { base .OnMouseUp(e); int left = area.Left; int top = area.Top; int right = area.Right; int bottom = area.Bottom; area.X = Math.Min(left, right); area.Y = Math.Min(top, bottom); area.Width = Math.Abs(left - right); area.Height = Math.Abs(top - bottom); if (e.Button == MouseButtons.Right) { if ( this .area == Rectangle.Empty) { this .DialogResult = DialogResult.Cancel; } else { this .area = Rectangle.Empty; this .Invalidate(); } } this .index = this .GetSelectedHandle( new Point(e.X, e.Y)); this .SetCursor(); if (drawing && md) { if (type == 2 && fd) { md = false ; textBox1.Visible = true ; textBox1.Focus(); } else { DrawGDI(type, e.Location); md = false ; DrawGDI(type, e.Location); api.ReleaseCursor(); if (type == 4) points.Reset(); } } draw = false ; if (!inited) inited = true ; this .Refresh(); } } private void DrawGDI( int drawtype, Point p) { PointF rectStartPointF = sl; switch (drawtype) { case 0: { float width = Math.Abs(p.X - sl.X); float heigth = Math.Abs(p.Y - sl.Y); if (p.X < sl.X) { rectStartPointF.X = p.X; } if (p.Y < sl.Y) { rectStartPointF.Y = p.Y; } if (!md) { graphics = Graphics.FromImage(origin); graphics.DrawRectangle(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = Graphics.FromImage(img); graphics.DrawRectangle(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = this .CreateGraphics(); graphics.DrawImage(img, new Point(0, 0)); } else { Image image = (Image) this .img.Clone(); graphics = Graphics.FromImage(image); graphics.DrawRectangle(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = this .CreateGraphics(); graphics.DrawImage(image, new Point(0, 0)); } isdrew = true ; break ; } case 1: { float width = Math.Abs(p.X - sl.X); float heigth = Math.Abs(p.Y - sl.Y); if (p.X < sl.X) { rectStartPointF.X = p.X; } if (p.Y < sl.Y) { rectStartPointF.Y = p.Y; } if (!md) { graphics = Graphics.FromImage(origin); graphics.DrawEllipse(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = Graphics.FromImage(img); graphics.DrawEllipse(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = this .CreateGraphics(); graphics.DrawImage(img, new Point(0, 0)); } else { Image image = (Image) this .img.Clone(); graphics = Graphics.FromImage(image); graphics.DrawEllipse(pen, rectStartPointF.X, rectStartPointF.Y, width, heigth); graphics = this .CreateGraphics(); graphics.DrawImage(image, new Point(0, 0)); } isdrew = true ; break ; } case 2: { float width = Math.Abs(p.X - sl.X); float heigth = Math.Abs(p.Y - sl.Y); if (p.X < sl.X) { rectStartPointF.X = p.X; } if (p.Y < sl.Y) { rectStartPointF.Y = p.Y; } Image image = (Image) this .img.Clone(); graphics = Graphics.FromImage(image); graphics.DrawRectangle(Pens.Black, rectStartPointF.X, rectStartPointF.Y, width, heigth); textBox1.Size = new Size(( int )width, ( int )heigth); textBox1.Location = sl; graphics = this .CreateGraphics(); graphics.DrawImage(image, new Point(0, 0)); isdrew = true ; break ; } case 3: { if (!md) { graphics = Graphics.FromImage(origin); pen.EndCap = LineCap.ArrowAnchor; pen.Width = 4; graphics.DrawLine(pen, sl, p); graphics = Graphics.FromImage(img); graphics.DrawLine(pen, sl, p); pen.Width = 1; pen.EndCap = LineCap.NoAnchor; graphics = this .CreateGraphics(); graphics.DrawImage(img, new Point(0, 0)); } else { Image image = (Image) this .img.Clone(); graphics = Graphics.FromImage(image); pen.EndCap = LineCap.ArrowAnchor; pen.Width = 4; graphics.DrawLine(pen, sl, p); pen.Width = 1; pen.EndCap = LineCap.NoAnchor; graphics = this .CreateGraphics(); graphics.DrawImage(image, new Point(0, 0)); } isdrew = true ; break ; } case 4: { if (!md) { points.AddLine(p, p); graphics = Graphics.FromImage(origin); graphics.DrawPath(pen, points); graphics = Graphics.FromImage(img); graphics.DrawPath(pen, points); graphics = this .CreateGraphics(); graphics.DrawImage(img, new Point(0, 0)); } else { graphics = this .CreateGraphics(); points.AddLine(p, p); graphics.DrawPath(pen, points); } isdrew = true ; break ; } } drew = true ; } protected override void OnPaint(PaintEventArgs e) { base .OnPaint(e); if (drawing) return ; if (!drew) { e.Graphics.DrawRectangle( new Pen( this .ForeColor), this .area); for ( int i = 1; i < 9; i ) { e.Graphics.FillRectangle( new SolidBrush(Color.Blue), this .GetHandleRect(i)); } } if (draw && !inited) { Point p = new Point(area.Right, area.Bottom); string str = Math.Abs(area.Width).ToString() " × " Math.Abs(area.Height).ToString(); e.Graphics.DrawString(str, new Font( "宋体" , 12, FontStyle.Bold), Brushes.White, new Point(p.X, p.Y - 30)); e.Graphics.FillRectangle(Brushes.Black, new Rectangle( new Point(p.X, p.Y - 30), new Size(( int )e.Graphics.MeasureString(str, new Font( "宋体" , 12, FontStyle.Bold)).Width 4, 20))); e.Graphics.DrawString(str, new Font( "宋体" , 12, FontStyle.Bold), Brushes.White, new Point(p.X, p.Y - 30)); } if (inited && !drawing) { pictureBox1.Visible = true ; pictureBox1.Location = new Point( this .area.Left, this .area.Bottom 5); pictureBox2.Visible = true ; pictureBox2.Location = new Point( this .area.Left 18, this .area.Bottom 5); pictureBox8.Visible = true ; pictureBox8.Location = new Point( this .area.Left 36, this .area.Bottom 5); pictureBox3.Visible = true ; pictureBox3.Location = new Point( this .area.Left 54, this .area.Bottom 5); pictureBox4.Visible = true ; pictureBox4.Location = new Point( this .area.Left 72, this .area.Bottom 5); pictureBox5.Visible = true ; pictureBox5.Location = new Point( this .area.Left 90, this .area.Bottom 5); pictureBox6.Visible = true ; pictureBox6.Location = new Point( this .area.Left 108, this .area.Bottom 5); pictureBox7.Visible = true ; pictureBox7.Location = new Point( this .area.Left 126, this .area.Bottom 5); } } #endregion #region DrawAREA private int GetSelectedHandle(Point p) { int index = -1; for ( int i = 1; i < 9; i ) { if (GetHandleRect(i).Contains(p)) { index = i; break ; } } if ( this .area.Contains(p)) index = 0; System.Diagnostics.Trace.WriteLine(area.ToString()); System.Diagnostics.Trace.WriteLine(p.ToString()); System.Diagnostics.Trace.WriteLine(index.ToString()); return index; } private void MoveHandleTo(Point point) { if (isdrew) return ; int left = area.Left; int top = area.Top; int right = area.Right; int bottom = area.Bottom; switch (index) { case 0: area.X = point.X - op.X; area.Y = point.Y - op.Y; this .op = point; return ; case 1: left = point.X; top = point.Y; break ; case 2: top = point.Y; break ; case 3: right = point.X; top = point.Y; break ; case 4: right = point.X; break ; case 5: right = point.X; bottom = point.Y; break ; case 6: bottom = point.Y; break ; case 7: left = point.X; bottom = point.Y; break ; case 8: left = point.X; break ; } this .op = point; area.X = left; area.Y = top; area.Width = right - left; area.Height = bottom - top; } private void SetCursor() { Cursor cr = Cursors.Default; if (index == 1 || index == 5) { cr = Cursors.SizeNWSE; } else if (index == 2 || index == 6) { cr = Cursors.SizeNS; } else if (index == 3 || index == 7) { cr = Cursors.SizeNESW; } else if (index == 4 || index == 8) { cr = Cursors.SizeWE; } else if (index == 0) { cr = Cursors.Default; } Cursor.Current = cr; } private Rectangle GetHandleRect( int index) { Point point = GetHandle(index); return new Rectangle(point.X - 3, point.Y - 3, 5, 5); } private Point GetHandle( int index) { int x, y, xCenter, yCenter; xCenter = area.X area.Width / 2; yCenter = area.Y area.Height / 2; x = area.X; y = area.Y; switch (index) { case 1: x = area.X; y = area.Y; break ; case 2: x = xCenter; y = area.Y; break ; case 3: x = area.Right; y = area.Y; break ; case 4: x = area.Right; y = yCenter; break ; case 5: x = area.Right; y = area.Bottom; break ; case 6: x = xCenter; y = area.Bottom; break ; case 7: x = area.X; y = area.Bottom; break ; case 8: x = area.X; y = yCenter; break ; } return new Point(x, y); } #endregion |
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)