Click here to Skip to main content
14,428,725 members

C / C++ / MFC

Questionvc++ tutor Pin
RomTibi2-Aug-06 9:28
MemberRomTibi2-Aug-06 9:28 
QuestionRe: vc++ tutor Pin
David Crow2-Aug-06 10:25
MemberDavid Crow2-Aug-06 10:25 
AnswerRe: vc++ tutor Pin
RomTibi3-Aug-06 6:58
MemberRomTibi3-Aug-06 6:58 
AnswerRe: vc++ tutor Pin
Hamid_RT2-Aug-06 19:31
MemberHamid_RT2-Aug-06 19:31 
GeneralRe: vc++ tutor Pin
RomTibi3-Aug-06 7:01
MemberRomTibi3-Aug-06 7:01 
GeneralRe: vc++ tutor Pin
Hamid_RT3-Aug-06 11:05
MemberHamid_RT3-Aug-06 11:05 
GeneralRe: vc++ tutor Pin
Hamid_RT4-Aug-06 7:49
MemberHamid_RT4-Aug-06 7:49 
AnswerRe: vc++ tutor [modified] Pin
Rilhas4-Aug-06 11:33
MemberRilhas4-Aug-06 11:33 
I think the simplest way to start with visual program building is to create a new project with VC6. VC7 and VC8 complicate matters a little, but the steps are basically the same. The following description is for VC6.

Start by creating a new MFC application (exe) project named "MFCTest" and select a dialog based application. Leave all the defaults if you are unsure what they mean. After VC6 has created your project build it. You should see 0 errors and 0 warnings. Run it and you should see a simple dialog.

Start by going to the resource view and display the "IDD_MFCTEST_DIALOG" dialog. Add an edit bot to it by selecting it in the tool bar ("ab|") and draw it inside the dialog. A box with the word "Edit" appears. Open its properties (RMB->properties or ALT+ENTER) and change the ID from "IDC_EDIT1" to "IDC_NAME". Add another one and change its ID from "IDC_EDIT2" to "IDC_ADDRESS". We will now setup these boxes when the dialog opens.

The definition of the dialog is in the file "MFCTestDlg.cpp". In line 91 you find the function "BOOL CMFCTestDlg::OnInitDialog()" which is the function called during the creation of the dialog and where you may setup items, like values in edit boxes and populate lists. This function starts by setting up the menu and the icons and then has a "TODO:" section where you add your code.

Add the line "CEdit* np=(CEdit*)GetDlgItem(IDC_NAME);" after the "TODO:" to tell the program to find the edit box with the identifier IDC_NAME and put its address in "np". Then add the line "np->SetWindowText("Abracadabra");" to set the text in the edit box. Do the same to the address box by adding the lines "CEdit* ap=(CEdit*)GetDlgItem(IDC_ADDRESS);" and "ap->SetWindowText("Mars");".

Now compile and run the program and you will have the edit controls setup by the time the dialog opens. Now, to get text when the user types it, we have to add a handler to be called when the text changes.

Go to the resource view and select the "IDD_MFCTEST_DIALOG" dialog. Then select the IDC_NAME box. Press CTRL+W (to invoke the wizard) and verify that the project is "MFCTest", the class name is "MFCTestDlg", and that the selected object ID is "IDC_NAME". Then select "EN_CHANGE" in the "messages:" box. The "Add Function" button will become available and you may select it and accept the proposed function name. The "EN_CHANGE" becomes bold, and the "Edit Code" becomes the default button. Click it and you will go to line 177 of "MFCTestDlg.cpp".

The created function "void CMFCTestDlg::OnChangeName()" will be called when the text in the IDC_NAME box changes. For this example we will get whatever text the user types here and put a reversed version in the IDC_ADDRESS box. insert the following lines in the function:

CEdit* np=(CEdit*)GetDlgItem(IDC_NAME);
CEdit* ap=(CEdit*)GetDlgItem(IDC_ADDRESS);
CString s;

First the edit boxes are found. Then a new string is created and filled with the contents of the name edit box. Then we reverse it and put the result in the address box. Note that setting the text in the address box would invoke its EN_CHANGE message, and so if you did the same in the handler for the address (if we had any) as we are doing in the name you would end up with an infinite loop of one handler invoking the other indefinitely.

If you want to add variables to the dialog class you can have the wizard do that for you or you can go to the "MFCTestDlg.h" file and add them yourself. The "main()" equivalent is found in "MFCTest.cpp" - the dialog creation is found in line 57 and it's invocation (open) is done with "int nResponse = dlg.DoModal();". Note that this call only returns when the dialog is closed.

I hope this quick start guide helps you. This is how I started it and works for me. The rest of the variations (like diferent controls and such) you can learn as you go along. MFC functions will always respond to the F1 key for help.


QuestionTemplates / Class Interfaces Pin
jon-802-Aug-06 8:42
professionaljon-802-Aug-06 8:42 
AnswerRe: Templates / Class Interfaces Pin
Zac Howland2-Aug-06 11:46
MemberZac Howland2-Aug-06 11:46 
QuestionConverting hex string to hex number? Pin
Wheatbread2-Aug-06 7:18
MemberWheatbread2-Aug-06 7:18 
AnswerRe: Converting hex string to hex number? Pin
David Crow2-Aug-06 7:19
MemberDavid Crow2-Aug-06 7:19 
GeneralRe: Converting hex string to hex number? Pin
Wheatbread2-Aug-06 7:36
MemberWheatbread2-Aug-06 7:36 
AnswerRe: Converting hex string to hex number? Pin
basementman2-Aug-06 7:38
Memberbasementman2-Aug-06 7:38 
AnswerRe: Converting hex string to hex number? Pin
Zac Howland2-Aug-06 7:54
MemberZac Howland2-Aug-06 7:54 
QuestionAutomated MS Word and VC++ [modified] Pin
georgekjolly2-Aug-06 7:12
Membergeorgekjolly2-Aug-06 7:12 
AnswerRe: Automated MS Word and VC++ Pin
DaTxomin2-Aug-06 14:45
MemberDaTxomin2-Aug-06 14:45 
GeneralRe: Automated MS Word and VC++ Pin
georgekjolly2-Aug-06 18:35
Membergeorgekjolly2-Aug-06 18:35 
QuestionNetUserGetInfo Problems - PLZ Help Pin
Programm3r2-Aug-06 6:21
MemberProgramm3r2-Aug-06 6:21 
QuestionRe: NetUserGetInfo Problems - PLZ Help Pin
David Crow2-Aug-06 6:53
MemberDavid Crow2-Aug-06 6:53 
QuestionSize of an empty class Pin
Sarath C2-Aug-06 5:46
MemberSarath C2-Aug-06 5:46 
AnswerRe: Size of an empty class Pin
Zac Howland2-Aug-06 6:15
MemberZac Howland2-Aug-06 6:15 
GeneralRe: Size of an empty class Pin
Sarath C2-Aug-06 6:24
MemberSarath C2-Aug-06 6:24 
GeneralRe: Size of an empty class Pin
Zac Howland2-Aug-06 6:45
MemberZac Howland2-Aug-06 6:45 
GeneralRe: Size of an empty class Pin
Sarath C2-Aug-06 19:59
MemberSarath C2-Aug-06 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.