除了ima的回答,我已經想通了使用不透明蒙版。我使用以下代碼掛鉤到圖像的LayoutUpdated事件中。
// Make a visual brush out of the masking control.
VisualBrush brush = new VisualBrush(maskingControl);
// Set desired opacity.
brush.Opacity = 1.0;
// Get the offset between the two controls.
Point offset = controlBeingMasked.TranslatePoint(new Point(0, 0), maskingControl);
// Determine the difference in scaling.
Point scale = new Point(maskingControl.ActualWidth/controlBeingMasked.ActualWidth,
maskingControl.ActualHeight/controlBeingMasked.ActualHeight);
TransformGroup group = new TransformGroup();
// Set the scale of the mask.
group.Children.Add(new ScaleTransform(scale.X, scale.Y, 0, 0));
// Translate the mask so that it always stays in place.
group.Children.Add(new TranslateTransform(-offset.X, -offset.Y));
// Rotate it by the reverse of the control, to keep it oriented correctly.
// (I am using a ScatterViewItem, which exposes an ActualOrientation property)
group.Children.Add(new RotateTransform(-controlBeingMasked.ActualOrientation, 0, 0));
brush.Transform = group;
controlBeingMasked.OpacityMask = brush;
如果您想要所需的基礎不透明度,請使用兩個圖像;一個始終處於基本不透明狀態,另一個使用位於其上的不透明蒙版。如果您希望基礎不透明度高於蒙版不透明度,那麼使用ima的方法可能會更容易。
與無掩模方法相比,此解決方案的一個優點是,如果屏蔽控制移動,更改大小等,它將自動接收更改,而不必保持另一個控件與其同步。
下面是它的外觀: http://img149.yfrog.com/img149/4183/99793865.jpg
我有這個想法爲使用型控制成型的方式和圖像或形狀與OpacityMask的偏移,當你移動的對象,但亂搞通常不會是一個好的解決方案,特別是如果你想用作掩碼的控件是動態的(在我的情況下不是)。 – nsantorello 2009-08-10 11:47:30
我忘記提及它通常不是一個好的解決方案的原因是因爲如果你想要某種形象的基礎不透明(我這樣做),你將不得不使這個圖像「僞造」控制較大(以適應較大的偏移或較大的可屏蔽圖像)。 – nsantorello 2009-08-10 11:50:31
你剛剛得到34代表,你是如何開始150代表賞金的? – Sadegh 2009-08-11 14:08:18