在 WPF 中使用另一个控件作为不透明蒙版?
-
22-07-2019 - |
题
我尝试将 OpacityMask 属性与 VisualBrush 结合使用,以便当您将图像拖动到另一个控件(例如另一个图像、矩形或任何其他控件)上时,第二个控件上的图像部分具有不同的不透明度。也就是说,图像具有一定的非零基础不透明度,并且图像中位于另一个控件之上的任何部分都具有不同的(同样是非零)不透明度。
是否可以仅使用 VisualBrush 和 OpacityMask 实现这一点?或者是否需要更复杂的方法?
谢谢!
编辑:我试图使图像具有较低的不透明度(例如 0.5),并且在控件上拖动的部分具有较高的不透明度(例如 1.0)。我最初省略了这个细节,这对于所采取的方法很重要。
解决方案
除了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的做法。
此溶液,而不是无掩模方法的一个优点是,如果掩蔽控制移动时,改变大小,等等,这将自动拿起变化,而不必保持同步的另一控制与它
下面是它的外观: 结果 <子>(来源: yfrog.com )子>
其他提示
- 没有口罩
- 为控件定义视觉画笔
- 使用该画笔在控件顶部绘制形状
- 拖动图像 之间 形状和控制
- 设置画笔的不透明度以获得所需的效果
不隶属于 StackOverflow