WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小

xaml代码:

 1 <Canvas Name="movBg">
 2             <Canvas.Background>
 3                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
 4                     <GradientStop Color="White" Offset="0" />
 5                     <GradientStop Color="#FFF5FF00" Offset="1" />
 6                 </LinearGradientBrush>
 7             </Canvas.Background>
 8             <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">
 9                 <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>
10                 <Canvas.Background>
11                     <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
12                         <GradientStop Color="White" Offset="0" />
13                         <GradientStop Color="#FFD679F2" Offset="1" />
14                     </LinearGradientBrush>
15                 </Canvas.Background>
16             </Canvas>
17 </Canvas>  

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             return;
103         }
104         #endregion  
时间: 2024-10-12 20:01:06

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

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

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

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

原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示 通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windo

Python3 Tkinter基础 Scrollbar pack(side,fill) 在一个窗体中创建一个靠右的 充满Y轴的垂直滚动条

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: from tkinter import * root=Tk() scroll=Scrollbar(root) scroll.pack(side=RIGHT,fill=Y) mainloop() result: --------------------------

WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension

问题描述: 在Resources.resx定义了一个静态字符串字段Title,并在WPF窗体视图中绑定为窗体的标题: Title="{x:Static local:Resources.Title}" 但是,在运行应用时,抛出System.Windows.Markup.StaticExtension异常. 原因: 解决方案: 将Resources.resx的访问修饰符由internal修改为public.

vb.net: 一个用户控件,把它放在窗体中,当拖动它时,窗体也随之移动,这样这个窗体就不需要标题栏来拖动了

首先新建一个用户控件,这个控件中只有一个 label ,其名为: lblDrag,至于其所显示的文本和大小,可以随意设置. 接下来打开其代码界面,输入以下代码: -------------------------------------------------------------------------------------------------' 用户控件,当拖动这个控件时,该控件所属的窗体会被拖动Public Class uc_dagrLabelToMove Private isDr

wpf,后台触发按钮点击以及拖动

触发按钮Click MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left); args.RoutedEvent = Button.ClickEvent; btnOkCommand.RaiseEvent(args); 触发按钮绑定的Command 需要添加UIAutomationProvider 引用 ButtonAutomationPeer bam = new B

WPF布局管理之Canvas、InkCanvas (转)

一.Canvas 在WPF中子元素的绝对定位的布局控件 其子元素使用Width.Height定义元素的宽度和高度 使用Convas.Left(Convas.Right).Convas.Top(Convas.Bottom)定义与Convas容器的相对位置 如果同时存在Convas.Left和Convas.Right.Convas.Top和Convas.Bottom,则Convas.Left.Convas.Top优先生效 例如: <Canvas> <Button Canvas.Left=&q

WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图: XAML代码:// Transform.XAML <Canvas Width="700" Height="700" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://sc

WPF 窗体拖转时不触发MouseLeftButtonUpEvent

解决方案:手动添加Handler,因为e.Handled这个属性是用在路由事件中的,当某个控件得到一个RoutedEvent,就会检测Handled是否为true,为true则忽略该事件. //手动注册 this.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(UIElement_OnMouseLeftButtonUp), true); WPF 窗体拖转时不触发MouseLeftButtonUpEvent