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(1, 1, 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(1, 1, 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 + 1;
- innerRc.Width = with;
- }
- if (innerRc.Right > outerRc.Right)
- {
- innerRc.X = outerRc.Right - with - 1;
- innerRc.Width = with;
- }
- if (innerRc.Top < outerRc.Top)
- {
- innerRc.Y = outerRc.Top + 1;
- innerRc.Height = height;
- }
- if (innerRc.Bottom > outerRc.Bottom)
- {
- innerRc.Y = outerRc.Bottom - height - 1;
- 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) > 0 && (Canvas.GetLeft(mov) + wl + mov.Width) <= movBg.ActualWidth &&
- (Canvas.GetTop(mov) - hl/2.0) > 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