[ Pobierz całość w formacie PDF ]
.Listing 6-4 shows the code you ll need to change.Notice that we use theCOleControl class functions to make the required change.The SetText() function allows usto change the caption of the button.Every time the user inserts this control, the caption of Button will appear.Listing 6-4void CPButton1Ctrl::OnResetState(){// Resets defaults found in DoPropExchangeCOleControl::OnResetState(); //Modify the control appearance.COleControl::SetText("Button");}Now that we have a method for exchanging information and we ve set the control up the waywe want it to look, it s time to implement the three custom properties that we created.Everytime you create a custom property, you ll need to define some code to make that property dosomething.Otherwise, it ll just sit there and do nothing at all.Listing 6-5 shows the codeyou ll need to add to implement the ModalResult, OnOff, and StdButtonType properties.Note that I ve removed some of the repeated code that you ll find on the source code CD.I llexplain the inner workings of this code in the next section.For right now, all you need toknow is that it implements the properties we created.Listing 6-5void CPButton1Ctrl::OnModalResultChanged(void){AFX_MANAGE_STATE(AfxGetStaticModuleState());//Set the modified flag.SetModifiedFlag();}void CPButton1Ctrl::OnStdButtonTypeChanged(void){AFX_MANAGE_STATE(AfxGetStaticModuleState());// Change the modal result and button// caption to match the user selection.switch (m_StdButtonType){case 0:m_ModalResult = mrNone;COleControl::SetText("Button");break;//// Cases 1 through 8 on source code CD.//case 9:m_ModalResult = mrOff;COleControl::SetText("Off"); }// Set the OnOff property to false// since the user selected another type.m_OnOff = FALSE;//Set the modified flag.SetModifiedFlag();}void CPButton1Ctrl::OnOnOffChanged(void){AFX_MANAGE_STATE(AfxGetStaticModuleState());// If the programmer set the OnOff property true,// take appropriate action.if (m_OnOff){COleControl::SetText("On"); //Change the caption.m_SetOn = TRUE; //Set an internal caption flag.m_ModalResult = mrOn; //Set the modal result value.}else{COleControl::SetText("Button"); //Restore default caption.m_SetOn = FALSE; //Turn our caption flag off.m_ModalResult = mrNone; //Use the default modalresult.}//Set the modified flag.SetModifiedFlag();}At this point, the user can change properties.However, what happens when a user clicks thebutton? If he or she is using one of the standard button types, the OnOff control will return amodal result value.However, the OnOff control also has a special behavior.If you set theOnOff property to True, the button should switch between on and off as the user clicks it.Weneed to add some special event code to handle this situation.Click the Overrides button inthe Properties window and you ll see a list of events you can override.Click the down arrowin the field next to the OnClick entry and choose OnClick from the list.Visual C++ willadd a new method for you.Now it s time to add some code to the OnClick() function.Click the Edit Code button, andVisual C++ will take you to the new function.Listing 6-6 shows the code you ll need to add.Listing 6-6void CPButton1Ctrl::OnClick(USHORT iButton){// See if the OnOff flag is set.If so, change// the caption and internal caption flag.The effect// you should see from this code is a toggling of the// caption text.if (m_OnOff){if (m_SetOn){COleControl::SetText("Off");m_SetOn = FALSE;m_ModalResult = mrOff;}else{COleControl::SetText("On");m_SetOn = TRUE;m_ModalResult = mrOn;}}// Call the default OnClick processing.COleControl::OnClick(iButton);}Using a VARIANT_BOOL type for the OnOff property enables the user to see a nice drop-down list box containing True and False.Wouldn t it be nice if you could also provide a drop-down list box for ModalResult and StdButtonType? To make this feature work, you need toadd the two enumerations shown in Listing 6-7.Add them to both the beginning of the IDLfile (right beneath the #include statements) and to the Public area of the class definition inthe PButton1Ctrl.h file.Listing 6-7// Define the valid ModalResult property values.typedef enum ModalType{mrNone = -1L,mrOK = 1L,mrCancel = 2L,mrAbort = 3L,mrRetry = 4L,mrIgnore = 5L,mrYes = 6L,mrNo = 7L,mrOn = 8L,mrOff = 9L,}MODALTYPE;// Define the valid StdButtonType property values.typedef enum StdButton{None = 0L,OK = 1L,Cancel = 2L,Abort = 3L,Retry = 4L,Ignore = 5L,Yes = 6L,No = 7L,On = 8L,Off = 9L,}STDBUTTON;Adding the enumerations helps, but you also need to change the two properties in the IDLfile.Note that the type of the properties remains long.All we ve done is assign anenumeration to the property so that it provides help to the user.Here are the small, butimportant, changes you need to make to the two properties to enable the drop-down listboxes.[id(1), helpstring("property ModalResult")] ModalType ModalResult;[id(2), helpstring("property StdButtonType")] StdButtonStdButtonType;The final coding item is a special variable.You ll notice in the code that I keep referring to anm_SetOn member variable, but this variable isn t part of the class right now.All you need todo is add it as a type BOOL to the Protected area of the PButton1Ctrl.h file.Breaking the Code into PiecesLet s start taking this code apart.The first function that you modified is DoPropExchange().This function performs only one service in this example it enables you to make yourcustom properties persistent.Essentially, the PX_ series of function calls stores the value ofa particular property from one session to the next.There s one function call for each variabletype that you define.Each one of them accepts four variables like this:PX_Bool(pPX, "OnOff", m_onOff, FALSE);The first variable is a pointer to a property exchange structure
[ Pobierz całość w formacie PDF ]