xaml代码:

 <Canvas Name="movBg">
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="#FFF5FF00" Offset="1" />
</LinearGradientBrush>
</Canvas.Background>
<Canvas Canvas.Top="50" Canvas.Left="50" Height="100" HorizontalAlignment="Left" Name="mov" VerticalAlignment="Top" Width="200" MouseLeftButtonDown="mov_MouseLeftButtonDown" MouseLeftButtonUp="mov_MouseLeftButtonUp" MouseMove="mov_MouseMove" MouseWheel="mov_MouseWheel" MaxWidth="300" MaxHeight="300">
<Border BorderThickness="1" BorderBrush="Black" Width="{Binding ElementName=mov,Path=Width}" Height="{Binding ElementName=mov,Path=Height}" MaxWidth="{Binding ElementName=mov,Path=MaxWidth}" MaxHeight="{Binding ElementName=mov,Path=MaxHeight}"></Border>
<Canvas.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="#FFD679F2" Offset="1" />
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
</Canvas>

C#代码:

 #region 拖动选区,滚轴改变大小
//鼠标相对于被拖动的Canvas控件mov的坐标
Point childPoint = new Point();
//鼠标相对于作为容器的Canvas控件movBg的坐标
Point prevPoint = new Point(); private void mov_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
childPoint = e.GetPosition(mov);
} private void mov_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Canvas c = sender as Canvas;
Rect rc = new Rect(, , movBg.ActualWidth, movBg.ActualHeight);
Rect childRc = new Rect(Canvas.GetLeft(c), Canvas.GetTop(c), c.ActualWidth, c.ActualHeight);
if (!rc.Contains(childRc))
{
childRc = AutoResize(rc, childRc);
c.SetValue(Canvas.LeftProperty, childRc.Left);
c.SetValue(Canvas.TopProperty, childRc.Top);
c.Width = childRc.Width;
c.Height = childRc.Height;
}
c.ReleaseMouseCapture();
} private void mov_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Canvas c = sender as Canvas; prevPoint = e.GetPosition(movBg);
double x = prevPoint.X - childPoint.X;
double y = prevPoint.Y - childPoint.Y; Rect rc = new Rect(, , movBg.ActualWidth, movBg.ActualHeight);
Rect childRc = new Rect(Canvas.GetLeft(c), Canvas.GetTop(c), c.ActualWidth, c.ActualHeight);
if (!rc.Contains(childRc))
{
childRc = AutoResize(rc, childRc);
c.SetValue(Canvas.LeftProperty, childRc.Left);
c.SetValue(Canvas.TopProperty, childRc.Top);
c.Width = childRc.Width;
c.Height = childRc.Height;
c.ReleaseMouseCapture();
}
else
{
c.SetValue(Canvas.LeftProperty, prevPoint.X - childPoint.X);
c.SetValue(Canvas.TopProperty, prevPoint.Y - childPoint.Y);
c.CaptureMouse();
}
}
} private Rect AutoResize(Rect outerRc, Rect innerRc)
{
double with = innerRc.Width;
double height = innerRc.Height; if (innerRc.Left < outerRc.Left)
{
innerRc.X = outerRc.Left + ;
innerRc.Width = with;
}
if (innerRc.Right > outerRc.Right)
{
innerRc.X = outerRc.Right - with - ;
innerRc.Width = with;
}
if (innerRc.Top < outerRc.Top)
{
innerRc.Y = outerRc.Top + ;
innerRc.Height = height;
}
if (innerRc.Bottom > outerRc.Bottom)
{
innerRc.Y = outerRc.Bottom - height - ;
innerRc.Height = height;
}
return innerRc;
} private void mov_MouseWheel(object sender, MouseWheelEventArgs e)
{
double val = e.Delta * 0.001;
double wl = mov.Width * (val / 0.12) * 0.02;
double hl = mov.Height * (val / 0.12) * 0.02; if ((Canvas.GetLeft(mov) - wl/2.0) > && (Canvas.GetLeft(mov) + wl + mov.Width) <= movBg.ActualWidth &&
(Canvas.GetTop(mov) - hl/2.0) > && (Canvas.GetTop(mov) + hl + mov.Height) <= movBg.ActualHeight &&
(mov.Width + wl) < mov.MaxWidth && (mov.Height + hl) < mov.MaxHeight)
{
mov.SetValue(Canvas.LeftProperty, Canvas.GetLeft(mov) - wl / 2.0);
mov.SetValue(Canvas.TopProperty, Canvas.GetTop(mov) - hl / 2.0);
mov.Width += wl;
mov.Height += hl; }
return;
}
#endregion

WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小的更多相关文章

  1. 在WPF窗体中重绘

    原文:在WPF窗体中重绘   写这篇主要是为了验证任何元素自身都具备绘图功能. 在默认Window中重写OnRender方法 protected override void OnRender(Draw ...

  2. WPF 窗体中获取键盘和鼠标无操作时的超时提示

    原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示 通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间 , , );            timer.Tic ...

  3. wpf窗体中复合控件焦点控制

    1.         自定义控件 在UserControl标记中 <UserControl KeyboardNavigation.ControlTabNavigation="Local ...

  4. wpf 窗体中显示当前系统时间

    先看一下效果: 这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间. 接下来展示一下代码: 在XAML中: <StackPanel Width="205" ...

  5. WPF窗体中嵌入/使用WinForm类/控件(基于.NET Core)

    如题,WPF中嵌入WinForm的做法,网络上已经很多示例,都是基于.NET XXX版的. 今天King様在尝试WPF(基于.NET Core 3.1)中加入Windows.Forms.ColorDi ...

  6. 在WPF中的Canvas上实现控件的拖动、缩放

    如题,项目中需要实现使用鼠标拖动.缩放一个矩形框,WPF中没有现成的,那就自己造一个轮子:) 造轮子前先看看Windows自带的画图工具中是怎样做的,如下图: 在被拖动的矩形框四周有9个小框,可以从不 ...

  7. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  8. WPF实现窗体中的悬浮按钮

    WPF实现窗体中的悬浮按钮,按钮可拖动,吸附停靠在窗体边缘. 控件XAML代码: <Button x:Class="SunCreate.Common.Controls.FloatBut ...

  9. 在Winform窗体中使用WPF控件(附源码)

    原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...

随机推荐

  1. PCB的技巧

    (1)首先元件的移动,如下图中,向让D1的2引脚和R49的2引脚齐平,但是移动的距离每次都超过,不能平齐 修改元件最小移动距离即可,如下图,其中有很多可以改动的地方,但是需要改的是Component ...

  2. poj 2932 Coneology(扫描线+set)

    Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3574   Accepted: 680 Descript ...

  3. GRUB损坏后,如何修复windows启动mbr

    今天使用Ghost装系统遇到windows7不能启动的问题,采用下面帖子中的部分命令搞定之. 我自己是直接使用: 插入windows7安装光盘,从光盘启动,在光盘启动完成后,按下shift+f10键, ...

  4. 天津Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. Intent是什么?

    一.理解Intent: 在一个Android应用中,主要是由四种组件组成的,这四种组件可参考“Android应用的构成”.而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的And ...

  6. Yii 将对象转化成数组

    将从数据库查找的对象,转换成数组,并且以设定属性键名称,用到ArrayHelper::toArray $posts = Post::find()->limit(10)->all(); $d ...

  7. Java如何等待子线程执行结束

    工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...

  8. 一次mysql瘫痪解救

    最近手机app项目访问流量逐步的增加,对服务端webapi考验极大,是在一次新的业务消息推送后,极光推送给手机接受到的客户端达到19万个,此时app立马开始访问速度变慢了,用户体验相当差 客服接到的问 ...

  9. servlet中web.xml配置

    常见的Servlet中url-pattren的配置 1.固定配置, 如:/hi 引入通配符 * 2.以"/XXX"开头,以"*"结尾 3.以"*&qu ...

  10. Sublime_text3怎么发现PHP语法错误?

    昨晚因为php的某个变量代码写错了,sublime又没有提示语法错误.弄了许久,一段段的调试,最后才知道是取到的变量是空的 sublime可以提示php语法错误 在sublime写完了php代码后,如 ...