Posted by: Zeeshan Amjad | August 22, 2009

Using DrawingVisual class in VC++


This lightweight class is used to render the vector graphics, images, shapes, text etc on the screen. It is important that this class doesn’t provide any feature layout, input handling, focus and event handling to improve the performance.

Just like the ContainerVisual class, this class also needs container to host it. Container of this class has to be a object of FrameworkElement or its subclass. We can use Window as a container of this, because Window is also inherited indirectly from FrameworkElement class. We get a drawing context from RenderOpen function to draw something on the Visual. Here is an example to show this.

  1: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: using namespace System::Windows::Media;
  5: using namespace System::Globalization;
  6: 
  7: public ref class MyWindow : public Window
  8: {
  9: private:
 10: 	DrawingVisual^ dv;
 11: 
 12: public:
 13: 	MyWindow()
 14: 	{
 15: 		Title = "DrawingVisual Class";
 16: 		Width = 400;
 17: 		Height = 300;
 18: 		WindowStartupLocation = Windows::WindowStartupLocation::CenterScreen;
 19: 
 20: 		dv = gcnew DrawingVisual();
 21: 		
 22: 		DrawingContext^ dc = dv->RenderOpen();		
 23: 
 24: 		Rect^ rect = gcnew Rect(Point(75, 75), Size(25, 50));
 25: 		dc->DrawRectangle(Brushes::Yellow, gcnew Pen(Brushes::Navy, 2), *rect);
 26: 		dc->DrawEllipse(Brushes::Wheat, gcnew Pen(Brushes::Brown, 3),
 27: 			Point(200, 150), 25, 50);
 28: 	
 29: 		FormattedText^ ft = gcnew FormattedText(gcnew String("Hello from DrawingVisual"),
 30: 			CultureInfo::GetCultureInfo("en-us"),
 31: 			Windows::FlowDirection::LeftToRight,
 32: 			gcnew Typeface("Verdana"),
 33: 			24,
 34: 			Brushes::White);
 35: 
 36: 		dc->DrawText(ft, Point(0, 0));
 37: 		dc->Close();
 38: 
 39: 		AddVisualChild(dv);
 40: 	}
 41: 
 42: 	virtual int VisualChildrenCount() override
 43: 	{
 44: 		return 1;
 45: 	}
 46: 
 47: 	virtual Visual^ GetVisualChild(int index) override
 48: 	{
 49: 		if (index > 0)
 50: 		{
 51: 			throw gcnew ArgumentOutOfRangeException("index");
 52: 		}
 53: 
 54: 		return dv;
 55: 	}
 56: };
 57: 
 58: [STAThread]
 59: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 60: 				   LPSTR lpCmd, int nCmd)
 61: {
 62: 	MyWindow^ win = gcnew MyWindow();
 63: 
 64: 	Application^ app = gcnew Application();
 65: 	app->Run(win);
 66: }
 67: 

This program draws one ellipse, one rectangle and writes one string “Hello from DrawingVisual” on the screen. Here is the output of this program.

 

vcwpf_19

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: