1 / 11

Dialog Boxes � Popu p chil d window s create d b y Windows

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)

adolfo
Download Presentation

Dialog Boxes � Popu p chil d window s create d b y Windows

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DialogBoxes DialogBoxes �PopupchildwindowscreatedbyWindows �Usedforspecial-purposeinput&output –PrincipalI/OmechanisminWindows �Containseveralchildwindowcontrols �Layout&whatitdoesisarepredefined (template--aresource) �Howitdoesisdeterminedbya"Dialogboxprocedure" �Destroyedimmediatelyafteruse

  2. 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

  3. EndDialog(…) �Destroysdialogbox �Returnscontroltofunction(WndProc())thatstartedtheDialogBox() UserInteractionwithDialog BoxControls �WM_COMMANDmessage –LOWORD(wParam)containscontrolID –lParam,wParamcontainmessagedata

  4. 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

  5. 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

  6. 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

  7. �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

  8. �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

  9. AddingaModalDialogBoxtothe SketchingMFCApplication �Willallowtheusertospecifytext tobedisplayedinparentwindow

  10. �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”

  11. �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

More Related