Using Controls in the Microsoft Foundation Class Library (Page 1 of 4 )
Welcome to the fourth episode of our journey into the world of the MFC library. This part will be dedicated to the introduction of standard controls, common controls, input message handling from the keyboard, and, of course, some image handling as well.
We'll create an application that will include all of these in it. I will start with the standard and common controls in the first section. The second will cover message handling from the keyboard, and in the last section you will learn how to work with images. For the image part, we will cover the basic format of MFC, the bitmap images (.bmp extension).
With the agenda in front of you, the journey can begin. I hope you will like reading it at least as much as I liked writing it. At the end off this article, your appetite for programming in MFC will be sky high.
Standard Controls
Windows without controls is like a hunter without a weapon. It simply isn't useful. Windows' interactivity is based on it. If you see static text, introduce some data into a box, push down a button, choose an option in a radio button or from a combo box, you are using the standard controls. Some may be more complex than others, but they all rely on the same concepts. We can say without a doubt that if you want to program software in Windows, you'll definitely need them. It's crucial that you understand them, so listen up carefully.
We'll try a simpler one for educational purposes - the Edit control. The edit control is represented in MFC by the CEdit class. They are used for getting text (answers) from the user. But a box of this is handier than that. It works just like a mini Notepad, as it can have more lines, it can be fully edited by the user, or it can use the Clipboard for text copying. If you are still wondering about this box, then start up the search companion, and the box where you are asked to enter the searched file name is what we are trying to create.
In the process of creation, we start by declaring a CEdit type variable. Through this we'll modify and control the edit box. It is recommended that you declare this as a member of a Dialog/View class so you can refer to it any time. It begins like this:
CEdit _edit;
At the start of the program, we create the control. Just before the window of an application is created, a WM_CREATE message will be sent by Windows, so we shall integrate this process into the OnCreate function.
int CMFC_exampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
_edit.Create( ES_AUTOHSCROLL | WS_VISIBLE | WS_BORDER,
CRect(50,100,200,120),this,IDC_EDITBOX);
return 0;
}
The first parameter list is made from the style specifications of the edit control. ES_AUTOHSCROLL is for the automatic scroll when the edit control is filled. The WS_VISIBLE and WS_BORDER will let us actually see our created control. There are many WindowStyles or EditStyles, but for more information about them, follow up in the MSDN.
The second parameter is a CRect (basically for int type in a struct) type. This is where we'll designate the size and the position of the window. The fifth parameter assigns the control to the current view, while the final argument specifies the edit control's ID (we previously defined an IDC_EDITBOX as follows: #define IDC_EDITBOX 150).
As soon as you start up the program, the result pops up in front of your eyes. Now that we created the application, we should implement a purpose for it.
The dispatched message on a content modification is EN_CHANGE. So now we must manually implement this message into the message map. Additional messages to the box can be implemented in the same way. This consists of the following steps:
First declare the message in the message map. The first parameter is for the edit controls ID, and the second is for the name of the function that will handle the message:
ON_EN_CHANGE(IDC_EDITBOX,OnEditChange)
We shall declare the function in the header file as follows:
afx_msg void OnEditChange();
Just one more step to go. Write the function, add in the implementation file the following code:
void CMFC_exampleView::OnEditChange()
{
CString text;
_edit.GetWindowTextW( text );
_edit2.SetWindowTextW( text );
}
I added a little utility for our application by adding a second CEdit variable and assigning a secondary edit control to it. I made it read only and, as the upper code shows, when we modify the text in the first box, that text will be automatically displayed in the second box. I won't go into more detail on this subject, just open the attached project and by looking into the CMFC_exampleView class, you should comprehend all of it. As a note, I have deleted the code that we included in the previous part, so it won't confuse you.
Next: Common Controls >>
More BrainDump Articles
More By Gabor Bernat