CompositionMaskBrush 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用套用不透明度遮罩的 CompositionBrush 繪製 SpriteVisual。 不透明度遮罩的來源可以是 CompositionColorBrush、CompositionLinearGradientBrush、CompositionSurfaceBrush、CompositionEffectBrush 或 CompositionNineGridBrush 的任何 CompositionBrush。 不透明度遮罩必須指定為 CompositionSurfaceBrush。
public ref class CompositionMaskBrush sealed : CompositionBrush
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 196608)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class CompositionMaskBrush final : CompositionBrush
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 196608)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CompositionMaskBrush : CompositionBrush
Public NotInheritable Class CompositionMaskBrush
Inherits CompositionBrush
- 繼承
- 屬性
Windows 需求
裝置系列 |
Windows 10 Anniversary Edition (已於 10.0.14393.0 引進)
|
API contract |
Windows.Foundation.UniversalApiContract (已於 v3.0 引進)
|
範例
將star形狀的遮罩套用至影像。
private SpriteVisual CreateCircleImage(ICompositionSurface myImageSurface, ICompositionSurface myStarMaskSurface)
{
// Create SurfaceBrush with image surface and specify stretch and alignment
CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface);
imageBrush.HorizontalAlignmentRatio = 0.5f;
imageBrush.VerticalAlignmentRatio = 0.5f;
imageBrush.Stretch = CompositionStretch.UniformToFill;
// Create surface brush with opacity mask surface and specify stretch and alignment
CompositionSurfaceBrush starMaskBrush = _compositor.CreateSurfaceBrush(myStarMaskSurface);
starMaskBrush.Stretch = CompositionStretch.Uniform;
// SpriteVisual to be painted with masked content
SpriteVisual imageVisual = _compositor.CreateSpriteVisual();
imageVisual.Size = new Vector2(200, 200);
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = imageBrush; // Set source to content that is to be masked
maskBrush.Mask = starMaskBrush; // Set mask to content that is the opacity mask
imageVisual.Brush = maskBrush; // Paint SpriteVisual with MaskBrush
return imageVisual;
}
將不透明度遮罩套用至純色填滿以建立圖案;以動畫顯示不透明度遮罩 CompositionSurfaceBrush 上的轉換屬性,以建立色彩開花動畫
private void CreateCircleMaskSprite(ICompositionSurface myCircleMaskSurface, ICompositionSurface myImageSurface)
{
// SpriteVisual to be painted by MaskBrush
_circleVisual = _compositor.CreateSpriteVisual();
_circleVisual.Size = MyContainerGrid.RenderSize.ToVector2();
// SurfaceBrush w/ image as MaskBrush.Source
CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface);
imageBrush.Stretch = CompositionStretch.UniformToFill;
// SurfaceBrush w/ opacity mask surface as MaskBrush.Mask
CompositionSurfaceBrush circleBrush = _compositor.CreateSurfaceBrush(myCircleMaskSurface);
// Specify stretch and transform properties to scale and position brush for initial state
circleBrush.Stretch = CompositionStretch.Uniform;
circleBrush.CenterPoint = _circleVisual.Size / 2;
circleBrush.Scale = new Vector2(0.5f);
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = imageBrush;
maskBrush.Mask = circleBrush;
_circleVisual.Brush = maskBrush;
// Set SpriteVisual as child of XAML Grid
ElementCompositionPreview.SetElementChildVisual(MyContainerGrid, _circleVisual);
}
private void BloomAnimation()
{
// Create KeyFrame Animation to target Scale transform on SurfaceBrush
Vector2KeyFrameAnimation scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
scaleAnimation.InsertKeyFrame(1.0f, new Vector2(2));
scaleAnimation.Duration = TimeSpan.FromMilliseconds(500);
var maskBrush = (CompositionMaskBrush)_circleVisual.Brush;
var surfaceBrush = (CompositionSurfaceBrush) maskBrush.Mask;
// Animate the Scale on the circle opacity mask
surfaceBrush.StartAnimation("Scale", scaleAnimation);
}
套用圓角矩形遮罩以建立純色圓角矩形,其圓角半徑在調整大小時不會增加 (使用 CompositionNineGridBrush 作為 CompositionMaskBrush ) 的輸入。
private SpriteVisual CreateMaskedRoundedRectVisual(ICompositionSurface myRoundedRectMaskSurface)
{
// ColorBrush to be set as MaskBrush.Source
CompositionColorBrush colorBrush = _compositor.CreateColorBrush(Colors.Blue);
// SurfaceBrush w/ opacity mask surface
CompositionSurfaceBrush roundedRectBrush = _compositor.CreateSurfaceBrush(myRoundedRectMaskSurface);
roundedRectBrush.Stretch = CompositionStretch.Fill; // stretch for center of nine-grid
// NineGridBrush w/ insets on opacity mask surface
CompositionNineGridBrush ninegridBrush = _compositor.CreateNineGridBrush();
ninegridBrush.Source = roundedRectBrush;
ninegridBrush.SetInsets(_cornerRadius); // the radius, in pixels, of the corner as specified on my opacity mask surface
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = colorBrush;
maskBrush.Mask = ninegridBrush;
// Paint SpriteVisual with MaskBrush
SpriteVisual sprite = _compositor.CreateSpriteVisual();
sprite.Size = new Vector2(300, 200);
sprite.Brush = maskBrush;
return sprite;
}
備註
CompositionMaskBrush 無法設定為 CompositionEffectBrush的來源參數。 如果您想要將 IGraphicsEffect 套用至遮罩的內容,請改用 CompositionEffectBrush 搭配 複合效果 。
屬性
Comment |
要與 CompositionObject 建立關聯的字串。 (繼承來源 CompositionObject) |
Compositor |
用來建立這個CompositionObject的Compositor。 (繼承來源 CompositionObject) |
Dispatcher |
CompositionObject的發送器。 (繼承來源 CompositionObject) |
DispatcherQueue |
取得 CompostionObject 的 DispatcherQueue 。 (繼承來源 CompositionObject) |
ImplicitAnimations |
附加至這個物件的隱含動畫集合。 (繼承來源 CompositionObject) |
Mask |
筆刷,其中包含要遮罩來源筆刷內容的不透明度遮罩。 可以是 CompositionSurfaceBrush 或 CompositionNineGridBrush 類型。 |
Properties |
與 CompositionObject相關聯的屬性集合。 (繼承來源 CompositionObject) |
Source |
筆刷,其內容要由不透明度遮罩遮罩。 可以是 CompositionSurfaceBrush、CompositionColorBrush 或 CompositionNineGridBrush 類型。 |