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. Redis主从实现原理分析 [转]

    原文地址:http://blog.sina.com.cn/s/blog_7530db6f0100vegl.html 一, 实现原理图 (1)Slave服务器连接到Master服务器. (2)Slave ...

  2. HTTP Authorization

    谨以此文献给那些需要实现HTTP AUTH的“程序猿”们. 关于HTTP AUTH的文档不多. RFC在 http://www.ietf.org/rfc/rfc2617.txt wiki在 http: ...

  3. Android线程计时器实现

    cocos2dx的计时器很好用,但当app进入后台,其计时器会pause掉,如果想要一个稳恒计时器就得自己去实现完成了,在Cocos2d-x for ios中我们可以利用NSTimer类并结合objc ...

  4. 让你的Git水平更上一层楼的10个小贴士

    注意:本文中,一些命令包含含有方括号的部分(e.g.git add -p [file_name]).在这些例子中,您要在该处插入所需的数字,标示符等.而不需要保留方括号. 1.Git自动补全 如果你在 ...

  5. 基于TCP协议的客户端

    基于TCP协议的客户端 此客户端能用于下一篇博客的单线程服务器和多线程服务器 import java.io.BufferedReader; import java.io.IOException; im ...

  6. MySQL 优化Limit分页

    很多时候.我们需要选择出从指定位置开始的指定行数.此时.limit笑了     对于limit的定义是:     limit x,y     表示从第x行开始选择y条记录          在业务需要 ...

  7. 第一次尝试使用JAVA编写的ATM机程序

    package study; import java.util.Scanner; public class ATM { private static int[] users = { 111111, 2 ...

  8. sso笔记

    C:\Windows\System32\drivers\etc\hosts SSO:单点登录 1.使用Cookie解决单点登录 技术点: 1.设置Cookie的路径为setPath("/&q ...

  9. Javascript从入门到精通(一)

    第一篇   基础知识 一.JavaScript的主要特点:1.解释性:不同于一些编译性的程序语言(如C.C++等),它是一种解释性的程序语言,它的源代码不需要经过编译,而是直接在浏览器中运行时被解释. ...

  10. easyui常用控件及样式收藏

    CSS类定义: div easyui-window                               window窗口样式 属性如下: 1)       modal:是否生成模态窗口.tru ...