Adding Images (Visual 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.
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.
#define cIMAGE(xmlns) xmlns L"#image"
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 8
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: *Name = SysAllocString(cIMAGE(cNAMESPACE)); break;
In the SmartDocXMLTypeCaption property subroutine, insert the following code.
case 8: *Caption = SysAllocString(L"Images"); break;
In the ControlCount property subroutine, insert the following code. You are adding two images to the task pane for the image element.
else if (wcscmp(XMLTypeName,cIMAGE(cNAMESPACE)) == 0) { *Count =2; }
In the ControlID property subroutine, insert the following code.
else if (wcscmp(XMLTypeName,cIMAGE(cNAMESPACE)) == 0) { *ControlID = ControlIndex + 700; }
In the ControlTypeFromID property subroutine, insert the following code.
case 701: *Type = C_TYPE_IMAGE; break; case 702: *Type = C_TYPE_IMAGE; break;
In the ControlCaptionFromID property subroutine, insert the following code.
case 701: *Caption = SysAllocString(L"Click letter to type text."); break; case 702: *Caption = SysAllocString(L"Click image to insert into document."); break;
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.
CComBSTR bstrContent(m_bstrPath.m_str); switch (ControlID) { case 701: bstrContent.Append(L"alphabet.gif"); break; case 702: bstrContent.Append(L"simplesample.bmp"); break; } bstrContent.CopyTo(ImageSrc); bstrContent.Empty();
In the ImageClick method, insert 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 has clicked.
Range objRange,objNodeRange; XMLNodes objXMLNodes; XMLNode objXMLNode; InlineShapes objInlineShapes; CString szText; // Variables that will be used and re-used in our calls DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; VARIANT vOpt; vOpt.vt = VT_ERROR; vOpt.scode = DISP_E_PARAMNOTFOUND; switch (ControlID) { case 701: if (XCoordinate>=0 && XCoordinate<=16) { if (YCoordinate>=0 && YCoordinate<=20) szText+="A"; if (YCoordinate>=21 && YCoordinate<=40) szText+="G"; if (YCoordinate>=41 && YCoordinate<=60) szText+="M"; if (YCoordinate>=61 && YCoordinate<=80) szText+="S"; } if (XCoordinate>=17 && XCoordinate<=32) { if (YCoordinate>=0 && YCoordinate<=20) szText+="B"; if (YCoordinate>=21 && YCoordinate<=40) szText+="H"; if (YCoordinate>=41 && YCoordinate<=60) szText+="N"; if (YCoordinate>=61 && YCoordinate<=80) szText+="T"; } if (XCoordinate>=33 && XCoordinate<=48) { if (YCoordinate>=0 && YCoordinate<=20) szText+="C"; if (YCoordinate>=21 && YCoordinate<=40) szText+="Y"; if (YCoordinate>=41 && YCoordinate<=60) szText+="O"; if (YCoordinate>=61 && YCoordinate<=80) szText+="U"; if (YCoordinate>=81 && YCoordinate<=100) szText+="Y"; } if (XCoordinate>=49 && XCoordinate<=64) { if (YCoordinate>=0 && YCoordinate<=20) szText+="D"; if (YCoordinate>=21 && YCoordinate<=40) szText+="J"; if (YCoordinate>=41 && YCoordinate<=60) szText+="P"; if (YCoordinate>=61 && YCoordinate<=80) szText+="W"; if (YCoordinate>=81 && YCoordinate<=100) szText+="Z"; } if (XCoordinate>=65 && XCoordinate<=80) { if (YCoordinate>=0 && YCoordinate<=20) szText+="E"; if (YCoordinate>=21 && YCoordinate<=40) szText+="K"; if (YCoordinate>=41 && YCoordinate<=60) szText+="Q"; if (YCoordinate>=61 && YCoordinate<=80) szText+="W"; } if (XCoordinate>=81 && XCoordinate<=96) { if (YCoordinate>=0 && YCoordinate<=20) szText+="F"; if (YCoordinate>=21 && YCoordinate<=40) szText+="L"; if (YCoordinate>=41 && YCoordinate<=60) szText+="R"; if (YCoordinate>=61 && YCoordinate<=80) szText+="X"; } SetXMLNodeText(Target,1,szText,true); break; case 702: objRange.AttachDispatch(Target); objXMLNodes.AttachDispatch(objRange.GetXMLNodes()); objXMLNode.AttachDispatch(objXMLNodes.Item(1)); objNodeRange.AttachDispatch(objXMLNode.GetRange()); objInlineShapes.AttachDispatch(objNodeRange.GetInlineShapes()); CString strFilePath; strFilePath+=m_bstrPath.m_str; strFilePath+="simplesample.bmp"; VARIANT vLinked,vSaved, vRange; vLinked.vt=VT_BOOL; vLinked.boolVal=FALSE; vSaved.vt=VT_BOOL; vSaved.boolVal=TRUE; vRange.vt=VT_DISPATCH; vRange.pdispVal=objNodeRange.m_lpDispatch; objInlineShapes.AddPicture(strFilePath,&vLinked,&vSaved,&vRange); objInlineShapes.DetachDispatch(); objNodeRange.DetachDispatch(); objXMLNode.DetachDispatch(); objXMLNodes.DetachDispatch(); objRange.DetachDispatch(); break; }
Note The XCoordinate and YCoordinate parameters are measured in 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 and y coordinates 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 and y coordinates for the boundaries you specified, you can create your own switch statements to provide actions for these specific areas.
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.