by Jim Krumm
Click here to go to the Computer Science Department Home Page for
Below are some windows based applications written in C++ in our MFC class (COSC2405). Conversion from Studio.Net 2003 to Studio.Net 2005 to Studio.Net 2008 and back can be challenging. Some of the biggest differences deal with strings, the declaration of global variables (particularly global strings), when saving to file, asynchronous keys, and working with databases. In Studio 2003 a string declared String *mystring would be declared in Studio 2005 as String ^mystring. Concatenating strings in Studio.NET 2005 is a snap using "+" between multiple strings. This replaces Studio.NET's 2003's "Concat" method. In Studio.NET 2003 concatenating "something" to "somethingElse" and storing the result in strCat was written: strCat = strCat->Concat(something," ",somethingElse). Studio.NET 2005 does a much better job than Studio.NET 2003 with rewriting code after controls have been removed from a project. In the past, removing a control, like a button, from a compiled program often left you with a program that wouldn't compile. Studio.NET 2005 is also much quicker compiling than Studio.NET 2003 in both the debug and non-debug mode. Code which used to take minutes compiling on a 1 GHz processor now takes seconds.
Source Code Examples of Windows Applications Written in C++ Studio.NET 2005:
Here a series of programs and tutorials that show how to write windows applications in C++ in the Studio.NET 2005 environment.
Tutorial 1: Simple Intro on Setting up a Project and Using Basic Controls
Tutorial 2: Windows Basic Form Controls in C++ and Their Properties
Tutorial 3: Windows Forms and Useful ListBox Properties
Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands
Tutorial 6: File Saving, Opening and Using Common Dialog Boxes
Tutorial 7: Concatenating Strings Together in the Windows C++ Environment:
Tutorial 8: Detecting Asynchronous Keys
Tutorial 9: Using Arrays of Strings
Tutorial 10: Setting up an Access DataBase with a Windows C++ Application
Tutorial 11: Visual AddressBook using an Access Database and XML
Tutorial 12: Making A Setup or Deployment Package of a Microsoft Windows Program in C++
Tutorial 13: Pong as Visual Studio.Net 2005 C++ Program
Tutorial 2: Windows Basic Form Controls in C++ and Their Properties This program will show how to transfer an image to a pictureBox and do conversions from Strings to numerical values and numbers to String. It also shows how to declare Strings in this environment. Set up a form that looks like this:
Then within your project add a file called " HEAD1.jpg" to your debug folder and then add the following lines of code to the appropriate buttons functions:
|
Tutorial 3 : Windows Forms and Useful ListBox Properties This program will show how to use listboxes, and some of their more useful properties. First create a form which looks as follows, including the one listBox shown:
Code the buttons as followings:
Note: If you lose the design window (which often happens when you open the “sln” file using explorer) try the following: Go to File > New > Project > Window Form Application To make a design window appear which shows the forms go to view > designer |
Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands This tutorial will show how to move a control button around the form employing the timer. It also shows something about how placement works and the coordinate system of the form. Begin by designing a form which looks as follows:
Set the timer's enabled property to false. Then code the following to the buttons:
|
|
In this tutorial you will add a Menu to make button and textbox appear. To add a menu to your form go to the toolBox and double Click on the the mainMenu tool. This will add a menu to your form. Then type "&File" where it says "Type Here", which add File to the Menu. Then in the box below "File" type "Make Button Appear" and then "Make TextBox Appear", then a lone hyphen (-), and then E&xit. Then add the remaining items to your menu until it looks something like this:
Then add the following code to the appropriate item in the menu by double clicking on this item to get to its Code Window:
|
Tutorial 6 : File Saving, Opening and Using Common Dialog Boxes This program will show you how to save to file, open from file and how to use dialog boxes to perform these tasks. Begin by placing a text file call "myfile.txt" in your debug directory with a few lines of text placed in it of your choosing. Then design a form which looks as follow:
Add to the namespace region: using namespace System::IO; Add the following code to the relevant buttons:
|
Tutorial 7: Concatenating Strings Together in the Windows C++ Environment: This program will show how to put two strings together to create a combination of the two. This is called concatenation. Make your form look something like this:
Add the following code to the button:
|
Tutorial 8 : Detecting Asynchronous Keys This project will allow you to move a label around your form using Asynchronous Keys. It is called asynchronous because more than one key can be pressed at a time and it will be recognized. This is usually something that isn't allowed. First set up your form to look as follows (and yes a timer needs to be added). A big change in Studio 2005 (from Studio 2003) is that you must include the dynamic link library user32.dll for this program to allow the asynchronous reading of key presses.
Set the timer to be enabled with an interval of 20. These means it goes off every 20/1000 of a second. Next code the following, add everything in yellow:
|
Tutorial 9: Using Arrays of Strings In this tutorial you will see how to create arrays of strings. Make a form which looks as follows:
|
Tutorial 10: Setting up an Access DataBase with a Click here if you would like the source code to this file and its database. This program shows how to insert a record into a database using SQL commands and Access 2003. First create a database by entering data called “phonebook.mdb” on the root of your C:\ drive using Access. This should look as follows:
When you finish entering your data go to file and save the table as Table1 (the default). Say yes to creating a primary field. Since it matters where you put your database, it is easiest to put it in the root of your C:\ drive after you have created it. Also be careful, databases may be case sensitive. Close Access and your database before going on! Start a project. Add a DataGridView object to the form from data in the toolbox. Click on the arrow going to the right in the top right of dataGridView1.
Select Choose Data Source
and select Add Project Data Source.
Select database and click next.
Select New Connection.
This will trigger the appearance of the following dialog window:
Select Change, which will cause the following window to appear and select “Microsoft Access Database File” and then OK. This will cause you to return to the Add Connection Dialog which looks as follows. Click browse and navigate to and select phonebook.mdb. Don’t include a password but leave Admin as shown. Click on test connection and make sure a connection to the database exists. Then click OK. Back at the wizard click Next.
In the following dialog window select Tables as shown and click Finish.
This should take you back to your original form. Go the ToolBox and right click in the Data region of the toolbox and select “Choose Items."
Select OleDbDataAdapter and OleDbConnection from the .NET Framework Components by putting check in its box and press OK. This will add the OleDbDataAdapter and OleDbConnection tools to the toolBar.
Double click on the OleDbDataAdapter, adding it to your project. This will trigger the appearance of the Data Adapter Configuration Wizard as shown below.
Click the down arrow below "select which data connection should the data adapter use" and select phonebook.mdb and click next. In the following dialog window select Use SQL statements.
In the next window click on Query Builder…
In the next dialog window select Add, then close this “Add Table Window”. As in the following picture select All Columns and click on OK.
This should lead to the dialog box:
Click Advanced options. Click OK. Click Finish.
If something goes wrong in setting up your database, it is very, very difficult to fix. You may have to go to view, select Server Explorer, right click on the database you just configured and select delete. Then remove the related objects on your form. If this doesn’t clean things out so you get the above window you may have to close your project, open a new one and start over. It may even be necessary to reboot your computer. Design a form which looks as follows:
To the right and below the First, Last, and Phone are Textboxes textBox1, textBox2, and textBox3. Below the Insert button is the large multi-lined Textbox textBox4. In your form's code window add to the using namespace area the line of code: using namespace System::Data::OleDb; Then add oleDbConnection1->Open() in the following area.
Next code the following under your insert button (named button1). This is critical: every apostrophe, quote, space, word has to be done as follows in the insert SQL call (as everything means something):
In the code above for button 1 the odd angled apostrophes are usually found on the keyboard just below the escape key. They are supposed to be there as are the straight apostrophes found on the keyboard on the double quotes key. Help with getting this line correct (particularly with the angled apostrophes) can be found for the properties window of oleDbDataAdapter1. In the properties window of oleDbDataAdapter1 under the InsertCommand property you can copy the setting “INSERT INTO `Table1` (`Last`, `First`, `Phone`) VALUES (?, ?, ?)” and paste this directly into your program. Then make the necessary changes to this line replacing everything after “VALUES” with ('"+textBox2->Text+"', '"+ textBox1->Text+"', '"+textBox3->Text+"')"; Other key database commands such as delete, update, and select are in the properties window of the oleDbDataAdapter1.
The program should work now. Confirmation will come in textBox4, but you can also check your database to see if your new entry is added. A word of caution here: sometimes a database will not allow a write if your database is open in Access. When the program runs successfully the program should look as follows:
|
Tutorial 11 : Visual AddressBook 2005 using an Access Database This program uses the automated XML environment to set up a C++ Windows Database application using Microsoft Studio 2005. This program shows how to insert a record into a database using SQL commands and Access 2003. First create a database by entering data called “phonebook.mdb” on the root of your C:\ drive using Access. This should look as follows:
When you finish entering your data go to file and save the table as Table1 (the default). Say yes to creating a primary field. Since it matters where you put your database, it is easiest to put it in the root of your C:\ drive after you have created it. Also be careful, databases may be case sensitive. Close Access and your database before going on! Start a project. Go the ToolBox and right click on the toolbox and select “choose items”. Select OleDbDataAdapter by putting an x to its left. Again put your application in the root directory. Put your database in the root (for simplicity, as the path to the database cannot change if your application undergoes name changes). Then make a Visual C++ Windows Application project called AddressBookDB. From your toolbox draw a dataGridView on your form. Within the DataGridView Tasks window (which can be activated by clicking on the small arrow appearing at the top of the DataGridView field) select “Choose Data Source.”
In the next window Click on Add Project Data Source…
In the Data Source Configuration Wizard, select Database and then next.
This will trigger the following window:
In this window select New Connection… which will display the following window:
Here click Change… which will display the following window:
Here select Microsoft Access Database File, click OK and in the previous window click browse and navigate and select phonebook.mdb. This will take you back Data Configuration Wizard. In this wizard click next which will display the following window:
Here expand and select Tables to select Table1, ID, First, First, Phone, Views. Then click Finish. This will take you back to your original form which will look something like the form which follows:
Here size the dataGridView so it displays all the fields (ID through Phone) shown above. Notice 3 tools have been automatically added to your form: phonebookDataSet, table1BindingSource, and the TableTableAdapter. Now go to the main menu as shown below
and select show Data Sources. A Data Sources window will appear. Expand the data sources so that all the data sources (Last, First, Phone) can be seen. Now drag and drop Last from the Data Sources window to your form. This will put a label and a textbox on your form tied to the Last field in your database which looks as follows:
Add the other fields of First, and Phone to your form until your program. The TextBox for Last will be automatically named: LastTextBox, for First will be named FirstTextBox, and for Phone will be named PhoneTextBox. Then add The following buttons to your form as follows: Button1 for First Item, Button2 for Next Record, Button3 for Last Record, Button4 for Add Record, Button5 for Save Database, Button6 for Delete Record, Button7 for Cancel, Button8 for Find Record and finally Button8 for Reset Data.
Next add a groupbox to labeled Find, and to this groupbox add 2 Textboxes and the following labels such that this Groupbox looks as follows. The textbox in the find groupbox for First is textBox4, and for Last is textBox4, and the label under Phone is label7 with autosize set to false and borderstyle set to fixed3D:
Next add a picturebox to the right of the find groupbox as follows and load a picture into it. Put a jpg picture in you code directory of someone to be in your phonebook named in the following fashion: “first“+“last“+“.jpg” with your cpp files. For instance a jpg of Alex Einstein would have the name: AlexEinstein.jpg. Place one picture in directory called Default.jpg and make it an image of whatever you want a “non-person picture” to look like. Finally label your form as being the phonebook database program such that the completed interface looks as follows:
Go to the code window and add the following line to the rest of the namespace values:
Next write the following code as a function:
Finally code the buttons as follows:
The finished program looks as follows:
To add a new record it is necessary to click add record, then enter the data in the Last, First, and Phone fields to the left and then click Save Database. The First Item, Next Record Last Record buttons allow you to navigate through the database. To edit, navigate to the person or do a find on them. Change the fields to the left and then click Save Database. If you change your mind, Click cancel to cancel the edit. To do a find type a first name, or a last name or both into the find fields and click find. To view all the data in the database again click reset data. |
Tutorial 12 : Making A Setup or Deployment Package of a Microsoft Windows Program in C++ By Russell Pitts Bring up the project you are working on as a Microsoft Windows Form Application which you wish to distribute. Here for the sake of simplicity I will use a simple project that has a button on it and a label called “HelloWorld”. If the button is clicked a message appears in the label that says hello. It looks as follows:
On the button is the simple code:
Rebuild the Project so the project and its files are saved. Now go to File>New Project>Other Project types>Setup and Deployment>Setup Wizard as shown in the following figure:
Under Solution, select “Add to Solution” and then enter OK. In the setup Wizard, 1 of 5, that follows enter Next...
In the next window, 2 of 5, go with the default and select Next:
In the next window, 3 of 5, select Primary output from HelloWorld and Resource Satellite DLL's from HelloWorld and select Finish.
Open the Solution explorer for the project and right click on Setup1 and select build as is shown in the following figure:
Navigate in Windows Explorer to the debug folder of Setup1 of your HelloWorld Project as illustrated in the following figure and copy the entire debug folder
Then paste the debug folder someplace else and change the name of the debug folder to Setup. These two files Setup1.msi and setup.exe in this folder are all you need to install the package.
|
Tutorial 13: Pong as a Visual Studio.Net 2005 C++ Program
Basic Setup: Make a form of size 476x382. Right click on the tool box, select choose items, com components, select windows media player. Add two media players, stay with the default names on everything. Add the left paddle (pictureBox1), the right paddle (pictureBox2), and the ball (pictureBox3). To start you can make the size of each paddle 10x48 and the ball 20x20. Position these objects as shown above. You can use images for the paddles and balls if you wish, or make the backcolor of the paddles black and the ball yellow with a border style of fixed single. Set the sizemode of all 3 objects to stretch image. Lay a large label on the bottom and set its backcolor to red. Size it to 471x37. Place 4 more labels on the large red label and label them with the defaults above showing the score of player1 and player 2. Add a mainmenu tool and have it say file. In its drop down field write speed and exit. To the right of this menu have music and add a few songs in the fields if you wish. To the right of this have a help menu (with a single field). The code goes as follows:
|
©All rights reserved by James Krumm. Originally made available at www.caspercomsci.com. Materials here can be used, and redistributed, provided proper reference is made to the origin and author(s) of these materials. Please send any corrections or suggestions to jkrumm@caspercollege.edu. Last modified May 29, 2009.
To find out more about the Casper College Computer Science Program contact us at:
James Krumm |