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. 使用Hexo搭建github博客步骤,超简便

    categories: 工具 tags: git Windows 搭建博客 你只需要node环境和一个github账号就可以开工啦! 本教程适合于Windows环境,Mac教程也大同小异 利用hexo ...

  2. glsl-BufferObject- change

    修改其值的最快方式: 创建: Mutable Storage To create mutable storage for a buffer object, you use this API: void ...

  3. poj 1039 Pipe(几何基础)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9932   Accepted: 3045 Description ...

  4. RHEL7磁盘分区挂载和格式化

    安装大数据平台,每台机器需要挂载10个磁盘,用JBOD模式,操作系统为RHEL7.2. 写了两个脚本,format_disk.sh和mount_disk.sh实现磁盘自动分区格式化以及挂载,修改fst ...

  5. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  6. N对括号的合法组合

    递归实现,需要注意以下几点: 1. 递归终止条件 2. 递归递推关系式 这里实际上是一个排列问题,只是排列需要满足条件在每一次递归调用时左括号数不能少于右括号数. 还有一点需要特别注意,当推出递归调用 ...

  7. Eclipse的安装以及与Tomcat的集成

    1.下载indgo版本的Eclipse:http://www.eclipse.org(64位:eclipse-jee-indigo-SR2-win32-x86_64.zip) 2.解压到D:\Prog ...

  8. PTA 06-图3 六度空间 (30分)

    "六度空间"理论又称作"六度分隔(Six Degrees of Separation)"理论.这个理论可以通俗地阐述为:"你和任何一个陌生人之间所间隔 ...

  9. Maven配置 settings.xml 转

    https://my.oschina.net/qjx1208/blog/201085 摘要: 记录settings.xml的配置,理解mirror.repository.profile的关系 本地仓库 ...

  10. Yii2 ActiveForm表单自定义样式

    实例: <?php $form = ActiveForm::begin([ 'fieldConfig' => [ 'template' => '<div class=" ...