Posted by: Zeeshan Amjad | September 8, 2009

Using VisualCollection in VC++


ContainerVisual is a concrete class is used as a container for other visual object. Here is a sample code to demonstrate this class.

  1: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: using namespace System::Windows::Media;
  5: using namespace System::Windows::Input;
  6: 
  7: public ref class MyVisual : public Visual
  8: {
  9: };
 10: 
 11: public ref class MyWindow : public Window
 12: {
 13: private:
 14: 	MyVisual^ mv;
 15: 	VisualCollection^ vc;
 16: 
 17: public:
 18: 	MyWindow()
 19: 	{
 20: 		Title = "VisualCollection Class";
 21: 		Width = 300;
 22: 		Height = 200;
 23: 		WindowStartupLocation = Windows::WindowStartupLocation::CenterScreen;
 24: 
 25: 		vc = gcnew VisualCollection(this);
 26: 		mv = gcnew MyVisual();
 27: 
 28: 		vc->Add(mv);		
 29: 
 30: 		MouseDown += gcnew MouseButtonEventHandler(this, &MyWindow::OnMouseDown);
 31: 	}
 32: 
 33: 	void OnMouseDown(Object^ sender, MouseButtonEventArgs^ e)
 34: 	{
 35: 
 36: 		Point point(e->GetPosition(this).X, e->GetPosition(this).Y);
 37: 		
 38: 		Point screenPoint = mv->PointToScreen(point);
 39: 
 40: 		String^ str = String::Format("Before Calling PointToScreen ({0},{1})\n"
 41: 			"After Calling PointToScreen({2},{3})", 
 42: 			point.X, point.Y, screenPoint.X, screenPoint.Y);
 43: 
 44: 		Content = str;
 45: 	}
 46: };
 47: 
 48: [STAThread]
 49: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 50: 				   LPSTR lpCmd, int nCmd)
 51: {
 52: 	MyWindow^ win = gcnew MyWindow();
 53: 
 54: 	Application^ app = gcnew Application();
 55: 	app->Run(win);
 56: }
 57: 

The most important addition to ContainerVisual class is an addition of VisualCollection field. VisualCollection is one of very rare case in WPF that this class is not inherited by DependencyObject or DispatcherObject. This class directly inherits from the Object class. Here is a class diagram of this.

vcwpf_18

It is important to note that although this class is available only at the ContainerVisual and its subclasses, but Visual class still has two protected functions related to this named VisualChildernCount and GetVisualChild. VisualChildernCount always return 0 at Visual class level and GetVisualChild throws ArgumentOutOfRangeException. And it perfectly makes sense, because we don’t even have VisualCollection defined at this level. These functions are available only for its subclasses to override and provide proper implementation. It is one more reason to make Visual abstract class.

There is one more method to write something on the window and use Visual class functionality. Create an object of VisualCollection class and set Window class its parent at the time of it creation by passing its address in constructor. Then add the object of MyVisual class in the VisualCollection.

Here is the output of this program.

vcwpf_17

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: