Posted by: Zeeshan Amjad | June 18, 2010

Define Control Template with code using VC++


We just saw an example of defining control template with code here. In that example we define it using C#. Now we are going to do the same experiment with VC++. In VC++ we can’t use XAML directly just like C# and VB.Net. We have only two options here, first use XAML file at run time and second define everything with code. Here is a code to define the control template with VC++.

FrameworkElementFactory^ ellipse = gcnew FrameworkElementFactory(Ellipse::typeid);
ellipse->SetValue(Ellipse::FillProperty, Brushes::AliceBlue);
ellipse->SetValue(Ellipse::MarginProperty, gcnew Thickness(2));
ellipse->SetValue(Ellipse::StrokeProperty, Brushes::Blue);

FrameworkElementFactory^ grid = gcnew FrameworkElementFactory(Grid::typeid);
grid->SetValue(Grid::MarginProperty, gcnew Thickness(5));

grid->AppendChild(ellipse);

ControlTemplate^ ct = gcnew ControlTemplate(Button::typeid);
ct->VisualTree = ellipse;

Rest of the code is very similar to previous example. Here we are doing exactly the same, i.e. define two buttons and apply control template to these button. Here is complete code of the program.

#include <windows.h>

using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;
using namespace System::Windows::Shapes;

[STAThread]
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 			   LPSTR lpCmd, int nCmd)
{
	Window^ win = gcnew Window();
	win->Title = L"Button Control Template";
	win->Width = 400;
	win->Height = 300;

	StackPanel^ sp = gcnew StackPanel();

	FrameworkElementFactory^ ellipse = gcnew FrameworkElementFactory(Ellipse::typeid);
	ellipse->SetValue(Ellipse::FillProperty, Brushes::AliceBlue);
	ellipse->SetValue(Ellipse::MarginProperty, gcnew Thickness(2));
	ellipse->SetValue(Ellipse::StrokeProperty, Brushes::Blue);

	FrameworkElementFactory^ grid = gcnew FrameworkElementFactory(Grid::typeid);
	grid->SetValue(Grid::MarginProperty, gcnew Thickness(5));

	grid->AppendChild(ellipse);

	ControlTemplate^ ct = gcnew ControlTemplate(Button::typeid);
	ct->VisualTree = ellipse;

	Button^ b1 = gcnew Button();
	b1->Margin = Thickness(5);
	b1->Width = 100;
	b1->Height = 50;
	b1->Template = ct;

	sp->Children->Add(b1);

	Button^ b2 = gcnew Button();
	b2->Margin = Thickness(5);
	b2->Width = 100;
	b2->Height = 50;
	b2->Template = ct;

	sp->Children->Add(b2);

	win->Content = sp;
	Application^ app = gcnew Application();
	app->Run(win);
	return 0;
}

 

Here is the output of the program.

ButtonControlTemplate

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: