C#开发学习笔记:WPF中鼠标滚轮滚动与Slider控件的值改变关联

时间:2024-03-15 21:05:55

1.创建WPF控件并布局同时与WinForm关联

(1).创建WPF用户控件并布局

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DemoCollection" x:Class="DemoCollection.WPFUserControl2" 
             mc:Ignorable="d" 
             d:DesignHeight="371" d:DesignWidth="706">
    <Viewbox x:Name="Viewbox" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Stretch="Fill">
        <StackPanel x:Name="StackPanel1" >
            <Slider x:Name="Slider1" Width="113" Panel.ZIndex="11" ValueChanged="Slider1_ValueChanged" Margin="700,0,0,0"/>
            <ScrollViewer x:Name="ScrollViewer1" Height="464" Width="907" Margin="0,0,0,0" HorizontalScrollBarVisibility="Visible" VerticalAlignment="Top" VerticalContentAlignment="Center" ScrollChanged="ScrollViewer1_ScrollChanged">
                <Canvas x:Name="Canvas1" Height="464" Width="907" MouseWheel="Canvas1_MouseWheel"/>
            </ScrollViewer>
        </StackPanel>
    </Viewbox>
</UserControl>

(2).解决方案生成之后,将创建的WPF用户控件拖拽到WinForm窗体中

2.WPF用户控件加载时给画布绑定背景图片并使用System.Windows.Shapes.Path创建几个几何图形

public WPFUserControl2()
{
	InitializeComponent();
	w = this;
	//绑定Image控件要显示的图片
	string path = this.GetType().Assembly.Location;
	FileInfo directory = new FileInfo(path);
	//根据绝对路径获取图片
	string sss = @"G:\自己做的小东西\DemoCollection\DemoCollection\Resources\background2.jpg";            
	BitmapImage image = new BitmapImage(new Uri(sss, UriKind.Absolute));
   
	//Canvas1.Width += 800;
	//Canvas1.Height += 1000;
	//Image1_x = Canvas1.Width;
	//Image1_y = Canvas1.Height;
	//用于存放画布的原始大小
	Original_height = Canvas1.Height;
	Original_width = Canvas1.Width;

	//使用图片绘制画布的背景
	ImageBrush imageBrush = new ImageBrush(image);
	w.Canvas1.Background = imageBrush;

	Ellipse e = new Ellipse();//创建一个椭圆形对象
	//设置椭圆的上、下、左、右的边距
	Thickness thickness = new Thickness(Convert.ToDouble(200), Convert.ToDouble(100), 0, 0);
	e.Height = 70;
	e.Width = 70;
	e.Margin = thickness;
	Canvas1.Children.Add(e);

	//设置椭圆内部绘制的样式
	Brush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Red"));
	e.Fill = brush;
	//设置椭圆在其父控件中的显示顺序
	Panel.SetZIndex(e, 10);

	//Path:绘制一系列的直线和曲线;要与System.IO.Path区分开
	System.Windows.Shapes.Path p = new System.Windows.Shapes.Path();

	//Geometry:定义几何形状,可用于对二维图形数据进行裁剪、命中测试、呈现
	//EllipseGeometry:初始化一个具有指定中心点位置,X轴半径,Y轴半径的椭圆
	Geometry g = new EllipseGeometry(new Point(Convert.ToDouble(350), Convert.ToDouble(350)), 10, 10);

	//将要绘制的图像赋值给Path对象
	p.Data = g;


	//指定内部绘制方式
	Brush brush1 = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Black"));
	p.Fill = brush1;
	p.Name = "第1个点";
	Canvas1.Children.Add(p);

	System.Windows.Shapes.Path p1 = new System.Windows.Shapes.Path();
	Geometry g1 = new EllipseGeometry(new Point(Convert.ToDouble(400), Convert.ToDouble(350)), 20, 20);

	p1.Data = g1;
	Brush brush2 = new SolidColorBrush((Color)ColorConverter.ConvertFromString("White"));
	p1.Fill = brush2;
	p1.Name = "第2个点";
	Canvas1.Children.Add(p1);

	System.Windows.Shapes.Path p2 = new System.Windows.Shapes.Path();
	Geometry g2 = new EllipseGeometry(new Point(Convert.ToDouble(500), Convert.ToDouble(350)), 30, 30);

	p2.Data = g2;
	Brush brush3 = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Blue"));
	p2.Fill = brush3;
	p1.Name = "第3个点";
	Canvas1.Children.Add(p2);
   
}

 

3.添加Slider控件的值改变事件

private void Slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
	if (e.NewValue != 0)
	{
		double a = Math.Round(e.NewValue);
		if (a != 0&&a>=1)
		{
			//ScaleTransform:在二维坐标系x-y中缩放对象
			//参数分别为:x轴的缩放比例,y轴的缩放比例,此ScaleTransform对象缩放中心坐标的x值,此ScaleTransform对象缩放中心坐标的y值
			//如果后两个参数不设置,则对象以左上角的(0,0)为缩放中心
			ScaleTransform sf = new ScaleTransform(a, a, Canvas1.Width / 2, Canvas1.Height / 2);

			Image1_x = Canvas1.Width;
			Image1_y = Canvas1.Height;
			//使用LayoutTransform时,当Canvas1随着Slider放大缩小时,外层的滚动条可以跟随改变滚动条的高宽
			Canvas1.LayoutTransform = sf;
			//使用RenderTransform时,当Canvas1随着Slider放大缩小时,外层的滚动条无法改变实际显示内容的高宽
			//Canvas1.RenderTransform = sf;
		}

	}
	else
	{
		Canvas1.Width = Original_width;
		Canvas1.Height = Original_height;

	}
}

 

3.给Canvas绑定鼠标滚轮事件,判断如果当前键盘按下左Shif键的同时滚动滚轮,则改变Slider的值

private void Canvas1_MouseWheel(object sender, MouseWheelEventArgs e)
{
	//Delta:代表鼠标滚轮滚动的方向,如果>0代表向前滚动,如果<0代表向后滚动
	int a = e.Delta;
	if (Keyboard.GetKeyStates(Key.LeftShift) == KeyStates.Down)
	{
		if (e.Delta > 0)
			Slider1.Value += 1;
		else
			Slider1.Value -= 1;
	}
}

 

运行效果:

C#开发学习笔记:WPF中鼠标滚轮滚动与Slider控件的值改变关联