Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » WPF » Enhance the look of Windows Controls using Opacity Masks in Avalon

Enhance the look of Windows Controls using Opacity Masks in Avalon

An opacity mask is like a layer having different degrees of transparency, which can be applied to any Windows control object or even panels. Depending on the opacity/transparency settings of the mask, the background of the control will also acquire the same properties.

Page Views : 25335
Downloads : 161
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
OpacityMask_sample.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction

It is possible to add a fancy touch to the appearance of Windows Controls using a simple feature called Opacity Mask. An opacity mask is like a layer having different degrees of transparency, which can be applied to any Windows control object or even panels. Depending on the opacity/transparency settings of the mask, the background of the control will also acquire the same properties. So the mask acts like a template to specify the opacity/transparency settings for the background of a control.

By creating masks with various designs and transparency levels and applying them to controls, it is possible to create interesting effects in the appearance of controls. One such sample is shown in Figure 1.

 

Figure 1

How is opacity specified in XAML?

In XAML, the #AARRGGBB notation is used to specify a color and its opacity. In this notation 'AA' stands for the alpha value which is used to define opacity using a 2 digit hexadecimal number. 'AA' can take values from 00 (fully transparent) to FF (fully opaque).

While setting the opacity values for a mask, it is very important to note the following point:

It really does not matter what values are set to the R, G and B values of an opacity mask. The alpha value is the only important factor that determines the nature of the mask. This means that the mask itself can be of any color but this will not have any influence on how the object beneath it appears. The object beneath will be rendered depending only on the alpha values of the mask. This is shown in Figure 2.



Figure 2

How is an opacity mask applied to a Windows control?

Opacity mask can be set to a control using any type of brush such as Gradient Brush or Tile Brush. This brush should be applied to the OpacityMask property of the control. The following examples demonstrate this in detail.

Examples

Opacity mask using LinearGradient Brush

In order to apply an opacity mask in the form of a linear gradient, a LinearGradientBrush is defined and applied to the OpacityMask property of a control, say, Button, as shown in the code below. The transparency of the mask is determined by the alpha values specified in the brush definition.

<Button Width="100" Height="150" Background="DeepSkyBlue" Canvas.Top="100" Canvas.Left="275">
Button 1<Button.OpacityMask
>
<LinearGradientBrush StartPoint="0,0" EndPoint
="1,1">
<LinearGradientBrush.GradientStops
>
<GradientStop Color="#00000000" Offset="0.0"
/>
<GradientStop Color="#99000000" Offset
="0.25"/>
<GradientStop Color="#55000000" Offset="0.5"
/>
<GradientStop Color="#BB000000" Offset="0.75"
>
<GradientStop Color="#33000000" Offset="1.0"
/>
</LinearGradientBrush.GradientStops
>
</LinearGradientBrush
>
</Button.OpacityMask
>
</
Button>



Figure 3

By varying the gradient stops and start and end points, different kinds of mask patterns can be produced.

Opacity mask using RadialGradient Brush

This is very similar to the previous type. In this case the mask is defined using alpha values in a RadialGradientBrush. This brush is then applied to the OpacityMask property of a RadioButtonList control.

<RadioButtonList Width="150" Height="200" Background="FireBrick" Canvas.Left="700" Canvas.Top="100">
<RadioButtonList.OpacityMask
>
<RadialGradientBrush GradientOrigin="0.5,0.5"  Center="0.5,0.5" RadiusX="0.5" RadiusY
="0.5">
<RadialGradientBrush.GradientStops
>
<GradientStop Color="#aa000000" Offset="0"
/>
<GradientStop Color="#55000000" Offset
="0.25"/>
<GradientStop Color="#aa000000" Offset="0.5"
/>
<GradientStop Color="#55000000" Offset
="0.75"/>
<GradientStop Color="#aa000000" Offset="1"
/>
</RadialGradientBrush.GradientStops
>
</RadialGradientBrush
>
</RadioButtonList.OpacityMask
>
.........
</RadioButtonList>

 

Figure 4

Opacity mask using Image Brush

An Image Brush can be used to produce interesting masks. The image (used by the Image Brush) can be constructed such that different portions of it have different levels of transparency. When such a mask is applied to a control, the effect of the mask gets beautifully reflected on the background of the control.

An Image Brush is constructed by using its ImageSource property to specify the name of the file to use as mask. In Figure 5, the mask is applied to a ComboBox control.

<ComboBox Width="200" Height="150" Background="Gold" Canvas.Left="700" Canvas.Top="100">
<ComboBoxItem Foreground="Red">Option 1</ComboBoxItem
>
<ComboBoxItem Foreground="Red">Option 2</ComboBoxItem
>
<ComboBoxItem Foreground="Red">Option 3</ComboBoxItem
>
<ComboBox.OpacityMask
>
<ImageBrush ImageSource
="Images\Im6.png"/>
</ComboBox.OpacityMask
>
</ComboBox>



Figure 5

In the case of a mask constructed using Image Brush, it is necessary to create the mask using a tool which specifically allows transparency values to be set on it. It is not sufficient to merely use an image (say, created using PaintBrush) which has areas with different colors and shades. In that case, even though the image has different colors, all those colors are fully opaque so it is equivalent to not setting a mask.

To be able to create an image with transparency settings, use a photo shop tool which has provision for such settings to be made.

Opacity mask using Drawing Brush

Opacity mask can also be constructed using a Drawing Brush. A Drawing Brush is used to construct drawings with basic components like rectangle and ellipse or even images or text. While specifying the color of the brush to be used for the drawing, the opacity is also specified using #AARRGGBB notation. Now if the brush is applied to the Opacity Mask property of a control, the effect of the mask can be seen as shown in Figure 6.

. . . . .
<Button.OpacityMask
>
<DrawingBrush
>
<DrawingBrush.Drawing
>
<GeometryDrawing Brush
="#AA00FF00">
<GeometryDrawing.Geometry
>
<GeometryGroup
>
<EllipseGeometry RadiusX="75" RadiusY
="75"/>
<EllipseGeometry RadiusX="75" RadiusY
="65"/>
<EllipseGeometry RadiusX="75" RadiusY
="55"/>
<EllipseGeometry RadiusX="45" RadiusY
="100"/>
<EllipseGeometry RadiusX="35" RadiusY
="100"/>
<EllipseGeometry RadiusX="25" RadiusY
="100"/>
<EllipseGeometry RadiusX="15" RadiusY
="100"/>
</GeometryGroup
>
</GeometryDrawing.Geometry
>
</GeometryDrawing
>
</DrawingBrush.Drawing
>
</DrawingBrush
>
</
Button.OpacityMask
>
. . . . . 



Figure 6

Opacity mask using a pattern

It is possible to create tiled patterns of a given image or drawing using ImageBrush or DrawingBrush, which are types of Tile Brushes. Such a tiled pattern can itself be used as an opacity mask.

<Label Width="200" Height="150" Canvas.Left="5" Canvas.Top="400">
<Label.Background
>
<ImageBrush ImageSource="Images\sunset.jpg"
/>
</Label.Background
>
<Label.OpacityMask
>
<ImageBrush Stretch="UniformToFill" AlignmentX="Center" Viewport="0,0,25,25" ViewportUnits="Absolute" TileMode="Tile" ImageSource="Images\Im7.png"
/>
</Label.OpacityMask
>
</
Label>
 


Figure 7

Image Brush is used to create a pattern in the above example. The type of tile pattern is specified using various attributes of the Image Brush. A pentagon shaped mask is used. The pentagon itself has a certain degree of opacity while areas surrounding it are completely transparent. When such a mask is applied to a base image, portions of the image lying beneath the pentagon shape are made partially visible while the remaining portions become completely transparent.

Conclusion

Thus we have seen that it is possible to introduce interesting patterns and variations in the look of Windows Controls using opacity masks. We have also seen that XAML provides a simple way to implement opacity masks without using procedural code.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sowbhagya Vaidyaraman
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.