Welcome to your new Wikidot site

www.embedded.wikidot.com(For Embedded C programs and RTOS)www.embedded.wikidot.com
Overview
lesson1
Link listl

Visual C++/MFC Tutorial -

Brief Historybold text

In early 1980, Bjarne Stroustrup from Bell labs began the development of the C++ language. In October 1985, the first commercial release of the language appeared and along with the first edition of the book "The C++ Programming Language" by Bjarne Stroustrup.

What is C++?
C++ is a high-level object-oriented programming (also referred to as OOP) language that builds on the C language. It contains everything C does, and adds the power of OOP. Object-oriented ideas are so easy even children can (and do) understand them. Our minds think in objects, and are trained to do so from day one. Combine the easy to understand ideas behind OOP with the power of abstraction, inheritance, exceptions, etc. and you have a language that can make large, complex projects easier to develop and maintain.

What Can C++ Be Used For?
More than anything, C++ is being used to extend and build on what is already there. For example, the Microsoft Foundation Classes provide a C++ interface to the C Windows Application Programming Interface (or API). KDE provides an OOP interface for X Windows.

C++ is used to develop a wide variety of applications. Most C++ compilers, including MS-Visual C++ and Borland C++ Builder, develop Graphical User Interface (GUI) applications. C++ is also used in scientific applications. Being a high-level object-oriented language, it makes it easier for the programmer to simulate the scientific problem.

What Is The Syntax Like?
C++ compilers are compatible with C syntax. C++ adds the object-oriented programming facilities. Similar to C, C++ programs are written in several files. Usually you will have a group of source code files that are compiled separately, then linked together into a single executable file. Each source code file will have a header file that describes its contents.

Usually, each header file in C++ corresponds to a class definition, while source files have the logical implementation of the class functions.

Classes rather than functions are the cornerstone in C++. Classes contain all the member variables and functions which are automatically associated with any instance of the class. Classes usually represent actual entities in our life such as cars, trains, the human brain, etc. which makes the programming easier.

An Example C++ Program
This is an example of a C++ program making use of MFC classes and Visual Studio Application Wizard.

You can create a new project workspace in Visual C++ by following these steps:

* Select File | New. This opens the New Wizard.
* On the Projects tab, select MFC AppWizard (exe).
* Type a name for your project, such as Hello, in the Project Name field.
* Click OK. This causes the New Wizard to do two things:
o Create a project directory (specified in the Location field)
o Start the AppWizard.
* In Step 1 of the AppWizard, specify that you want to create a Single-Document application.
* Click Finish at the bottom of the wizard.

Add the following code to the OnDraw() function found in CHelloView class.

void CHelloView::OnDraw(CDC* pDC)
{
CAsdDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Display a short message to the user
pDC->TextOut(0,0,"Hello World!");
}

Lesson 1: Behind the Scenes with Handles and Messages

Lesson 1: Behind the Scenes with Handles and Messages
Though you think you want to dive right into the code, you really don't. Windows programming is overwhelming at first. Let's take a quick look at how Windows works. The backbone of all of your programming will be responding to and sending messages. What are messages? Messages are simply a 32bit number designating some event. Example: You move the mouse, a message (defined as WM_MOUSEMOVE) is 'posted' to the active window. You press a key, a message (WM_KEYDOWN) is 'posted' to the active window. You resize the window, a message (WM_SIZE) is 'posted' to the active window. Get the picture?

Now where do these messages go? They get queued up and a window eventually takes them out of the queue and reacts to them. For instance when a window gets the WM_MOVE message it changes the coordinates of the window and redraws it on the screen.

Let's move on to Handles. Windows is very much object oriented. You have several window objects (like the desktop, the program your reading this with, etc…). How does the programmer distinguish all of these things in an non-object-oriented language? He uses handles. Handles are a way to reference different windows objects. You can have handles to windows, handles to files, handles to allocated memory, handles to images, etc. You can think of them as pointers. You must create them some how. And when you are done with them, you must destroy them. If you don't you will end up with what is called a resource leak. This could bring your system to a grinding halt. So take care to always make sure they are destroyed at sometime.

Now lets tie these two things together. Say you have a window. You will have a handle to it (called an HWND). Lets name your handle your_HWND. The operating system wants to tell you to redraw your window because it was just uncovered by some other window. Windows passes you a message like this:

PostMessage(your_HWND, WM_PAINT, 0,0);

This function posts a paint messages to the window with handle your_HWND. The last two parameters are used for extra information about the message. Don't worry about them for now.

Now your application will have a function with a big case statement in it to handle all of the messages. For example:

void HandleTheMessage(long Message)
{
switch(Message)
{
case WM_PAINT:
DrawWindow();
break;

case WM_KEYDOWN:
break;

//etc…
}
}

Ok that is basically how windows works under the hood. That should be enough to get you going when we start talking about MFC.

C++ Essentials

If you want to use Microsoft Visual C++, it helps a ton if you really know C++. Everything is about classes. If you are used to plain C, you won't really see the big deal with classes until you use them for a while. Let's review what you need to know about classes to get started with VC++.

A class is a structure for the most part. Let's work with an example instead of me just telling you rules. Let's make a class to represent a line. In the .h file you would define the class as follows:

class CLine
{
int m_nX1;
int m_nY1;
int m_nX2;
int m_nY2;

public:
// constructors
CLine();
CLine(int x1, int y1, int x2, int y2);

// destructor
~CLine();

// set the line data
void SetPoints(int x1, int y1, int x2, int y2);

// draw the line
void Draw();
}

A quick word about naming conventions. Class names usually start with 'C' and the member variables usually are prefixed by a 'm_'. Then in the microsoft way you will have a letter to let you know what data type the name is and then the name of the variable. Capitalize the letter of all new words in the name. Don't use underscores and stuff like that. I recommend this Microsoft standard (called Hungarian notation) since it is widely accepted and very easy to read. If you see m_pPoint, you would assume this is a member variable of a class that points (it is a pointer) to a point. If you see fData, you would assume that it is a floating-point value.

Back to our class. The int variables are the end points of the line. Note that they are before the 'public:' part. This means that a programmer using this class will not be allowed to manipulate these guys directly. They are not for 'public' use. The functions under the public statement are for public use. The first three are called constructors. These functions are called anytime a new CLine class is created. Here are some examples when the are called:

// this calls CLine()
CLine MyLine;

// this is a pointer to a CLine class
CLine *pMyLine;

// this calls CLine()
pMyLine = new CLine;

// this is a pointer to a CLine class
CLine *pMyLine;
// this calls CLine(int x1, int y1, int x2, int y2)
pMyLine = new CLine(0,0,10,10);

// this calls CLine(int x1, int y1, int x2, int y2)
CLine MyLine(0,0,10,10);

All of these construct a line. Some initialize it to its default settings and others copy coordinates. The 'new' keyword is used to create new things in C++, like malloc in C. You need to call 'delete' for everything you say new to, like free in C. This goes for classes as well as other data types. I could allocate an array of 100 integers with:

// a pointer to some integers
int *pNumbers;

// make memory for 100 of them
pNumbers = new int[100];

// set the first element to 0
pNumbers[0]=0;

// set the last element to 99
pNumbers[99]=99;

// free the memory.
delete [] pNumbers;

Notice the [] after the delete. This is to tell the program to delete the entire array. If you say 'delete pNumbers;' you will only free memory for the first element. You will then be 'leaking' memory. Memory leaks are when you forget to delete memory. This may end up crashing your computer if you use all the computers memory.

Sorry, let's get back to the constructors for CLine. The code for these constructor functions which automatically get called when a new line is created will look like:

CLine::CLine()
{
m_nX1=0;
m_nX2=0;
m_nY1=0;
m_nY2=0;
}

CLine::CLine(int x1, int y1, int x2, int y2)
{
m_nX1=x1;
m_nX2=x2;
m_nY1=y1;
m_nY2=y2;
}

Notice that the function declaration is much like a regular 'C' function except that we put the class name and two colons in front of the function name (CLine::). One difference with constructors is that they don't have a return value. This is the case for destructors also. A destructor is the function which automagically gets called when our CLine is deleted or goes out of scope. For instance:

// this is a pointer to a CLine class
CLine *pMyLine;

// this calls CLine()
pMyLine = new CLine;

// memory for the class is cleared up and ~CLine() is called
delete pMyLine;

{
// this calls CLine()
CLine MyLine;
}

// this '}' ends the section of the program where MyLine is
// valid. ~CLine() will be called. (MyLine goes out of 'scope')

For our class, ~CLine() doesn't need to do anything. However, sometimes you may want to put your cleanup code here. Like deleting any allocated memory in your class. Since we have nothing to do out function code is empty:

CLine::~CLine()
{
// do nothing
}

Let's fill in the other 2 functions.

void CLine::SetPoints(int x1, int y1, int x2, int y2)
{
m_nX1=x1;
m_nX2=x2;
m_nY1=y1;
m_nY2=y2;

return;
}

void CLine::Draw()
{
// psuedo code here, these are operating system
// functions to draw a line
MoveTo(m_nX1, m_nY1);
LineTo(m_nX2, m_nY2);

return;
}

How would I call these functions? Here are a couple of examples. One with pointers and one without.

CLine *pLine = new CLine(0,0,10,10);
pLine->Draw();
delete pLine;

CLine MyLine;
MyLine.SetPoints(0,0,10,10);
MyLine.Draw();

That's it for the class. Now this class can be used in other classes. You can imagine a CSquare class that has 4 Cline classes in it:

class CSquare
{
CLine m_LineTop;
CLine m_LineLeft;
CLine m_LineBottom;
CLine m_LineRight;
//…
}

Or better yet, the point of all of this class stuff, you can use the CLine class to make your own class. This is done a ton in Visual C. Lets say you wanted to draw lines in your program, and you thought the line class might be nice, but it is missing an important feature, it doesn't let you set the line color. Instead of writing a whole new class, you can simple inherit the CLine class. This would look like this:

class CColorLine : public CLine
{
public:
void Draw(long color);
};

What's going on here? Well with this class we have all the functionality of our other class, but now we can use this other Draw() function which allows us to set the color. The CPP code would look like:

void CColorLine::Draw(long color)
{
// psuedo code here, these are operating system
// functions to draw a line
SetColor(color);

CLine::Draw();
return;
}

Now we have all the functionality of the other class but we added an extra function called Draw. But it's the same name as our other Draw! No matter. Cpp is smart enough to know that if you call Draw(color) to use the new function, but if you call Draw() it will use the old function. The strange part of the code may be CLine::Draw(). This just tells our program to call the base class's Draw function. We save ourselves from having to write that LineTo and MoveTo code again. Pretty cool, huh? Now we can do something like this:

CColorLine MyLine;
MyLine.SetPoints(0,0,10,10);

// assuming 0 is black, this will draw a black line.
MyLine.Draw(0);

Of course I'm leaving out a ton of aspects and things here. Like defining operators, overriding functions, virtual functions, protected and private members… the list goes on. You have enough to get started though.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License