Adding Check Boxes (Visual C++ Tutorial) [Office 2003 SDK Documentation]

Previous  Adding Radio Buttons

The following steps show how to add a check box to the SimpleSample smart document.

  1. The first thing you need to do is add a constant for the checkbox element in the SimpleSample schema. Insert the following code into the general declarations section of your code module, below the existing constants.

    #define cCHECKBOX(xmlns) xmlns L"#checkbox"
    
  2. Next you need to add 1 to the cTYPES constant.  Remove the existing cTYPES constant, and enter the following code or change your code to match.

    #define cTYPES 6
    
  3. Now you are ready to modify the existing code to insert the check boxes.  The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.

    In the SmartDocXMLTypeName property subroutine, insert the following code.

                case 6:
                    *Name = SysAllocString(cCHECKBOX(cNAMESPACE));
                    break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 6:
                    *Caption = SysAllocString(L"Checkboxes");
                    break;
    

    In the ControlCount property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cCHECKBOX(cNAMESPACE)) == 0)
            {           
                *Count = 2;
            }
    

    In the ControlID property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cCHECKBOX(cNAMESPACE)) == 0)
            {           
                *ControlID = ControlIndex + 500;
            }
    

    In the ControlTypeFromID property subroutine, insert the following code. Because you are adding two check boxes to the SimpleSample smart document, you need to add two check box controls.

                case 501:
                    *Type = C_TYPE_CHECKBOX;
                    break;
    
                case 502:
                    *Type = C_TYPE_CHECKBOX;
                    break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.  Again, because you are adding two check boxes, you need two captions.

                case 501:
                    *Caption = SysAllocString(L"Show/Hide paragraph marks.");
                    break;
    
                case 502:
                    *Caption = SysAllocString(L"Show/Hide XML tags.");
                    break;
    
  4. The PopulateCheckbox method specifies the initial state of the check boxes.  For the two check boxes in the SimpleSample smart document, the check boxes are initially selected, as shown in the following code.

            switch(ControlID)
            {
                case 501:
                    *Checked = VARIANT_TRUE;
                    break;
                case 502:
                    *Checked = VARIANT_TRUE;
                    break;
            }
    
  5. The OnCheckboxChange method specifies what happens when a user clicks a check box.  In the SimpleSample smart document, when the user selects or clears one of the check boxes, a Microsoft Word option is turned on or off.

            _Application objApp;
            Range objRange;        
            Window objWindow;
            View objView;
    
            objRange.AttachDispatch(Target);        
            objApp.AttachDispatch(objRange.GetApplication());
            objWindow.AttachDispatch(objApp.GetActiveWindow());
            objView.AttachDispatch(objWindow.GetView());
    
            switch (ControlID)
            {
                case 501:
                    objView.SetShowAll(!objView.GetShowAll());
                    break;
                case 502:
                    objView.SetShowXMLMarkup(!objView.GetShowXMLMarkup());
                    break;
            }            
    
            objView.DetachDispatch();
            objWindow.DetachDispatch();
            objRange.DetachDispatch();
            objApp.DetachDispatch();
    
  6. Recompile your SimpleSample smart document dynamic-link library (DLL), and copy it to the deployment location that you specified earlier. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.

Next  Adding List Boxes and Combo Boxes