Share via


Adding Images (C# Tutorial) [Office 2003 SDK Documentation]

Previous  Adding List Boxes and Combo Boxes

The following steps show you how to add a couple of images to the SimpleSample smart document.

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

    const String cIMAGE = cNAMESPACE + "#image";
    
  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.

    const Int32 cTYPES = 8;
    
  3. You also need to add a a global variable to store the text between images clicks. Insert the following code into the general declarations section of your code module. You will use it in the ImageClick method shown below.

    private String strText;
    
  4. Now, you are ready to modify the existing code to insert the image. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.

    In the SmartDocXMLTypeName property subroutine, insert the following code.

                case 8:
                   strTypeName = cIMAGE;
                   break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 8:
                   strTypeCaption = "Image";
                   break;
    

    In the ControlCount property subroutine, insert the following code. You are adding two images to the task pane for the image element.

                case cIMAGE:
                   intNumberOfControls = 2;
                   break;
    

    In the ControlID property subroutine, insert the following code.

                case cIMAGE:
                   intControlID = ControlIndex + 700;
                   break;
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 701:
                   type = C_TYPE.C_TYPE_IMAGE;
                   break;
    
                case 702:
                   type = C_TYPE.C_TYPE_IMAGE;
                   break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

                case 701:
                   strControlCaption = "Click letter to type text.";
                   break;
    
                case 702:
                   strControlCaption = "Click image to insert into document.";
                   break;
    
  5. Use the PopulateImage method to specify the path of the image to display in the Document Actions task pane for the image element. The following code sample designates a path for the ImageSrc parameter.

             switch (ControlID){
                case 701:
                   ImageSrc = strPath + "alphabet.gif";
                   break;
    
                case 702:
                   ImageSrc = strPath + "simplesample.bmp";
                   break;
    
                default:
                   break;
             }
    
  6. In the ImageClick method, place the code that specifies what happens when the user clicks the image. The following code uses the XCoordinate and YCoordinate parameters to determine which letter of the alphabet the user clicked.

             Microsoft.Office.Interop.Word.Range objRange = 
                (Microsoft.Office.Interop.Word.Range)Target;
             String strImage;
    
                    //Temporary object for the optional parameters
                    //of the InsertXML method.
                    object objTemp = System.Reflection.Missing.Value;
    
             switch (ControlID)
             {
                case 701:
                   if (XCoordinate <= 16)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "A";
                      }
                       else if (YCoordinate <= 40){
                          strText = strText + "G";
                      }
                       else if (YCoordinate <= 60){
                          strText = strText + "M";
                      }
                       else if (YCoordinate <= 80){
                          strText = strText + "S";
                      }
                   }
                    else if (XCoordinate <= 32)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "B";
                       }
                       else if (YCoordinate <= 40){
                          strText = strText + "H";
                       }
                       else if (YCoordinate <= 60){
                          strText = strText + "N";
                       }
                       else if (YCoordinate <= 80){
                          strText = strText + "T";
                       }
                   }
                   else if (XCoordinate <= 48)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "C";
                       }
                       else if (YCoordinate <= 40){
                          strText = strText + "I";
                       }
                       else if (YCoordinate <= 60){
                          strText = strText + "O";
                       }
                       else if (YCoordinate <= 80){
                          strText = strText + "U";
                       }
                       else if (YCoordinate <= 100){
                          strText = strText + "Y";
                       }
                   }
                   else if (XCoordinate <= 64)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "D";
                       }
                       else if (YCoordinate <= 40){
                          strText = strText + "J";
                       }
                       else if (YCoordinate <= 60){
                          strText = strText + "P";
                       }
                       else if (YCoordinate <= 80){
                          strText = strText + "V";
                       }
                       else if (YCoordinate <= 100){
                          strText = strText + "Z";
                       }
                   }
                   else if (XCoordinate <= 80)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "E";
                       }
                       else if (YCoordinate <= 40){
                          strText = strText + "K";
                       }
                       else if (YCoordinate <= 60){
                          strText = strText + "Q";
                       }
                       else if (YCoordinate <= 80){
                          strText = strText + "W";
                       }
                   }
                   else if (XCoordinate <= 96)
                   {
                       if (YCoordinate <= 20){
                          strText = strText + "F";
                       }
                       else if (YCoordinate <= 40){
                          strText = strText + "L";
                       }
                       else if (YCoordinate <= 60){
                          strText = strText + "R";
                       }
                       else if (YCoordinate <= 80){
                          strText = strText + "X";
                       }
                   }
    
                   objRange.XMLNodes[1].Range.Text = strText;
                   break;
    
                case 702:
                   strImage = strPath + "simplesample.bmp";
    
                   objRange.XMLNodes[1].Range.InlineShapes.AddPicture(
                      strImage, ref objTemp, ref objTemp, ref objTemp);
                   break;   
             }
    

    Note  The XCoordinate and YCoordinate parameters are measured by using pixels. If you are using graphics for which you don't know the measurements and you need to create sectioned areas that specify different actions when different areas of a graphic are clicked (as shown in the alphabet.gif code example above), you can easily determine the location of the x-coordinate and y-coordinate by using a message box that displays the XCoordinate and YCoordinate parameters and then clicking the boundaries of the sections for which you want to provide specific actions. Then, using the x-coordinate and y-coordinate for the boundaries you specified, you can create your own select case statements to provide actions for these specific areas.

  7. Recompile your SimpleSample smart document 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 Document Fragments