Transcript Document
Classic Controls Trần Anh Tuấn A Week 1 How to create a MFC project in VS 6.0 Introduction to Classic Controls Initial Step : New a project with specific ProjectName , Location , and kind of Project File > New Step 1 : Based on your purpose.This step will ask users type of Application (MDI , SDI , Dialog Based Here are some example of MDI, SDI , Dialog Based Step 2 & 3 : Some feature and support for user to include. (Recommend for default) Step 4 : Confirm all information about class which will be created like : Class Name , Header File (.h) , Implementation File (.cpp) and Base Class When you finish, all your specifications will be showed. Now we glance at something in our project In Workspace Window we have three tabs to view project components Class View : List all Classes and corresponding Functions that are present in our Project. You can insert new Class into your project by Right Mouse and New Class There are three class type. MFC Class (a new class derived from any MFC Classes) , Generic Class ( any new classes from user view) Form Class (a new class derived from Dialog Class) Resource View : List all resources that we use in our project. Here are some kinds of Resource Dialog : resource for users to create another dialogs Icon : insert or draw an icon which you like String Table : allow user to define a long string by an ID For example: we define a long description with an IDS_STRING1 like : Menu : add one or more Menu for your application Toolbar : Add and Design a your new toolbar. Accelerator : Define a hot key for your function. Such as Ctrl+X : Close Window Bitmap : Import a bitmap or draw directly by Draw Toolbar File View: Seperate all components by files including Source File (cpp) , Header File (.h) , Resource File ( files resulting from Resource View). Note : when you insert a new class into project. Always two files (.h and .cpp) will be added Class Winzards Window : Click View Class Winzard and we see Message Maps : a map between ObjectID with its Event. And when you map a coressponding function will be created For example : We want to Push Button OK (remember ID of OK button is IDOK) and MessageBox will show with : “Now the window will close” Step 1: Class Winzards Step 2: Map IDOK with event BN_CLICKED and click OK A Confirmation Form will ask you want to change function name or not The function will be generated in Class Dialog and you input your code below comment // TODO: ...... From left to right : Compile (Ctrl + F7) , Build (F7) , Stop Build (Ctrl + Break ), Execute (Ctrl + F5) , Go (F5) , Insert Breakpoint (F9) Member Variables Allow user to add variable as a control variable or value variable of control Control variable : use to do function of a control Value variable of control : use to change value on the control. (Learn more in other Lesson ) Batch Build : to create release the project (with exe file and all classes ) Click Build Batch Build and see Classic Controls Windows makes the classic controls available to the application programs that it hosts by registering six predefined WNDCLASSes. The control types, their WNDCLASSes, and the corresponding MFC classes are shown in the following table. To have a control on a dialog , there are two ways : 1.Create by Code , 2. Drag and Drop from Controls Toolbar. Way 1 : Create by Code Step 1 – Create one pointer to a specific Control in Dialog Class (.h) Step 2 – Contruct an object pointer by new operation Step 3 – Define an ID for the object that will be create. (with ID you can do anything with the object ) Click View > Resource Symbols and see In this example IDC_BUTTON will be ID for the object we will create. Note : Remember each ID will have an integer value to distinguish , so input a new value that have not defined before in the range of integer. Step 4 – Define all properties of the object (usually put in InitDialog function) More Step : Although Button is Showed in dialog. But it not has any effects so you must do more. Example: Change label of the button when click button. Add WM_COMMAND into Dialog and check wParam by ID of the object Way 2 : Drag and Drop From Control Bar In tab Resource , Open main Dialog and see Example : Step 1 : Drag and Drop Button Object and put in the desired position Step 2 : In property , we change ID , Caption and Others Close Property Sheet and Save all then IDC_BUTTON1 will be automatically created. Step 3 : In ClassWinzards , Map Event BN_CLICKED and Object IDC_BUTTON1 to create function OnButton. Step 4 : Create Control Varible for button object Some of common commands for classic controls. Static Text : Example : We have control variable : m_ctltext and value String variable m_vartext To change the caption of label : Updatedata(TRUE); m_vartext = “Hello VN”; Updatedata(FALSE); Or m_ctltext. SetWindowText(“Hello VN”); Push Button Example : To do something when click button we map ID with Event to create function To change caption of button we do : SetDlgItemText ( IDC_BUTTON_EXIT, ”Thoat”) Edit Box Example : To set value for edit box we create value variable m_varedit and Updatedata(TRUE) m_edit = 10; Updatedata(FALSE); To do something when user input edit box. We map with event EN_CHANGE Radio Button Example : Step 1 : Drag and Drop radio button into dialog in order In Property of Radio1 and Radio4 we check property Group And Now we have two group of Radio Button. Group1 : Radio1,2,3 Group2 : Radio4,5,6 Create value variabe for object Radio1 and Radio4. - Now m_varsel1 is the value for group radio 1 ,2 ,3 - m_varsel4 is the value for group radio 4 ,5 ,6 Which radio is checked ? Check Box : Example : When check a checkbox the message BN_CLICKED will send.So we map object with this message With a control variable of checkbox. To see whether checkbox is checked or not : m_English.GetCheck() 1 : checked 0 : not checked Group Box : Only used to group objects which have the same functions Combo Box : Example : To know which item is chosen. Create a Cstring variable m_varCombo. UpdateDate(TRUE); MessageBox(varCombo); List Box : Example : To create a list of student First , we create an object ListBox Second , Create a control value m_ctlList for that object Third, Use AddString to add item into ListBox m_ctldssv.AddString(str); And Last , To check which Item is chosen int ctlvitri = m_dssv.GetCurSel(); End Of Week 1