Posted by: Zeeshan Amjad | September 23, 2009

Visual Class in WPF using VC++.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.

vcwpf_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: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: using namespace System::Windows::Media;
  5: public ref class MyVisual : public Visual
  6: {
  7: };
  8: 
  9: [STAThread]
 10: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 11: 				   LPSTR lpCmd, int nCmd)
 12: {
 13: 
 14: 	Window^ win = gcnew Window();
 15: 	win->Title = "Visual Class";
 16: 	win->Width = 300;
 17: 	win->Height = 200;	
 18: 
 19: 	MyVisual^ v = gcnew MyVisual();
 20: 	win->Content = v;
 21: 
 22: 	Application^ app = gcnew Application();
 23: 	app->Run(win);
 24: }
 25: 

 

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.

 

vcwpf_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: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: using namespace System::Windows::Media;
  5: 
  6: public ref class MyVisual : public Visual
  7: {
  8: public:
  9: 	virtual String^ ToString() override
 10: 	{
 11: 		return gcnew String("This is my Visual Class");
 12: 	}
 13: };
 14: 
 15: [STAThread]
 16: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 17: 				   LPSTR lpCmd, int nCmd)
 18: {
 19: 
 20: 	Window^ win = gcnew Window();
 21: 	win->Title = "Visual Class";
 22: 	win->Width = 300;
 23: 	win->Height = 200;	
 24: 
 25: 	MyVisual^ v = gcnew MyVisual();
 26: 	win->Content = v;
 27: 
 28: 	Application^ app = gcnew Application();
 29: 	app->Run(win);
 30: }
 31: 

 

Here is the output of this program.

 

vcwpf_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: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: 
  5: [STAThread]
  6: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  7: 				   LPSTR lpCmd, int nCmd)
  8: {
  9: 
 10: 	Window^ win = gcnew Window();
 11: 	win->Title = " UIElement Class";
 12: 	win->Width = 300;
 13: 	win->Height = 200;	
 14: 
 15: 	UIElement^ uie = gcnew UIElement();
 16: 	win->Content = uie;
 17: 
 18: 	Application^ app = gcnew Application();
 19: 	app->Run(win);
 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.

 

vcwpf_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.

 

vcwpf_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: