Posted by: Zeeshan Amjad | August 28, 2009

Using UIElement in VB.Net


UIElement class brings life. Here L mean “Layout”, I mean “Input”, F mean “Focus” and E mean “Event”. In other words every class inherited from UIElements has support for layout, input, focus and event handling.

This class also provides one protected virtual function name OnRender, which gives use to render for UIElement. There isn’t any implementation of this method in UIElement and derived classes are supposed to override this method and draw its contents on the screen. We have already discussed this method during the discussion of Visual class i.e. if we select the Content property of window to UIElement or any of its subclass then framework will calls OnRender function, on the other hand framework calls ToString function. Here is a simple program to show that OnRender function of UIElement class does nothing, but just provides hook up point so derived class can override it and render the contents of it.

Now let’s try to override the OnRender function of UIElement class. We inherited one class from UIElement named MyUIElement and draw one rounded rectangle with gradient brush. Here is a sample code of this program.

  1: Imports System.Windows
  2: Imports System.Windows.Media
  3: 
  4: Public Class MyUIElement
  5:     Inherits UIElement
  6: 
  7:     Protected Overrides Sub OnRender(ByVal drawingContext As System.Windows.Media.DrawingContext)
  8:         MyBase.OnRender(drawingContext)
  9:         Dim rect As Rect
 10:         rect = New Rect(New Point(10, 10), New Point(370, 250))
 11:         Dim gBrush As LinearGradientBrush
 12:         gBrush = New LinearGradientBrush(Colors.AliceBlue, Colors.Blue, _
 13:                                          New Point(0, 0), New Point(1, 1))
 14:         drawingContext.DrawRoundedRectangle(gBrush, New Pen(Brushes.Brown, 5), rect, 10, 10)
 15:     End Sub
 16: End Class
 17: 
 18: Public Class MyWindow
 19:     Inherits Window
 20: 
 21: 
 22:     Public Sub New()
 23:         Title = "MyUIElement Class"
 24:         Width = 400
 25:         Height = 300
 26:         WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
 27: 
 28:         Content = New MyUIElement()
 29: 
 30:     End Sub
 31: 
 32: End Class
 33: 
 34: Public Class wpf18
 35:     Public Shared Sub Main()
 36: 
 37:         Dim win As MyWindow
 38:         win = New MyWindow()
 39: 
 40:         Dim app As Application
 41:         app = New Application()
 42:         app.Run(win)
 43:     End Sub
 44: End Class
 45: 

Here is the output of this program.

vcwpf_20

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: