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. DHU-1241 Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. Bzoj 4196: [Noi2015]软件包管理器 树链剖分

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 721  Solved: 419[Submit][Statu ...

  3. 知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET

    知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET undefined 公司介绍 - 数人科技 undefined

  4. MySQL数据库建立外键失败的原因总结

    在MySQL数据库创建外键时,经常会发生一些错误,这是一件很令人头疼的事.一个典型的错误就是:Can’t create table... 的错误.在很多实例中,这种错误的发生都是因为mysql一直以来 ...

  5. pathmunge /etc/profile

    pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "a ...

  6. fzu 2135 数字游戏 【水题】

    Problem 2135 数字游戏 Accept: 253    Submit: 392Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem ...

  7. MKDOCS在线文档编辑器

    http://www.mkdocs.org/  api接口文档编写 ,效果非常不错

  8. 实践javascript美术馆的小案例,学习到的东西还是蛮多的,包括javascript编程中的预留退路、分离javascript、以及实现向后兼容等

    javascript美术馆(改进2) 一.javascript编程过程中的好习惯 1.实现预留退路 js被禁掉,图片也可以显示出来,href属性带有图片路径 <script src=" ...

  9. xcode6下使用autolayout+sizeclass实践

    历史车轮滚滚向前,将autolayout配合sizeclass做布局的方式推上了主流,虽然有点晚,但最终还是进行了一次完整的实践,特此记录一下: 因为网上已经有很多博客介绍了autolayout配合s ...

  10. JAVA向文件中追加内容(转)

    向文件尾加入内容有多种方法,常见的方法有两种: RandomAccessFile类可以实现随机访问文件的功能,可以以读写方式打开文件夹的输出流 public void seek(long pos)可以 ...