创建一个允许缩放和平移的WPF窗口(Creating a WPF Window that allows zooming and panning)

编程入门 行业动态 更新时间:2024-10-13 06:18:44
创建一个允许缩放和平移的WPF窗口(Creating a WPF Window that allows zooming and panning)

我想创建一个可以容纳多个控件的窗口。 但是,我希望用户能够平移和缩放以查看这些控件的更大版本。

我甚至不知道从哪里开始寻找。

我打算从ScaleTransform开始,它对使用鼠标上的滚动按钮做出响应,但我不确定这是否是最好的主意。

只需要一个正确的方向。

谢谢!

I want to create a Window that will hold several controls. However, I would like the user to be able to pan around and zoom in and out to see larger versions of those controls.

I don't even know where to begin looking.

I was going to start at ScaleTransform that responds to the use of the scroll button on the mouse but I am not sure if that is the best idea.

Just need a push in the right direction.

thanks!

最满意答案

这可能是Viewbox的理想选择。

请参阅: http : //msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.110).aspx

基本上,您可以将窗口的全部内容Viewbox到Viewbox如下所示:

<Window> <Viewbox> <!-- content here --> </Viewbox> </Window>

然后绑定到Viewbox控件的宽度和高度以模拟缩放。 对于快速测试,您可以通过代码隐藏来监听滚轮事件,命名Viewbox控件,并直接访问Viewbox以更改其中的值。

编辑 :这是我刚刚发现的一个场景,让你开始。 他们正在使用图像,但它与我上面描述的概念完全相同。

http://www.c-sharpcorner.com/uploadfile/yougerthen/working-with-wpf-viewbox-control/

编辑2:使用鼠标滚动的快速工作示例

XAML:

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" MouseWheel="MainWindow_OnMouseWheel"> <Grid> <Viewbox x:Name="ZoomViewbox" Stretch="Fill"> <StackPanel> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/> <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" /> </StackPanel> </Viewbox> </Grid> </Window>

C#:

using System.Windows; using System.Windows.Input; namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ZoomViewbox.Width = 100; ZoomViewbox.Height = 100; } private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e) { UpdateViewBox((e.Delta > 0) ? 5 : -5); } private void UpdateViewBox(int newValue) { if ((ZoomViewbox.Width >= 0) && ZoomViewbox.Height >= 0) { ZoomViewbox.Width += newValue; ZoomViewbox.Height += newValue; } } } }

This might be a good candidate for a Viewbox.

See here: http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.110).aspx

Basically, you can Wrap the entire contents of the Window into a Viewbox like so:

<Window> <Viewbox> <!-- content here --> </Viewbox> </Window>

and then bind to the Viewbox control's width and height to simulate the zooming. For a quick test, you could just listen to scroll wheel events via code-behind, name the Viewbox control, and access the Viewbox directly at change the values there.

Edit: here's a scenario I just found to get you started. They are using an image, but it's the exact same concept that I described above.

http://www.c-sharpcorner.com/uploadfile/yougerthen/working-with-wpf-viewbox-control/

Edit2: Quick working example using mouse-scroll

Xaml:

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" MouseWheel="MainWindow_OnMouseWheel"> <Grid> <Viewbox x:Name="ZoomViewbox" Stretch="Fill"> <StackPanel> <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/> <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" /> </StackPanel> </Viewbox> </Grid> </Window>

C#:

using System.Windows; using System.Windows.Input; namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ZoomViewbox.Width = 100; ZoomViewbox.Height = 100; } private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e) { UpdateViewBox((e.Delta > 0) ? 5 : -5); } private void UpdateViewBox(int newValue) { if ((ZoomViewbox.Width >= 0) && ZoomViewbox.Height >= 0) { ZoomViewbox.Width += newValue; ZoomViewbox.Height += newValue; } } } }

更多推荐

本文发布于:2023-07-27 20:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1295019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   创建一个   窗口   zooming   panning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!