初步理解OnMouseDown和OnMouseUp

本学习来自MicroSoft Windows 程序设计,第304页

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace OnMouseDownDemo
 9 {
10     class Program:Form
11     {
12         Point ptBeg, ptEnd;
13 
14         static void Main(string[] args)
15         {
16             Application.Run(new Program());
17         }
18         public Program()
19         {
20             Text = "OnMouseDown Demo";
21             ForeColor = SystemColors.WindowText;
22             BackColor = SystemColors.Window;
23         }
24         protected override void OnMouseDown(MouseEventArgs e)
25         {
26             if (e.Button == MouseButtons.Left)
27             {
28                 ptBeg = ptEnd = new Point(e.X,e.Y);
29 
30                 Graphics grfx = CreateGraphics();
31                 grfx.DrawRectangle(new Pen(Color.Red),e.X,e.Y,30,40);
32                 //Invalidate();
33             }
34         }
35         protected override void OnMouseUp(MouseEventArgs e)
36         {
37             if (e.Button == MouseButtons.Left)
38             {
39                 ptBeg = ptEnd = new Point(e.X, e.Y);
40 
41                 Graphics grfx = CreateGraphics();
42                 grfx.DrawRectangle(new Pen(Color.Red), e.X, e.Y, 60, 80);
43                 //Invalidate();
44             }
45         }
46     }
47 }

 

 以上情况是:左键点击后立即松开

 

 以上情况是:先点击左键(不松开)会出现“小矩形”,而后在不松开左键的情况向右下角拖动,松开左键,就会出现“大矩形”。

由以上Demo试验可以看出,左键点下和左键松开是对应不同的e.x和e.y坐标的。

 

posted @ 2022-05-21 14:30  chenlight  阅读(67)  评论(0编辑  收藏  举报