WPF覆盖画布上的文本+缩放不能很好地工作(WPF override a text on canvas + zooming dont work well)

编程入门 行业动态 更新时间:2024-10-27 14:28:35
WPF覆盖画布上的文本+缩放不能很好地工作(WPF override a text on canvas + zooming dont work well)

问题1

如何在不删除整个画布内容的情况下覆盖书面文本? 我可以通过C#做到这一点吗? 没有在XAML代码中添加元素?

问题2

我集成了一个缩放功能,但只有在我指向画布上绘制的东西时它才会缩放。 当我只是指向我的Canvas时,我怎么才能缩放?

XAML

<Canvas x:Name="coordinateSystemBackground" VerticalAlignment="Top" Cursor="Cross" MouseWheel="coordinateSystemBackground_MouseWheel" RenderTransformOrigin="0.688,0.559" Width="1200" Height="720"> <ScrollViewer Grid.Column="0" Grid.Row="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Width="1200" Height="750"> <Canvas x:Name="coordinateSystem" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="Cross" UseLayoutRounding="False" Canvas.Left="0" Width="1200" Height="720"> <Canvas.RenderTransform> <ScaleTransform x:Name="st"/> </Canvas.RenderTransform> </Canvas> </ScrollViewer> </Canvas>

My Zoom function

private void coordinateSystemBackground_MouseWheel(object sender, MouseWheelEventArgs e) { // Skalierungsfaktor double ScaleRate = 1.1; //Transformiert gesamte Canvas... if (e.Delta > 0) { st.ScaleX *= ScaleRate; st.ScaleY *= ScaleRate; labZoomFaktor.Content = (int)((st.ScaleX * 100) - 100) + " %"; } else { st.ScaleX /= ScaleRate; st.ScaleY /= ScaleRate; labZoomFaktor.Content = (int)((st.ScaleX * 100) - 100) + " %"; } }

Problem 1

How can I override a written text without delete the whole canvas content? Can I do that over C#? Without adding elements in the XAML code?

Problem 2

I intergrated a zoom function but it zooms only if I'm pointing on something drawn in my canvas. How can I just zoom when I'm just pointing on my Canvas?

XAML

<Canvas x:Name="coordinateSystemBackground" VerticalAlignment="Top" Cursor="Cross" MouseWheel="coordinateSystemBackground_MouseWheel" RenderTransformOrigin="0.688,0.559" Width="1200" Height="720"> <ScrollViewer Grid.Column="0" Grid.Row="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Width="1200" Height="750"> <Canvas x:Name="coordinateSystem" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="Cross" UseLayoutRounding="False" Canvas.Left="0" Width="1200" Height="720"> <Canvas.RenderTransform> <ScaleTransform x:Name="st"/> </Canvas.RenderTransform> </Canvas> </ScrollViewer> </Canvas>

My Zoom function

private void coordinateSystemBackground_MouseWheel(object sender, MouseWheelEventArgs e) { // Skalierungsfaktor double ScaleRate = 1.1; //Transformiert gesamte Canvas... if (e.Delta > 0) { st.ScaleX *= ScaleRate; st.ScaleY *= ScaleRate; labZoomFaktor.Content = (int)((st.ScaleX * 100) - 100) + " %"; } else { st.ScaleX /= ScaleRate; st.ScaleY /= ScaleRate; labZoomFaktor.Content = (int)((st.ScaleX * 100) - 100) + " %"; } }

最满意答案

为了使命中测试获取鼠标输入,需要在光标下有一些非空的目标元素部分。 在这种情况下,画布的子项正在拾取事件并将它们冒泡到Canvas但Canvas本身只是空白空间,因此不会拾取鼠标输入。 通过在Canvas上设置Background="Transparent" ,您将使整个事物可见以进行测试。

In order for hit testing to pick up mouse input there needs to be some non-null part of the targeted element under the cursor. In this case the canvas's children are picking up events and bubbling them to the Canvas but the Canvas itself is just empty space and so won't pick up the mouse input. By setting Background="Transparent" on the Canvas you will make the entire thing visible to hit testing.

更多推荐

本文发布于:2023-08-01 14:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1360377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很好   画布   缩放   文本   工作

发布评论

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

>www.elefans.com

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