国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

用C# 實(shí)現(xiàn)鼠標(biāo)框選效果的實(shí)現(xiàn)代碼

2020-01-26 16:13:34
字體:
供稿:網(wǎng)友

實(shí)現(xiàn)步驟:

1.實(shí)現(xiàn)整個(gè)鼠標(biāo)框選的幾個(gè)事件(down、move、up),當(dāng)鼠標(biāo)點(diǎn)下記錄鼠標(biāo)框選的起點(diǎn),鼠標(biāo)抬起結(jié)束操作。

2.以鼠標(biāo)框選過程中獲取的鼠標(biāo)坐標(biāo)為基點(diǎn)計(jì)算框選的矩形的4點(diǎn)坐標(biāo),4點(diǎn)坐標(biāo)以順時(shí)針方向布點(diǎn)。

3.通過Shape.Path類實(shí)現(xiàn)在類上畫出此矩形。

代碼如下:

復(fù)制代碼 代碼如下:

namespace HostDemo {
 public class HostCanvas : Canvas {
  public HostCanvas() {
   InitializeComponent();
  }

  private void InitializeComponent() {
   this.Loaded += OnLoad;
   this.MouseDown += OnMouseDown;
   this.MouseMove += OnMouseMove;
   this.MouseUp += OnMouseUp;
   locus = new Path();
   locus.Fill = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255));
   locus.Stroke = Brushes.Red;
   locus.StrokeThickness = 1;
   locus.IsManipulationEnabled = true;
  }

  void OnMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
   ispath = false;
  }

  void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
   if(ispath){
    endpoint = e.GetPosition(this);
    locus.Data = DrawingRect(startpoint,endpoint);
   }
  }

  void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
   if(!this.Children.Contains(locus)) this.Children.Add(locus);
   if (locus.Data != null) locus.Data = null;
   startpoint = e.GetPosition(this);
   ispath = true;
  }

  void OnLoad(object sender, System.Windows.RoutedEventArgs e) {
   this.Background = new SolidColorBrush(Color.FromArgb(35, 255, 255, 255));
  }

  private PathGeometry DrawingRect(Point beginpoint, Point closepoint) {
   PathGeometry result = new PathGeometry(); 
   PathFigure figure = new PathFigure();
   figure.IsClosed = true;
   figure.StartPoint = beginpoint;
   PathSegmentCollection pathSegmentCollection = new PathSegmentCollection();
   PathFigureCollection pathFigureCollection = new PathFigureCollection();  
   LineSegment m1 = new LineSegment();
   m1.Point = new Point(closepoint.X, beginpoint.Y);
   LineSegment m2 = new LineSegment();
   m2.Point = closepoint;
   LineSegment m3 = new LineSegment();
   m3.Point = new Point(beginpoint.X, closepoint.Y);
   pathSegmentCollection.Add(m1);
   pathSegmentCollection.Add(m2);
   pathSegmentCollection.Add(m3);
   figure.Segments = pathSegmentCollection;
   pathFigureCollection.Add(figure);
   result.Figures = pathFigureCollection;

   return result();
  }

  private Path locus;
  private bool ispath = false;
  private Point startpoint;
  private Point endpoint;
 }
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 太仓市| 临汾市| 北宁市| 洞头县| 涟水县| 田林县| 阿鲁科尔沁旗| 垣曲县| 沧州市| 孝昌县| 崇信县| 贵州省| 拉孜县| 高州市| 长沙市| 江安县| 鹿邑县| 简阳市| 仙居县| 新疆| 乌恰县| 城口县| 桓仁| 林西县| 万盛区| 卓资县| 句容市| 云安县| 根河市| 哈密市| 蕉岭县| 扎鲁特旗| 亳州市| 安平县| 麦盖提县| 连云港市| 福鼎市| 莱芜市| 义马市| 阳春市| 岳阳市|