110 likes | 267 Views
Dialo g Boxes. Dialog Boxes � Popu p chil d window s create d b y Windows � Used for specia l - purpos e inpu t & output – Principa l I/ O mechanis m i n Windows � Contai n severa l chil d windo w controls � Layou t & wha t i t doe s i s ar e predefined (templat e -- a resource)
E N D
DialogBoxes DialogBoxes �PopupchildwindowscreatedbyWindows �Usedforspecial-purposeinput&output –PrincipalI/OmechanisminWindows �Containseveralchildwindowcontrols �Layout&whatitdoesisarepredefined (template--aresource) �Howitdoesisdeterminedbya"Dialogboxprocedure" �Destroyedimmediatelyafteruse
TypesofDialogBoxes �Modal �Modeless �SystemModal WM_INITDIALOGMessage �StartDialogboxwithcalltoDialogBox(…) –CausesWM_CREATE&WM_INITDIALOGmsgs –WM_INITDIALOGislikeanordinarywindow'sWM_CREATEmessage,butaftercontrolshavebeenceated �Processedbeforewindow(dialogbox)ismadevisible �Goodplacetoputdialogboxinitializationcode �InanMFCCDialog-derivedclass,thismessageactivatesdialogbox’sOnInitDialog()handler
EndDialog(…) �Destroysdialogbox �Returnscontroltofunction(WndProc())thatstartedtheDialogBox() UserInteractionwithDialog BoxControls �WM_COMMANDmessage –LOWORD(wParam)containscontrolID –lParam,wParamcontainmessagedata
ExchangingDatawithaDialogBox �Exchangingdatabetweendialogboxfunctionandapp'sWndProc() �SendMessage()couldbeusedtosendmessagetocontrolinside,BUT: –Needtoknowcontrol'shandle –NotknownsinceWindowscreatesthecontrols –IDsareknown--specifiedinresourcetemplate �UseGetDlgItem()togetcontrol'shandle: –hControl=GetDlgItem(hDlg,controlID); �ThenSendMessage(hControl,Msg,wParam,lParam); DialogBoxesinMFC �MFCDialogboxesarebasedonthe CDialogclass
ImportantMFCCDialogFunctions �DoModal()tostartdialogboxmodally �CDialogprovidesthreeover-rideablefunctionstoinitializeandrespondtoOKandCancelbuttonclicks �OnInitDialog() –HandlerforWM_INITDIALOGmessage �OnOK(),OnCancel() –HandlersforWM_COMMANDmessagesfromOK andCancelbuttons –BothcallCDialog’sEndDialog()functiontodismissthedialogboxandreturncontroltoDoModal() StepsinUsingaModalDialog Box(MFC): �1.Setupthedialogboxtemplateintheresources(.rcfile) –Specifiescontrolsused,theirstyle/layout –Canbeprepared"visually"withVisual Studiodialogboxeditor –Or"manually"withatexteditor �2.CreateaCDialog-basedclass �3.InstantiateaCDialogobject �4.CallitsDoModal()function
UsingModalDialogBoxesinMFC �DialogboxesareencapsulatedbyCDialogclass (derivedfromCWnd) �2.AppderivesitsowndialogboxfromCDialog –e.g.,classCMyDlg:publicCDialog •Constructorshouldspecifythatparentconstructorwillbeused •AlsoIDofDBoxresourcetemplatetobeused(IDD_XXX) –Dialogboxmsghandlingdonew/messagemaps –Dialogboxclassdeclarations(.hfile): •Messagemapandhandlingfunctiondeclarations –Dialogboxclassimplementation(.cppfile): •Messagemapandhandlerfunctiondefinitions –UseClassWizardtogeneratetheCDialog-basedclass •Setsupmsgmapping,constructor&correctDboxresourceID �3.AppinstantiatestheDialogBox: –UsuallydoneinCViewclassinresponseto amainwindowmenuitemselection –CMyDlgdlg; •Createsthedialogbox(notactivatedyet) •Initializationcode,ifany,shouldbeputin CDialog’sOnInitDialog()handlerfunction –InvokedinresponsetoWM_INITDIALOGmessage
�4.ActivatingtheDialogBox –UseCDialog’sDoModal()memberfunction •dlg.DoModal(); –Displaysthedialogbox –Messagesfromdialogboxcontrolsgotodialogboxhandlerfunctions –ContinuesuntildialogboxhasbeenclosedbyuserclickingOKorCancelbuttons •CDialog’sEndDialog()memberfunctioncauses DoModal()toreturn •Cantestreturnvalue –If(dlg.DoModal()==IDOK{//dosomething} •Messageprocessingcontinuesinparentwindow CommunicatingwithDialogBoxControls (exchangingdata) –Getapointertocontrol’sIDw/CWnd::GetDlgItem() –Usepointertosendappropriatemessagestocontrol,e.g.(foralistboxinadialogbox): •CListBox*pCtrl=(CListBox*)GetDlgItem(IDC_CTRL); •pCtrl->SendMessage(WM_GETTEXT,…); •GetDlgItemText(IDC_CTRL,m_string);combinesthesetwo –m_stringwouldbeapulbicvariabletoholdretrievedstring •SetDlgItemText(IDC_CTRL,m_string); –Sendsthestringtothecontrol –OKfornon-Wizard-generatedapps –There’samucheasierwayforWizard-generatedapplications �Method1
�Method2 –UseDDX(DialogDataExchange)mechanism –AutomaticallybuiltintoWizard-generatedApps –DDXsystemmovesdatabetweendialogboxcontrolsandvariablesinCdialog-derivedclass –Occurswhenacallismadeto CWnd::UpdateData(direction); –Booleanparametersetsdirectionofdatamovement •TRUE�fromcontrolstovariables •FALSE�fromvariablestocontrols �MFC’sCDialog::OnInitDialog()calls UpdateData(FALSE)automatically –(Recall,thisiscalledtostartthedialogbox) •SoDatafromprogramvariablesistransferredautomaticallytodialogboxcontrolswhenthedialogboxstarts �MFC’sCDialog::OnOK()calls UpdateData(TRUE) –(Thisiscalledwhenuserclicksthe“OK”buttoninsidethedialogbox) •Sodatafromdialogboxcontrolsistransferredautomaticallytoprogramvariableswhenuserclicksthedialogbox’s“OK”button) •OnOK()thencallsCDialog::EndDialog() –SodialogboxdisappearsandDoModal()returns –ReturnsIDOKorIDCANCELdependingonuseraction –Destructordestroysthedialogbox
AddingaModalDialogBoxtothe SketchingMFCApplication �Willallowtheusertospecifytext tobedisplayedinparentwindow
�CreateanewVisualC++,MFC,SDI application(asusual) �Addthesketchingcode(seeearlierexample) �Addanew“Text”menuitem(ID_TEXT) �Addthenewdialogbox –Project/AddResource/Dialog/New –ChangeIDtoIDD_TEXT –Caption:“EnterText” �Usethedialogboxeditortodragoverastaticandaneditcontrol: –StaticControl:“TextString” –Editcontrol:IDC_TEXTEDIT �CreatethenewDialogClass –Rightclickonanunoccupiedareaofthedialogbox&choose“AddClass”tobringupthe“MFCClassWizard”DialogBox –Classname:“CTextDlg” –Baseclass:“CDialog”
�AddNewClassVariables (andconnecttocontrols): –InClassView,rightclickonCTextDlg& chooseAddvariable •Inresulting“AddmembervariableWizard” –Check“ControlVariable”checkbox –ControlID:IDC_TEXTEDIT –Category:Value –Variabletype:CString –Variablename:m_text �AddhandlercodetonewCView“Text” menuitem –InClassViewselectCView-derivedclass –InPropertiesWizardBox“Events”(lightningbolticon): •ScrolldowntoID_TEXT •AddCommandhandlerOnText() •Edittheresultingcodebyadding: CTextDlgdlg; dlg.DoModal(); pDC=GetDC();//AssumesaCDC*pDCvariable pDC->TextOut(0,0,dlg.m_text,lstrlen(dlg.m_text)); �AttopofCview.cppfileunderneaththeotherincludestatements,add: �#includeTextDlg.h