Posted by: Zeeshan Amjad | September 18, 2009

Visual Class in WPF using C#


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.

wpf_12

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: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: 
  5: public class MyVisual : Visual
  6: {
  7: }
  8: 
  9: public class wpf10
 10: {
 11:     [STAThread]
 12:     public static void Main()
 13:     {
 14:         Window win = new Window();
 15:         win.Title = "Visual Class";
 16:         win.Width = 300;
 17:         win.Height = 200;
 18: 
 19:         MyVisual v = new MyVisual();
 20:         win.Content = v;
 21: 
 22:         Application app = new Application();
 23:         app.Run(win);
 24:     }
 25: }
 26: 

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.

wpf_13

 

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: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: 
  5: public class MyVisual : Visual
  6: {
  7:     public override String ToString()
  8:     {
  9:         return String.Format("This is my Visual class");
 10:     }
 11: }
 12: 
 13: public class wpf11
 14: {
 15:     [STAThread]
 16:     public static void Main()
 17:     {
 18:         Window win = new Window();
 19:         win.Title = "Visual Class";
 20:         win.Width = 300;
 21:         win.Height = 200;
 22: 
 23:         MyVisual v = new MyVisual();
 24:         win.Content = v;
 25: 
 26:         Application app = new Application();
 27:         app.Run(win);
 28:     }
 29: }
 30: 

Here is the output of this program.

wpf_14

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: using System;
  2: using System.Windows;
  3: 
  4: public class wpf12
  5: {
  6:     [STAThread]
  7:     public static void Main()
  8:     {
  9:         Window win = new Window();
 10:         win.Title = "UIElement Class";
 11:         win.Width = 300;
 12:         win.Height = 200;
 13: 
 14:         UIElement uie = new UIElement();
 15:         win.Content = uie;
 16: 
 17:         Application app = new Application();
 18:         app.Run(win);
 19:     }
 20: }
 21: 

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.

wpf_15

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.

 

wpf_16

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: