博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小
阅读量:4585 次
发布时间:2019-06-09

本文共 3300 字,大约阅读时间需要 11 分钟。

xaml代码:

1 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

 

C#代码:

1 #region 拖动选区,滚轴改变大小    2         //鼠标相对于被拖动的Canvas控件mov的坐标  3 Point childPoint = new Point(); 4 //鼠标相对于作为容器的Canvas控件movBg的坐标 5 Point prevPoint = new Point(); 6 7 private void mov_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 8 { 9 childPoint = e.GetPosition(mov); 10 } 11 12 private void mov_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 13 { 14 Canvas c = sender as Canvas; 15 Rect rc = new Rect(1, 1, movBg.ActualWidth, movBg.ActualHeight); 16 Rect childRc = new Rect(Canvas.GetLeft(c), Canvas.GetTop(c), c.ActualWidth, c.ActualHeight); 17 if (!rc.Contains(childRc)) 18 { 19 childRc = AutoResize(rc, childRc); 20 c.SetValue(Canvas.LeftProperty, childRc.Left); 21 c.SetValue(Canvas.TopProperty, childRc.Top); 22 c.Width = childRc.Width; 23 c.Height = childRc.Height; 24 } 25 c.ReleaseMouseCapture(); 26 } 27 28 private void mov_MouseMove(object sender, MouseEventArgs e) 29 { 30 if (e.LeftButton == MouseButtonState.Pressed) 31 { 32 Canvas c = sender as Canvas; 33 34 prevPoint = e.GetPosition(movBg); 35 double x = prevPoint.X - childPoint.X; 36 double y = prevPoint.Y - childPoint.Y; 37 38 Rect rc = new Rect(1, 1, movBg.ActualWidth, movBg.ActualHeight); 39 Rect childRc = new Rect(Canvas.GetLeft(c), Canvas.GetTop(c), c.ActualWidth, c.ActualHeight); 40 if (!rc.Contains(childRc)) 41 { 42 childRc = AutoResize(rc, childRc); 43 c.SetValue(Canvas.LeftProperty, childRc.Left); 44 c.SetValue(Canvas.TopProperty, childRc.Top); 45 c.Width = childRc.Width; 46 c.Height = childRc.Height; 47 c.ReleaseMouseCapture(); 48 } 49 else 50 { 51 c.SetValue(Canvas.LeftProperty, prevPoint.X - childPoint.X); 52 c.SetValue(Canvas.TopProperty, prevPoint.Y - childPoint.Y); 53 c.CaptureMouse(); 54 } 55 } 56 } 57 58 private Rect AutoResize(Rect outerRc, Rect innerRc) 59 { 60 double with = innerRc.Width; 61 double height = innerRc.Height; 62 63 if (innerRc.Left < outerRc.Left) 64 { 65 innerRc.X = outerRc.Left + 1; 66 innerRc.Width = with; 67 } 68 if (innerRc.Right > outerRc.Right) 69 { 70 innerRc.X = outerRc.Right - with - 1; 71 innerRc.Width = with; 72 } 73 if (innerRc.Top < outerRc.Top) 74 { 75 innerRc.Y = outerRc.Top + 1; 76 innerRc.Height = height; 77 } 78 if (innerRc.Bottom > outerRc.Bottom) 79 { 80 innerRc.Y = outerRc.Bottom - height - 1; 81 innerRc.Height = height; 82 } 83 return innerRc; 84 } 85 86 private void mov_MouseWheel(object sender, MouseWheelEventArgs e) 87 { 88 double val = e.Delta * 0.001; 89 double wl = mov.Width * (val / 0.12) * 0.02; 90 double hl = mov.Height * (val / 0.12) * 0.02; 91 92 if ((Canvas.GetLeft(mov) - wl/2.0) > 0 && (Canvas.GetLeft(mov) + wl + mov.Width) <= movBg.ActualWidth && 93 (Canvas.GetTop(mov) - hl/2.0) > 0 && (Canvas.GetTop(mov) + hl + mov.Height) <= movBg.ActualHeight && 94 (mov.Width + wl) < mov.MaxWidth && (mov.Height + hl) < mov.MaxHeight) 95 { 96 mov.SetValue(Canvas.LeftProperty, Canvas.GetLeft(mov) - wl / 2.0); 97 mov.SetValue(Canvas.TopProperty, Canvas.GetTop(mov) - hl / 2.0); 98 mov.Width += wl; 99 mov.Height += hl; 100 101 } 102

转载于:https://www.cnblogs.com/chen110xi/p/6757584.html

你可能感兴趣的文章
南阳97
查看>>
【LibreOJ 6278】 数列分块入门 2 (分块)
查看>>
利用Jmeter模拟Github登录
查看>>
解决mybatis空字段null字段不返回
查看>>
生命的意义
查看>>
Box-Muller 与 ziggurat
查看>>
zookeeper+dubbo问题
查看>>
dubbo zookeeper图解入门配置
查看>>
winform如何不卡界面
查看>>
Random随机库
查看>>
AFNetworking 3.0x版本最新特性
查看>>
PHP排序算法实现 与sort性能对比
查看>>
manage partitions
查看>>
Java快速入门
查看>>
C++中函数重载
查看>>
BLE广播数据的抓包解析
查看>>
基于 Android NDK 的学习之旅-----HelloWorld
查看>>
JAVA CAS原理深度分析
查看>>
initWithFrame方法的理解
查看>>
cocos2d-x的lua脚本加载CocostudioUI两种方式
查看>>