使用Canvas屏蔽C#/ WPF中的另一個畫布

[英]Use Canvas to mask another canvas in C#/WPF


I am learning some WPF/C# so this question might be silly for some of you but I can't find the solution.

我正在學習一些WPF / C#所以這個問題可能對你們有些愚蠢但我找不到解決方案。

I have a Canvas (let's say it countains texts) and then I have another Canvas that I want to use as a mask (anything in this second Canvas will mask the first one).

我有一個Canvas(讓我們說它計算文本)然后我有另一個Canvas我想用作一個掩碼(第二個Canvas中的任何東西都會掩蓋第一個)。

How can I do that? What if the second Canvas is moving? I want to change the scale of the second canvas (mask) so it "unveils" the content of the first one (content).

我怎樣才能做到這一點?如果第二個Canvas正在移動怎么辦?我想改變第二個畫布(蒙版)的比例,以便“揭開”第一個畫布(內容)的內容。

Thanks in advance!

提前致謝!

EDIT: If instead of a canvas I could use something like a rectangle (as soon as it's resizable) that's fine too!

編輯:如果不是畫布我可以使用像矩形(只要它可調整大小)的東西,這也很好!

EDIT 2: Here's the code I'm using:

編輯2:這是我正在使用的代碼:

// Order info
Canvas order_info = new Canvas();
order_info.Width = 220;
order_info.Height = 250;
order_info.Background = Brushes.Yellow;
user_info.Children.Add(order_info);
// Order info mask
Canvas order_info_mask = new Canvas();
order_info_mask.Width = 110;
order_info_mask.Height = 250;
order_info_mask.Background = Brushes.Pink;
user_info.Children.Add(order_info_mask);
// Apply mask
VisualBrush mask_brush = new VisualBrush();
mask_brush.Visual = order_info_mask;
order_info.OpacityMask = mask_brush;

2 个解决方案

#1


You probabbly want to use the OpacityMask property on your Canvas instead, which is of type Brush, which could be animated.

你可能想要在你的Canvas上使用OpacityMask屬性,它是Brush類型,它可以是動畫的。

Edit in response to question: You can do something like:

編輯以回答問題:您可以執行以下操作:

VisualBrush b = new VisualBrush();
b.Visual = canvas2;
canvas2.OpacityMask = b;

You can also set this in xaml with databinding:

您也可以使用數據綁定在xaml中進行設置:

<Canvas>
    <Canvas.OpacityMask>
        <VisualBrush Visual="{Binding ElementName=canvas2}" />
    </Canvas.OpacityMask>
</Canvas>

...

<Canvas x:Name="canvas2" ... /> 

However, to me, this seems like the incorrect approach, what visual transition are you are exactly?

但是,對我來說,這似乎是不正確的方法,你到底是什么視覺轉換?

#2


Mind, I am no WPF expert, but you should start with embedding two canvasses in a 1x1 grid:

記住,我不是WPF專家,但你應該首先在1x1網格中嵌入兩個畫布:

<Grid>
   <Canvas x:Name="background">
   </Canvas>
   <Canvas x:Name="foreground">
   </Canvas>
</Grid>

In this way the content of the canvases will be drawn on top of each other. Inside the foreground Canvas you can draw your rectangles etc. Hope this helps to give you a start!

通過這種方式,畫布的內容將彼此疊加。在前景畫布內,您可以繪制矩形等。希望這有助於您開始!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/20/9469d89c0f76fc1aaebdca065ac82cc2.html



 
  © 2014-2022 ITdaan.com 联系我们: