Topic : Fast Introduction to MFC
Author : Matt Swensson
Page : 1

Notice from www.cpp-home.com

This tutorial was not copyrighted. Anyway, we contacted the author but we received "Undelierable e-mail" response.
If anyone finds that to be offensive or any other sort of wrong, please contact us!





Tutorial 1


This tutorial will introduce you to the basics (very basic). You will start a new project where you will create a dialog with a button. Pressing the button will result in increasing a counter on the dialog. After finishing this project, you will have learned:
1)How to set up a project.
2)How to compile and run the program.
3)A little bit of how the VC++ classwizard works.
4)How to handle button events and update controls in your dialog.

Step 1: Starting a new project

Load up VC++, make sure there are no projects open (Close Workspace from File menu)
Select New from the File menu.
Select MFC Appwizard(exe) and type 'Tutorial1' as the project name. (never inclued my quotes unless I say to)
Click 'OK'
Select Dialog Based then click 'Finished'. (We are going to use the defaults, so you can bypass the next 4 pages
You will be told what is going to be generated, click 'Ok' Files will be created and you are now ready for Step 2!

Step 2: Modifying the dialog

Click on the ResourceView tab on the left. (You should currently be on ClassView)
Expand the 'Tutorial1 Resources' tab and then expand the 'Dialog' tab
Double click on 'IDD_TUTORIAL1_DIALOG'
You should now see the dialog to the right. Click once on the text that starts 'TODO: ...", then hit return.
Change the 'TODO: ..." to '0'.
Change the 'IDC_STATIC' to 'IDC_MYCOUNTER' and then press return.
Grab the right part of the box around the text and drag it to the left to size the box down. We don't need it that long.
Select the 'Button' tool from the resource toolbar.
Draw a button on your dialog where you would like. (you can modify what it says on the button if you like by pressing return when it is selected.)
Select the Ok button, press 'del', and do the same with the cancel button. (We won't need these)
Now to step 3!

Step 3: Adding functionality

Now we will add the functionality. Press Ctrl-W or select ClassWizard from the View menu.
Select IDC_BUTTON1 on the left side and select BN_CLICKED on the right.
Choose 'Add Function'. This will allow us to handle a button press.
Select the 'Member Variables' tab.
Select the IDC_MYCOUNTER and click 'Add Variable'. This lets us assign a variable to the control.
Add the text 'counter' to the name so the full name is 'm_counter'.
Go back to the 'Message Maps' tab and select the 'OnButton1' function in the list below.
Choose Edit Code. This will bring us to the proper function.
Delete the 'Todo:' text and enter the text below into the function:

int i = atoi(m_counter);
i++;
m_counter.Format("%d",i);
UpdateData(FALSE);


Basically what we are doing here is getting the text in the variable m_counter and converting it to an integer.
Then we add 1 to it and then put it back into the variable m_counter.
Then we call UpdateData() with FALSE as a parameter to tell the dialog to update the controls, not the variables.
This is actually a longer way to do things, but I thought I would show some other things such as CString manipulation.
Now all you have to do is compile and run the program. You can do this by clicking on the red !. If you get any errors, just go back and fix any typos.
That's it!

Tutorial 2 - How to place a PropertySheet in a dialog or View


I see many people asking how they can put a PropertySheet in one of their dialogs, or place it in one of their views and be able to have other controls all around. This tutorial teaches you how to do just that. I have seen other examples, but some of them take certain steps that I feel aren't that great. What I am saying is that there are multiple ways to implement this feature, but I like the code I use because it is fairly straightforward and easy to use.

Step 1: Create your PropertySheet

I assume at this point you have already created your application and have the view or dialog that you wish to place the PropertySheet into already created. You should be able to access this view or dialog from the resource editor so that we can take the appropriate steps to complete the implementation.
So our first step is to create a class that is derived from CPropertySheet. In this example I will call it:
CMyPropSheet.
At this point you might have some PropertyPages (the pages that will be in/on the sheet) created. That is fine.
You can add these pages to your PropertySheet in the PropertySheet's constructor.

Step 2: Modifying the dialog/view

At this point you should have the following:
1. Your application somewhat created with the proper view or dialog that you wish to add the PropertySheet accessible from the resource editor.
2. Your CPropertySheet derived class. (I called my CMyPropSheet)
3. At least one of the PropertyPages that you wish to add to the PropertySheet.
So now we are set. Open up your dialog or view that you wish to add the propertysheet to in the resource editor.
Now select the Picture tool and add a picture control to the dialog/view. Make it the size you want the propertysheet
to be.
Give the picture the properties of NOT Visible,Type:frame,Color:black. (actually color doesn't matter)
Give it the name IDC_PROPSHEETHOLDER.


Step 3: Implement it!

Here we are going to implement the PropertySheet.
Add a member variable to the view or dialog class. This should be the type of Propsheet you have created.
i.e. if your class is CPropSheet, declare in your view or dialog class (in the public section):

CPropSheet *m_pPropSheet;

Now go to the OnInitialUpdate() of your view, or OnInitDialog() of the dialog
and enter the following code.

//////////////
CWnd *pwndPropSheetHolder = GetDlgItem(IDC_PROPSHEETHOLDER);
m_pPropSheet = new PropSheet(pwndPropSheetHolder);

if( !m_pPropSheet->Create(this,//pwndPropSheetHolder, WS_CHILD|WS_VISIBLE,0) )
{
delete m_pPropSheet;
m_pPropSheet = NULL;
return;
}

//Now we will size it to the proper size, the size of the picture control.
CRect rectPropSheet;
CRect rcSheet;
m_pPropSheet->GetWindowRect(rcSheet);
pwndPropSheetHolder->GetWindowRect(rectPropSheet);
ScreenToClient(rectPropSheet); //now set the holder to the correct size of the property sheet so we //dont cut it off pwndPropSheetHolder->SetWindowPos(NULL,rectPropSheet.left,rectPropSheet.top, rcSheet.Width(),rcSheet.Height(),
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE);
//now move the sheet to the right spot m_pPropSheet->SetWindowPos(NULL,rectPropSheet.left,rectPropSheet.right,rcSheet.Width(),rcSheet.Height(),
SWP_NOZORDER|SWP_NOACTIVATE);
/////////


First, this code gets the placeholder we created and then creates the propertysheet as a child of that placeholder.
Then we size it to the proper size by getting the dimensions of the placeholder and then sizing the propertysheet
to that size. Also, above you may have seen how I commented out pwndPlaceHolder. I have seen many examples that use this, but when I do I encounter a bug everytime. If I click on a control in the propertysheet then switch to another window (like my c: dir) and then come back, it locks up.
One more thing, I suggest adding the property pages in the constructor of the PropertySheet:

PropSheet::PropSheet(CWnd * pParentWnd)
:CPropertySheet("My Pages!!",pParentWnd)
{
AddPage(&m_ppDemog);
AddPage(&m_ppPreCath);
}


That's it!
If anyone encounters any bugs, please contact me.


Page : 1