Posted by: Zeeshan Amjad | September 20, 2009

Visual Class in WPF using VB.Net

This abstract class is defined in System.Windows.Media namespace and responsible for all visual type. In other words, whatever display on the screen is inherited from Visual class or its cousin Visual3D class. This class gives the support of hit test, transformation, clipping and bounding box calculation. It is important to note that this class doesn’t provide any high level functionality of WPF such as data binding, lay out, style and event handling. Here is a class diagram of Visual class.


Although this is an abstract class, but just like the DispatcherObject class, none of its function is defined abstract. It means if you simply inherit class from Visual then you can create its instance without overriding any method. Here is a sample code to demonstrate this.

  1: Imports System.Windows
  2: Imports System.Windows.Media
  4: Public Class MyVisual
  5:     Inherits Visual
  7: End Class
  9: Public Class wpf
 10:     Public Shared Sub Main()
 12:         Dim win As Window
 13:         win = New Window()
 14:         win.Title = "Visual Class"
 15:         win.Width = 300
 16:         win.Height = 200
 18:         Dim v As MyVisual
 19:         v = New MyVisual()
 20:         win.Content = v
 22:         Dim app As Application
 23:         app = New Application()
 24:         app.Run(win)
 25:     End Sub
 26: End Class

Here note that we have to include one more namespace System.Windows.Media because Visual class is defined in this namespace.

The output of this program display “MyVisual” inside the window. This is because frame work calls the ToString function if class is not inherited from UIElement class (next class in the class hierarchy chain). Frameworks call OnRender functions for all the classes that are derived from UIElement and ToString otherwise. If we call the ToString function on the instance of “MyVisual” class then we will get the string “MyVisual”. Here is the output of this program.


If we override the ToString function in MyVisual class and return something else then we will see the same string in the output window. Here is an example to demonstrate this.

  1: Imports System.Windows
  2: Imports System.Windows.Media
  4: Public Class MyVisual
  5:     Inherits Visual
  7:     Public Overrides Function ToString() As String
  8:         ToString = String.Format("This is my Visual class")
  9:     End Function
 11: End Class
 13: Public Class wpf
 14:     Public Shared Sub Main()
 16:         Dim win As Window
 17:         win = New Window()
 18:         win.Title = "Visual Class"
 19:         win.Width = 300
 20:         win.Height = 200
 22:         Dim v As MyVisual
 23:         v = New MyVisual()
 24:         win.Content = v
 26:         Dim app As Application
 27:         app = New Application()
 28:         app.Run(win)
 29:     End Sub
 30: End Class

Here is the output of this program.


On the other hand, if we create the object of UIElement class (next class in the hierarchy chain) then frame work will call OnRender function instead of ToString. Here is a sample program to demonstrate this concept.

  1: Imports System.Windows
  2: Imports System.Windows.Media
  5: Public Class wpf
  6:     Public Shared Sub Main()
  8:         Dim win As Window
  9:         win = New Window()
 10:         win.Title = "Visual Class"
 11:         win.Width = 300
 12:         win.Height = 200
 14:         Dim uie As UIElement
 15:         uie = New UIElement()
 16:         win.Content = uie
 18:         Dim app As Application
 19:         app = New Application()
 20:         app.Run(win)
 21:     End Sub
 22: End Class

Output of this program is a blank window with a caption “UIElement Class”. Also note that we do not have to use the name space System.Windows.Media because UIElement class is defined in System.Windows namespace. We are going to explore more about UIElement class in the coming sections.

Its cousin Visual3D class is providing almost the same functionality that Visual class gives. The main difference is it provides services for 3D objects. Visual3D class diagram is simpler than Visual. Here is a class hierarchy of Visual3D class.


Visual class is an interface between managed and unmanaged code. It calls the unmanaged DirectX code using milcore.dll. It can be demonstrated with the following diagram.



Leave a Reply

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

You are commenting using your 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


%d bloggers like this: