Page 1 of 1

MFC Length Converter Tutorial: A Basic MFC application which converts between various lengths: Rate Topic: -----

#1 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Posted 13 July 2008 - 02:41 PM

MFC tutorials which showcase an easy to follow very basic idea are a rarity to come by it seems. I feel as though I should attempt to contribute a basic template for those plagued by such feelings and hope to quell their angry hearts. This tutorial will go step by step to ensure that even the most confused can at least obtain a general idea. This tutorial will be revolved around creating it in Microsoft Visual Studio 2008, exact steps and images shown may slightly vary depending on which version you have. With this in mind, let us begin:

Create a MFC application:

-Ok, fire up Visual Studio, and go to File->New->Project
-You will be prompted by a pop up box, highlight “MFC application” and enter a name, for me I choose “Length Converter”, pick whatever makes you happy.
Posted Image

Press ok, another box should pop up. Hit the “Next” button and in the upper left hand corner, choose “Dialog Based” from the radio buttons as shown in the picture:

Posted Image

Ok now click “Finish” you have just created the MFC application.

You should now see an empty dialog box, which we will right click and delete everything off of.
(Short note: If you do not see the dialog box go to View->Resource View. This will bring up a side menu with “Length Converter”, or whatever you called it. Expand it, expand “Length Converter.rc” and finally expand “Dialog”. You should now see IDD_LENGTHCONVERTER, click on that and it will pop up).

Ok here is our empty dialog box:

Posted Image

On your right, you should see a side menu called “Toolbox”, if you do not go to View->Toolbox and it should pop right up. Now let’s select the “Edit Control” and click on your empty form to make a blank box. Make a couple of these like so:

Posted Image

Ok, now let’s put some type of a label by these boxes so we know what we are looking at. Let’s use the “Static Text” from the toolbox on the right. Click and drag them over on the empty dialog box and type in the lengths as such:

Posted Image

This will be a somewhat simple interface, so for the last part here let’s add two buttons on the form one will be called “Calculate”, the other “Clear”:

Posted Image

We have about all of the functionality on this form that we will need, so let us get down to the actual coding aspect of this form. We need to add variables for each of the length measurements, all of which we will make as doubles. So how do you create a variable? Well right click on the “Edit Control” box you created earlier next to “Inches” and select “Add Variable”.

You should get something similar to the following pop up window:

Posted Image

We now need to tell it what we need created, a double variable for inches. So let us do just that. Under the “Category” select “Value” and under the “Variable type select “double”, finally we need to give this variable a name; “inches” will do just fine. Make sure your window looks like mine:

Posted Image

Repeat this process for every edit box, making sure to establish a new and unique name for each of the variables we will create.

Once you have a variable established for each of the edit control boxes, double click on the “Clear” button, it should take you to an empty function definition, which we will fill in.

Posted Image

This is where we will add all of the code necessary for what the “Clear” button is intended to do, clear the edit boxes on the side. So remove the TODO comment, and fill in the function definition with the following code, which sets all of our variables to “0”.

void CLengthConverterDlg::OnBnClickedButton2()
{
	UpdateData(true);
	inches = 0;
	centimeters = 0;
	feet = 0;
	yards = 0;
	meters = 0;
	millimeters = 0;
	miles = 0;
	UpdateData(false);
}


Now, whenever we click the “Clear” button, all of the fields will be set to “0”.

Now, lets get to adding some functionality to the “Calculate” button which will do all of our dirty work for us. Likewise, double click on “Calculate” and it will bring up a unique empty function definition which we again will fill in.

Now, we need to think for a minute on how everything is going to be implemented, a user is going to enter a length in one of the fields on his right and click “Calculate” which should fill in the remaining fields with an equivalent length measurement. So, we need to make separate conditions, for example if the “inches” is not equal to 0, which means the user filled it in and hits calculate this is what we want it to do. The code would look something like this:

	if ( inches != 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){
		centimeters = inches * 2.54;
		feet = inches * 0.0833333333333333;
		yards = inches * 0.0277777778;
		meters = inches * 0.0254;
		miles = inches * 25.4;
		millimeters = inches * 1.57828283 * pow(10.0,-5.0);
	}//end if



Notice what this code is doing, it’s very straight forward. If inches has something within its field and somebody hits calculate the other variables are calculated as well, each being multiplied by the equivalent of what 1 inch is. Now we just need to implement this for every case, the code below does that job.

UpdateData(true);
	if ( inches != 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){
		centimeters = inches * 2.54;
		feet = inches * 0.0833333333333333;
		yards = inches * 0.0277777778;
		meters = inches * 0.0254;
		miles = inches * 25.4;
		millimeters = inches * 1.57828283 * pow(10.0,-5.0);
	}//end if

	if ( centimeters != 0 && inches == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){
		inches = centimeters * 0.393700787;
		feet = centimeters * 0.032808399;
		yards = centimeters * 0.010936133;
		meters = centimeters * 0.01;
		miles = centimeters * 10;
		millimeters = centimeters * 6.21371192 * pow(10.0,-6.0);
	}//end if

	if ( feet != 0 && inches == 0 && centimeters == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){
		inches = feet * 12;
		centimeters = feet * 30.48;
		yards = feet * 0.333333333;
		meters = feet * 0.3048;
		miles = feet * 304.8;
		millimeters = feet * 0.000189393939;
	}//end if

	if ( yards != 0 && inches == 0 && centimeters == 0 && feet == 0 && meters == 0 && miles == 0 && millimeters == 0 ){
		inches = yards * 36;
		centimeters = yards * 91.44;
		feet = yards * 3;
		meters = yards * 0.9144;
		miles = yards * 914.4;
		millimeters = yards * 0.000568181818;
	}//end if

	if ( meters != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && miles == 0 && millimeters == 0 ){
		inches = meters * 39.3700787;
		centimeters = meters * 100;
		feet = meters * 3.2808399;
		yards = meters * 1.0936133;
		miles = meters * 1000;
		millimeters = meters * 0.000621371192;
	}//end if

	if ( miles != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && millimeters == 0 ){
		inches = miles * 0.0393700787;
		centimeters = miles * 0.1;
		feet = miles * 0.0032808399;
		yards = miles * 0.0010936133;
		meters = miles * 0.001;
		millimeters = miles * 6.21371192 * pow(10.0,-7.0);
	}//end if

	if ( millimeters != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 ){
		inches = millimeters * 63360;
		centimeters = millimeters * 160934.4;
		feet = millimeters * 5280;
		yards = millimeters * 1760;
		meters = millimeters * 1609.344;
		miles = millimeters * 1609344;
	}//end if
	UpdateData(false);



Notice that every function begins with
 UpdateData(true); 
and
 UpdateData(false);
This is an essential piece to add, if it is left out it will not compile properly. Also make a note to “#include<cmath>” for the “pow” function that is being used here if you directly copy this code. Just put it at the top of the file with the functions that you are filling in.

Ok, so now we seem to have every basic case covered. Compile, run, and give it a shot:

Posted Image

Well hope this has shined some light for some of you who have less experience with creating MFC applications, if you have any questions or comments please do not hesitate to contact me via <DIC>.


Is This A Good Question/Topic? 0
  • +

Replies To: MFC Length Converter Tutorial:

#2 rosdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-February 09

Posted 16 February 2009 - 07:38 PM

Good one.. very basic and easy to follow tutorial.. good for someone learning MFC for the first time.
Was This Post Helpful? 0
  • +
  • -

#3 THE_NET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-May 09

Posted 05 May 2009 - 10:22 PM

Congratulations captainhampton :^:

Nice tutorial!!! It was my second program using the library "Microsoft Foundation Classes" (MFC) in Visual Studio 2008 :D

I wrote a lot of softwares in C and C++, but I am new using this library :rolleyes:

bye... ;)
Was This Post Helpful? 0
  • +
  • -

#4 garywillams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-June 09

Posted 23 June 2009 - 12:08 PM

Thanks for a great beginner tutorial.

Are there any more tutorial on MFC in VS 2008. I am new to this and looking for tutorials, where I can learn how to draw graphs in multidoc application.

Thanks in advance
:)
Was This Post Helpful? 0
  • +
  • -

#5 anorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 09

Posted 10 October 2009 - 01:07 PM

Great tutorial. However, I must mention that some of the conversions are mixed up. Most often it is miles/X millimeters/X. But this in no way affects to legitimacy of the code, just the results.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1