Posted by: Zeeshan Amjad | August 4, 2009

WPF Programming with VC++


WPF is not limited to C# or VB.Net. In fact we can write WPF code in any .Net language. Here is the simple application to calculate time value of money in VC++.

  1: #include <windows.h>
  2: using namespace System;
  3: using namespace System::Windows;
  4: using namespace System::Windows::Controls;
  5: using namespace System::Windows::Media;
  6: 
  7: public ref class MainWindow : public Window
  8: {
  9: private:
 10: 	Button^			btnCalculate;
 11: 	Button^			btnExit;
 12: 	Label^			lblPresent;
 13: 	TextBox^		txtPresent;
 14: 	Label^			lblRate;
 15: 	TextBox^		txtRate;
 16: 	Label^			lblYear;
 17: 	TextBox^		txtYear;
 18: 	Label^			lblFuture;
 19: 	Label^			lblFutureValue;
 20: 	Grid^			grid;
 21: 
 22: public:
 23: 	MainWindow()
 24: 	{
 25: 		Title = "Time Value Of Money";
 26: 		Height = 300;
 27: 		Width = 400;
 28: 		WindowStyle = Windows::WindowStyle::ToolWindow;
 29: 		ResizeMode = Windows::ResizeMode::NoResize;
 30: 		WindowStartupLocation = Windows::WindowStartupLocation::CenterScreen;
 31: 
 32: 		LinearGradientBrush^ gBrush = gcnew LinearGradientBrush();
 33: 		gBrush->StartPoint = Point(0, 0);
 34: 		gBrush->EndPoint = Point(1, 1);
 35: 		gBrush->GradientStops->Add(gcnew GradientStop(Colors::AliceBlue, 0));
 36: 		gBrush->GradientStops->Add(gcnew GradientStop(Colors::Blue, 1));
 37: 
 38: 		grid = gcnew Grid();
 39: 		grid->Background = gBrush;
 40: 
 41: 		// Create Rows in grid layout
 42: 		Windows::Controls::RowDefinition^ row1 = gcnew Windows::Controls::RowDefinition();
 43: 		Windows::Controls::RowDefinition^ row2 = gcnew Windows::Controls::RowDefinition();
 44: 		Windows::Controls::RowDefinition^ row3 = gcnew Windows::Controls::RowDefinition();
 45: 		Windows::Controls::RowDefinition^ row4 = gcnew Windows::Controls::RowDefinition();
 46: 		Windows::Controls::RowDefinition^ row5 = gcnew Windows::Controls::RowDefinition();
 47: 
 48: 		// Create Columns in grid layout
 49: 		Windows::Controls::ColumnDefinition^ col1 = gcnew Windows::Controls::ColumnDefinition();
 50: 		Windows::Controls::ColumnDefinition^ col2 = gcnew Windows::Controls::ColumnDefinition();
 51: 
 52: 		// Add all the rows into grid
 53: 		grid->RowDefinitions->Add(row1);
 54: 		grid->RowDefinitions->Add(row2);
 55: 		grid->RowDefinitions->Add(row3);
 56: 		grid->RowDefinitions->Add(row4);
 57: 		grid->RowDefinitions->Add(row5);
 58: 
 59: 		// Add all the columns into grid
 60: 		grid->ColumnDefinitions->Add(col1);
 61: 		grid->ColumnDefinitions->Add(col2);
 62: 
 63: 		// Present Value Label
 64: 		lblPresent = gcnew Label();
 65: 		lblPresent->Margin = Thickness(5);
 66: 		lblPresent->Content = "Present Value";
 67: 		lblPresent->VerticalAlignment = Windows::VerticalAlignment::Center;
 68: 		grid->SetRow(lblPresent, 0);
 69: 		grid->SetColumn(lblPresent, 0);
 70: 
 71: 		// Present Value text
 72: 		txtPresent = gcnew TextBox();
 73: 		txtPresent->Margin = Thickness(5);
 74: 		txtPresent->VerticalAlignment = Windows::VerticalAlignment::Center;
 75: 		grid->SetRow(txtPresent, 0);
 76: 		grid->SetColumn(txtPresent, 1);
 77: 
 78: 		// Interest Rate Label
 79: 		lblRate = gcnew Label();
 80: 		lblRate->Margin = Thickness(5);
 81: 		lblRate->Content = "Interest Rate";
 82: 		lblRate->VerticalAlignment = Windows::VerticalAlignment::Center;
 83: 		grid->SetRow(lblRate, 1);
 84: 		grid->SetColumn(lblRate, 0);
 85: 
 86: 		// Interest Rate text
 87: 		txtRate = gcnew TextBox();
 88: 		txtRate->Margin = Thickness(5);
 89: 		txtRate->VerticalAlignment = Windows::VerticalAlignment::Center;
 90: 		grid->SetRow(txtRate, 1);
 91: 		grid->SetColumn(txtRate, 1);
 92: 
 93: 		// Year Label
 94: 		lblYear = gcnew Label();
 95: 		lblYear->Margin = Thickness(5);
 96: 		lblYear->Content = "No of Years";
 97: 		lblYear->VerticalAlignment = Windows::VerticalAlignment::Center;
 98: 		grid->SetRow(lblYear, 2);
 99: 		grid->SetColumn(lblYear, 0);
100: 
101: 		// Year text
102: 		txtYear = gcnew TextBox();
103: 		txtYear->Margin = Thickness(5);
104: 		txtYear->VerticalAlignment = Windows::VerticalAlignment::Center;
105: 		grid->SetRow(txtYear, 2);
106: 		grid->SetColumn(txtYear, 1);
107: 
108: 		// Future Value Label
109: 		lblFuture = gcnew Label();
110: 		lblFuture->Margin = Thickness(5);
111: 		lblFuture->Content = "Future Value";
112: 		lblFuture->VerticalAlignment = Windows::VerticalAlignment::Center;
113: 		grid->SetRow(lblFuture, 3);
114: 		grid->SetColumn(lblFuture, 0);
115: 
116: 		// Future Value Label
117: 		lblFutureValue = gcnew Label();
118: 		lblFutureValue->Margin = Thickness(5);
119: 		lblFutureValue->Content = "";
120: 		lblFutureValue->VerticalAlignment = Windows::VerticalAlignment::Center;
121: 		grid->SetRow(lblFutureValue, 3);
122: 		grid->SetColumn(lblFutureValue, 1);
123: 
124: 		// Exit Button
125: 		btnExit = gcnew Button();
126: 		btnExit->Margin = Thickness(25, 10, 25, 10);
127: 		btnExit->Content = L"Exit";
128: 		btnExit->Click += gcnew RoutedEventHandler(this, &MainWindow::OnBtnExit);
129: 		grid->SetRow(btnExit, 4);
130: 		grid->SetColumn(btnExit, 0);
131: 
132: 		// Calculate Button
133: 		btnCalculate = gcnew Button();
134: 		btnCalculate->Margin = Thickness(25, 10, 25, 10);
135: 		btnCalculate->Content = L"Calculate";
136: 		btnCalculate->Click += gcnew RoutedEventHandler(this, &MainWindow::OnBtnCalculate);
137: 		grid->SetRow(btnCalculate, 4);
138: 		grid->SetColumn(btnCalculate, 1);
139: 
140: 		// Add childern into grid layout
141: 		grid->Children->Add(lblPresent);
142: 		grid->Children->Add(txtPresent);
143: 		grid->Children->Add(lblRate);
144: 		grid->Children->Add(txtRate);
145: 		grid->Children->Add(lblYear);
146: 		grid->Children->Add(txtYear);
147: 		grid->Children->Add(lblFuture);
148: 		grid->Children->Add(lblFutureValue);
149: 		grid->Children->Add(btnCalculate);
150: 		grid->Children->Add(btnExit);
151: 
152: 		Content = grid;
153: 	}
154: 
155: 	void OnBtnExit(Object^ sender, RoutedEventArgs^ e)
156: 	{
157: 		Close();
158: 	}
159: 
160: 	void OnBtnCalculate(Object^ sender, RoutedEventArgs^ e)
161: 	{
162: 		double present = Convert::ToDouble(txtPresent->Text);
163: 		double rate = Convert::ToDouble(txtRate->Text) / 100;
164: 		double year = Convert::ToDouble(txtYear->Text);
165: 		double future = present * Math::Pow(1 + rate, year);
166: 		lblFutureValue->Content = future.ToString("C");
167: 	}
168: };
169: 
170: [STAThread] 
171: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
172: 				  LPSTR lpCmdLine, int nCmdLine)
173: {
174: 	MainWindow^ window = gcnew MainWindow();
175: 
176: 	Application^ app = gcnew Application();
177: 	app->Run(window);
178: }
179: 

We have to select CLR switch to compile this project to access .Net class library. Here is the output of this program.

TVOM

Advertisements

Responses

  1. […] App class as a ref class. Once I came across the ref classes when working on .Net using Managed C++ here is one example of […]


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: