wxwidgets应用手记(8)之报表 把word嵌入到wxwidgets窗口中

先上个图:

未命名

主要的类

   1: #ifndef        _OLEautomation_h_
   2: #define        _OLEautomation_h_
   3:  
   4:  
   5: enum WdWordDialog
   6: {
   7:     wdDialogHelpAbout = 9,
   8:     wdDialogHelpWordPerfectHelp = 10,
   9:     wdDialogDocumentStatistics = 78,
  10:     wdDialogFileNew = 79,
  11:     wdDialogFileOpen = 80,
  12:     wdDialogMailMergeOpenDataSource = 81,
  13:     wdDialogMailMergeOpenHeaderSource = 82,
  14:     wdDialogFileSaveAs = 84,
  15:     wdDialogFileSummaryInfo = 86,
  16:     wdDialogToolsTemplates = 87,
  17:     wdDialogFilePrint = 88,
  18:     wdDialogFilePrintSetup = 97,
  19:     wdDialogFileFind = 99,
  20:     wdDialogFormatAddrFonts = 103,
  21:     wdDialogEditPasteSpecial = 111,
  22:     wdDialogEditFind = 112,
  23:     wdDialogEditReplace = 117,
  24:     wdDialogEditStyle = 120,
  25:     wdDialogEditLinks = 124,
  26:     wdDialogEditObject = 125,
  27:     wdDialogTextToTable = 127,
  28:     wdDialogTableToText = 128,
  29:     wdDialogTableInsertTable = 129,
  30:     wdDialogTableInsertCells = 130,
  31:     wdDialogTableInsertRow = 131,
  32:     wdDialogTableDeleteCells = 133,
  33:     wdDialogTableSplitCells = 137,
  34:     wdDialogTableRowHeight = 142,
  35:     wdDialogTableColumnWidth = 143,
  36:     wdDialogToolsCustomize = 152,
  37:     wdDialogInsertBreak = 159,
  38:     wdDialogInsertSymbol = 162,
  39:     wdDialogInsertPicture = 163,
  40:     wdDialogInsertFile = 164,
  41:     wdDialogInsertDateTime = 165,
  42:     wdDialogInsertField = 166,
  43:     wdDialogInsertMergeField = 167,
  44:     wdDialogInsertBookmark = 168,
  45:     wdDialogMarkIndexEntry = 169,
  46:     wdDialogInsertIndex = 170,
  47:     wdDialogInsertTableOfContents = 171,
  48:     wdDialogInsertObject = 172,
  49:     wdDialogToolsCreateEnvelope = 173,
  50:     wdDialogFormatFont = 174,
  51:     wdDialogFormatParagraph = 175,
  52:     wdDialogFormatSectionLayout = 176,
  53:     wdDialogFormatColumns = 177,
  54:     wdDialogFilePageSetup = 178,
  55:     wdDialogFileDocumentLayout = 178,
  56:     wdDialogFormatTabs = 179,
  57:     wdDialogFormatStyle = 180,
  58:     wdDialogFormatDefineStyleFont = 181,
  59:     wdDialogFormatDefineStylePara = 182,
  60:     wdDialogFormatDefineStyleTabs = 183,
  61:     wdDialogFormatDefineStyleFrame = 184,
  62:     wdDialogFormatDefineStyleBorders = 185,
  63:     wdDialogFormatDefineStyleLang = 186,
  64:     wdDialogFormatPicture = 187,
  65:     wdDialogToolsLanguage = 188,
  66:     wdDialogFormatBordersAndShading = 189,
  67:     wdDialogFormatFrame = 190,
  68:     wdDialogToolsThesaurus = 194,
  69:     wdDialogToolsHyphenation = 195,
  70:     wdDialogToolsBulletsNumbers = 196,
  71:     wdDialogToolsHighlightChanges = 197,
  72:     wdDialogToolsRevisions = 197,
  73:     wdDialogToolsCompareDocuments = 198,
  74:     wdDialogTableSort = 199,
  75:     wdDialogToolsOptionsGeneral = 203,
  76:     wdDialogToolsOptionsView = 204,
  77:     wdDialogToolsAdvancedSettings = 206,
  78:     wdDialogToolsOptionsPrint = 208,
  79:     wdDialogToolsOptionsSave = 209,
  80:     wdDialogToolsOptionsSpellingAndGrammar = 211,
  81:     wdDialogToolsOptionsUserInfo = 213,
  82:     wdDialogToolsMacroRecord = 214,
  83:     wdDialogToolsMacro = 215,
  84:     wdDialogWindowActivate = 220,
  85:     wdDialogFormatRetAddrFonts = 221,
  86:     wdDialogOrganizer = 222,
  87:     wdDialogToolsOptionsEdit = 224,
  88:     wdDialogToolsOptionsFileLocations = 225,
  89:     wdDialogToolsWordCount = 228,
  90:     wdDialogControlRun = 235,
  91:     wdDialogInsertPageNumbers = 294,
  92:     wdDialogFormatPageNumber = 298,
  93:     wdDialogCopyFile = 300,
  94:     wdDialogFormatChangeCase = 322,
  95:     wdDialogUpdateTOC = 331,
  96:     wdDialogInsertDatabase = 341,
  97:     wdDialogTableFormula = 348,
  98:     wdDialogFormFieldOptions = 353,
  99:     wdDialogInsertCaption = 357,
 100:     wdDialogInsertCaptionNumbering = 358,
 101:     wdDialogInsertAutoCaption = 359,
 102:     wdDialogFormFieldHelp = 361,
 103:     wdDialogInsertCrossReference = 367,
 104:     wdDialogInsertFootnote = 370,
 105:     wdDialogNoteOptions = 373,
 106:     wdDialogToolsAutoCorrect = 378,
 107:     wdDialogToolsOptionsTrackChanges = 386,
 108:     wdDialogConvertObject = 392,
 109:     wdDialogInsertAddCaption = 402,
 110:     wdDialogConnect = 420,
 111:     wdDialogToolsCustomizeKeyboard = 432,
 112:     wdDialogToolsCustomizeMenus = 433,
 113:     wdDialogToolsMergeDocuments = 435,
 114:     wdDialogMarkTableOfContentsEntry = 442,
 115:     wdDialogFileMacPageSetupGX = 444,
 116:     wdDialogFilePrintOneCopy = 445,
 117:     wdDialogEditFrame = 458,
 118:     wdDialogMarkCitation = 463,
 119:     wdDialogTableOfContentsOptions = 470,
 120:     wdDialogInsertTableOfAuthorities = 471,
 121:     wdDialogInsertTableOfFigures = 472,
 122:     wdDialogInsertIndexAndTables = 473,
 123:     wdDialogInsertFormField = 483,
 124:     wdDialogFormatDropCap = 488,
 125:     wdDialogToolsCreateLabels = 489,
 126:     wdDialogToolsProtectDocument = 503,
 127:     wdDialogFormatStyleGallery = 505,
 128:     wdDialogToolsAcceptRejectChanges = 506,
 129:     wdDialogHelpWordPerfectHelpOptions = 511,
 130:     wdDialogToolsUnprotectDocument = 521,
 131:     wdDialogToolsOptionsCompatibility = 525,
 132:     wdDialogTableOfCaptionsOptions = 551,
 133:     wdDialogTableAutoFormat = 563,
 134:     wdDialogMailMergeFindRecord = 569,
 135:     wdDialogReviewAfmtRevisions = 570,
 136:     wdDialogViewZoom = 577,
 137:     wdDialogToolsProtectSection = 578,
 138:     wdDialogFontSubstitution = 581,
 139:     wdDialogInsertSubdocument = 583,
 140:     wdDialogNewToolbar = 586,
 141:     wdDialogToolsEnvelopesAndLabels = 607,
 142:     wdDialogFormatCallout = 610,
 143:     wdDialogTableFormatCell = 612,
 144:     wdDialogToolsCustomizeMenuBar = 615,
 145:     wdDialogFileRoutingSlip = 624,
 146:     wdDialogEditTOACategory = 625,
 147:     wdDialogToolsManageFields = 631,
 148:     wdDialogDrawSnapToGrid = 633,
 149:     wdDialogDrawAlign = 634,
 150:     wdDialogMailMergeCreateDataSource = 642,
 151:     wdDialogMailMergeCreateHeaderSource = 643,
 152:     wdDialogMailMerge = 676,
 153:     wdDialogMailMergeCheck = 677,
 154:     wdDialogMailMergeHelper = 680,
 155:     wdDialogMailMergeQueryOptions = 681,
 156:     wdDialogFileMacPageSetup = 685,
 157:     wdDialogListCommands = 723,
 158:     wdDialogEditCreatePublisher = 732,
 159:     wdDialogEditSubscribeTo = 733,
 160:     wdDialogEditPublishOptions = 735,
 161:     wdDialogEditSubscribeOptions = 736,
 162:     wdDialogFileMacCustomPageSetupGX = 737,
 163:     wdDialogToolsOptionsTypography = 739,
 164:     wdDialogToolsAutoCorrectExceptions = 762,
 165:     wdDialogToolsOptionsAutoFormatAsYouType = 778,
 166:     wdDialogMailMergeUseAddressBook = 779,
 167:     wdDialogToolsHangulHanjaConversion = 784,
 168:     wdDialogToolsOptionsFuzzy = 790,
 169:     wdDialogEditGoToOld = 811,
 170:     wdDialogInsertNumber = 812,
 171:     wdDialogLetterWizard = 821,
 172:     wdDialogFormatBulletsAndNumbering = 824,
 173:     wdDialogToolsSpellingAndGrammar = 828,
 174:     wdDialogToolsCreateDirectory = 833,
 175:     wdDialogTableWrapping = 854,
 176:     wdDialogFormatTheme = 855,
 177:     wdDialogTableProperties = 861,
 178:     wdDialogEmailOptions = 863,
 179:     wdDialogCreateAutoText = 872,
 180:     wdDialogToolsAutoSummarize = 874,
 181:     wdDialogToolsGrammarSettings = 885,
 182:     wdDialogEditGoTo = 896,
 183:     wdDialogWebOptions = 898,
 184:     wdDialogToolsAutoManager = 915,
 185:     wdDialogInsertHyperlink = 925,
 186:     wdDialogFileVersions = 945,
 187:     wdDialogToolsOptionsAutoFormat = 959,
 188:     wdDialogFormatDrawingObject = 960,
 189:     wdDialogToolsOptions = 974,
 190:     wdDialogFitText = 983,
 191:     wdDialogEditAutoText = 985,
 192:     wdDialogPhoneticGuide = 986,
 193:     wdDialogToolsDictionary = 989,
 194:     wdDialogFileSaveVersion = 1007,
 195:     wdDialogToolsOptionsBidi = 1029,
 196:     wdDialogFrameSetProperties = 1074,
 197:     wdDialogTableTableOptions = 1080,
 198:     wdDialogTableCellOptions = 1081,
 199:     wdDialogIMESetDefault = 1094,
 200:     wdDialogConsistencyChecker = 1121,
 201:     wdDialogTCSCTranslator = 1156,
 202:     wdDialogHorizontalInVertical = 1160,
 203:     wdDialogTwoLinesInOne = 1161,
 204:     wdDialogFormatEncloseCharacters = 1162,
 205:     wdDialogFormatStylesCustom = 1248,
 206:     wdDialogCSSLinks = 1261,
 207:     wdDialogMailMergeFieldMapping = 1304,
 208:     wdDialogMailMergeInsertAddressBlock = 1305,
 209:     wdDialogMailMergeInsertGreetingLine = 1306,
 210:     wdDialogMailMergeInsertFields = 1307,
 211:     wdDialogMailMergeRecipients = 1308,
 212:     wdDialogInsertWebComponent = 1324,
 213:     wdDialogMailMergeFindRecipient = 1326,
 214:     wdDialogMailMergeSetDocumentType = 1339,
 215:     wdDialogToolsOptionsEditCopyPaste = 1356,
 216:     wdDialogToolsOptionsSecurity = 1361,
 217:     wdDialogSearch = 1363,
 218:     wdDialogShowRepairs = 1381,
 219:     wdDialogToolsOptionsSmartTag = 1395,
 220:     wdDialogSchemaLibrary = 1417,
 221:     wdDialogXMLOptions = 1425,
 222:     wdDialogFormattingRestrictions = 1427,
 223:     wdDialogMyPermission = 1437,
 224:     wdDialogXMLElementAttributes = 1460,
 225:     wdDialogPermission = 1469,
 226:     wdDialogMailMergeInsertAsk = 4047,
 227:     wdDialogMailMergeInsertFillIn = 4048,
 228:     wdDialogMailMergeInsertIf = 4049,
 229:     wdDialogMailMergeInsertNextIf = 4053,
 230:     wdDialogMailMergeInsertSet = 4054,
 231:     wdDialogMailMergeInsertSkipIf = 4055,
 232: };
 233:  
 234: enum WdGoToItem
 235: {
 236:     wdGoToBookmark = -1,
 237:     wdGoToSection = 0,
 238:     wdGoToPage = 1,
 239:     wdGoToTable = 2,
 240:     wdGoToLine = 3,
 241:     wdGoToFootnote = 4,
 242:     wdGoToEndnote = 5,
 243:     wdGoToComment = 6,
 244:     wdGoToField = 7,
 245:     wdGoToGraphic = 8,
 246:     wdGoToObject = 9,
 247:     wdGoToEquation = 10,
 248:     wdGoToHeading = 11,
 249:     wdGoToPercent = 12,
 250:     wdGoToSpellingError = 13,
 251:     wdGoToGrammaticalError = 14,
 252:     wdGoToProofreadingError = 15,
 253: };
 254:  
 255: enum WdGoToDirection
 256: {
 257:     wdGoToLast = -1,
 258:     wdGoToFirst = 1,
 259:     wdGoToAbsolute = 1,
 260:     wdGoToRelative = 2,
 261:     wdGoToNext = 2,
 262:     wdGoToPrevious = 3,
 263: };
 264:  
 265:  
 266: class IWordReport
 267: {
 268: public:
 269:     virtual void Open(const wchar_t* fileName,HWND parent ) = 0;
 270:     virtual void Close() = 0;
 271:     virtual void FillTable( int index,  const wchar_t* data[],int len) = 0;
 272:     virtual void SearchReplace(const wchar_t* Find, const wchar_t* Replacement) = 0;
 273:  
 274:     virtual void CommandBarsEnabled(bool visible) = 0;
 275:     virtual void OnSize()=0;
 276:  
 277:     virtual BOOL PrintPreview()=0;
 278:     virtual void PrintPreview(bool b)=0;
 279:  
 280:     virtual int Dialog(int dlgId)=0;
 281:     virtual void GoTo(int GoToDirection) = 0;
 282: };
 283:  
 284: IWordReport* OLEautomation_CreateWordReport();
 285:  
 286: #endif
   1:  
   2: #include "disphelper.h"
   3: #include <string>
   4: #include "OLEautomation.h"
   5: using namespace std;
   6:  
   7:  
   8: class CWordReport:public IWordReport
   9: {
  10: private:
  11:     CDispPtr wdApp, wdDoc;
  12:     HWND hwnd,parentHwnd;
  13:  
  14: public:
  15:     CWordReport()
  16:     {
  17:         wdApp = NULL;
  18:         wdDoc = NULL;
  19:         hwnd = 0;
  20:         parentHwnd = 0;
  21:     }
  22:  
  23: public:
  24:  
  25:     void Open(const wchar_t* fileName,HWND parent )
  26:     {
  27:         //CDhInitialize init;    /* Initialize DispHelper for this thread. */
  28:         /* Turn on DispHelper exceptions */
  29:         dhToggleExceptions(TRUE);
  30:  
  31:         try
  32:         {
  33:             parentHwnd = parent;
  34:  
  35:             /* Create an instance of 'Word.Application' on the local machine */
  36:             dhCheck( dhCreateObject(L"Word.Application", NULL, &wdApp) );
  37:  
  38:             hwnd = FindWindow( L"Opusapp", NULL);
  39:             if (hwnd != 0)
  40:             {
  41:                 /*
  42:                 [DllImport("user32.dll")]
  43:                 static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);
  44:                 [DllImport("user32.dll")]
  45:                 static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  46:                 [DllImport("user32.dll")]
  47:                 static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
  48:                 const uint SC_MOVE = 0xF010; //移动
  49:                 const uint SC_CLOSE = 0xF060;//关闭
  50:                 const uint MF_BYCOMMAND = 0x00; //按命令方式
  51:                 const uint MF_GRAYED = 0x01;    //灰掉
  52:                 const uint MF_DISABLED = 0x02;  //不可用
  53:                 private void Form1_Load(object sender, EventArgs e)
  54:                 {
  55:                 IntPtr hMenu = GetSystemMenu(this.Handle, false); //获取程序窗体的句柄
  56:                 if (hMenu != IntPtr.Zero)
  57:                 {
  58:                 DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能
  59:                 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能
  60:                 }
  61:                 }
  62: 
  63:                 */
  64:  
  65:                 //去掉系统菜单
  66:                 HMENU hMenu = GetSystemMenu(hwnd, false); //获取程序窗体的句柄
  67:                 if (hMenu != 0)
  68:                 {
  69:                     DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能
  70:                     EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能
  71:                 }
  72:  
  73:                 ::SetParent(hwnd,parent);
  74:  
  75:                 dhCallMethod(wdApp, L".Documents.Open(%S)", fileName);
  76:  
  77:                 /* Set wdDoc = wdApp.Documents.Add */
  78:                 /* '%o' means we wish to receive an IDispatch object in wdDoc */
  79:                 dhCheck( dhGetValue(L"%o", &wdDoc, wdApp, L".ActiveDocument") );
  80:  
  81:                 dhPutValue(wdApp, L".ActiveWindow.DisplayRightRuler=%b",false);
  82:                 dhPutValue(wdApp, L".ActiveWindow.DisplayScreenTips=%b",false);
  83:                 dhPutValue(wdApp, L".ActiveWindow.DisplayVerticalRuler=%b",false);
  84:                 dhPutValue(wdApp, L".ActiveWindow.ActivePane.DisplayRulers=%b",false);
  85:                 dhPutValue(wdApp, L".ActiveWindow.ActivePane.View.Type =%d",3);
  86:  
  87:                 CommandBarsEnabled(false);
  88:  
  89:                 /* wdApp.Visible = True */
  90:                 dhPutValue(wdApp, L".Visible = %b", true);
  91:                 //dhCallMethod(wdApp, L".Activate");
  92:  
  93:                 RECT rect;
  94:                 GetClientRect(parent,&rect);
  95:  
  96:                 SetWindowPos(hwnd,parent,0,0,rect.right - rect.left,rect.bottom-rect.top,SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
  97:                 MoveWindow(hwnd,0,0,rect.right - rect.left,rect.bottom-rect.top,true);
  98:  
  99:                 SetFocus(parent);
 100:             }
 101:         }
 102:         catch (string errstr)
 103:         {
 104:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 105:         }
 106:     }
 107:  
 108:     void Close()
 109:     {
 110:         try
 111:         {
 112:             if (wdDoc != NULL)
 113:             {
 114:                 /* wdApp.ActiveDocument.Saved = True */
 115:                 /* This prevents the user being prompted when word is closed. */
 116:                 dhPutValue(wdDoc, L".Saved = %b", TRUE);
 117:  
 118:                 //dhCallMethod(wdDoc, L".Save");
 119:                 dhCallMethod(wdDoc, L".Close");
 120:  
 121:                 wdDoc = NULL;
 122:             }
 123:             if (wdApp != NULL)
 124:             {
 125:                 /* wdApp.Quit() */
 126:                 dhCallMethod(wdApp, L".Quit");
 127:  
 128:                 wdApp = NULL;
 129:             }
 130:         }
 131:         catch (string errstr)
 132:         {
 133:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 134:         }
 135:         /* Turn off exceptions display for cleanup */
 136:         dhToggleExceptions(FALSE);
 137:     }
 138:  
 139:     void FillTable( int index,  const wchar_t* data[],int len)
 140:     {
 141:         CDispPtr wdTab,wdRow;
 142:  
 143:         try
 144:         {
 145:             dhCheck( dhGetValue(L"%o", &wdTab, wdDoc, L".Tables(%d)",index));
 146:             dhCheck( dhGetValue(L"%o", &wdRow, wdTab, L".Rows(%d)",2));
 147:             //在上面插入一行
 148:             dhCallMethod(wdTab, L".Rows.Add(%o)",wdRow);
 149:  
 150:             for (int i = 0; i <len;i++)
 151:             {
 152:                 dhPutValue(wdTab, L".Cell(%d,%d).Range.Text=%S", 2,i+1,data[i]);
 153:             }
 154:         }
 155:         catch (string errstr)
 156:         {
 157:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 158:         }
 159:     }
 160:  
 161:     void SearchReplace(const wchar_t* Find, const wchar_t* Replacement)
 162:     {
 163:         try
 164:         {
 165:             dhCallMethod(wdApp, L".Selection.Find.ClearFormatting");
 166:             dhCallMethod(wdApp, L".Selection.Find.Replacement.ClearFormatting");
 167:  
 168:             dhPutValue(wdApp, L".Selection.Find.Text = %S",Find);
 169:             dhPutValue(wdApp, L".Selection.Find.Replacement.Text = %S",Replacement);
 170:  
 171:             dhPutValue(wdApp, L".Selection.Find.MatchWholeWord = %b",TRUE);
 172:             dhPutValue(wdApp, L".Selection.Find.Forward =  %b",TRUE);
 173:             dhPutValue(wdApp, L".Selection.Find.Wrap = %d",1);
 174:             dhPutValue(wdApp, L".Selection.Find.Format = %b",FALSE);
 175:  
 176:             dhCallMethod(wdApp, L".Selection.Find.Execute(%m,%m,%m,%m,%m,%m,%m,%m,%m,%m,%d,%m,%m,%m,%m)",2);
 177:         }
 178:         catch (string errstr)
 179:         {
 180:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 181:         }
 182:     }
 183:  
 184:     void CommandBarsEnabled(bool visible)
 185:     {
 186:         try
 187:         {
 188:             int counter = 0;
 189:             dhCheck( dhGetValue(L"%d", &counter, wdApp, L".ActiveWindow.Application.CommandBars.Count") );
 190:             for (int i = 0;i< counter;i++)
 191:             {
 192:                 dhPutValue(wdApp, L".ActiveWindow.Application.CommandBars(%d).Enabled = %b",i+1,visible);
 193:             }
 194:         }
 195:         catch (string errstr)
 196:         {
 197:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 198:         }
 199:     }
 200:  
 201:     void OnSize()
 202:     {
 203:         RECT rect;
 204:         GetClientRect(parentHwnd,&rect);
 205:  
 206:         MoveWindow(hwnd,0,0,rect.right - rect.left,rect.bottom-rect.top,true);
 207:     }
 208:  
 209:     BOOL PrintPreview()
 210:     {
 211:         try
 212:         {
 213:             BOOL ret;
 214:             dhCheck( dhGetValue(L"%b", &ret, wdApp, L".PrintPreview"));
 215:             return ret;
 216:         }
 217:         catch (string errstr)
 218:         {
 219:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 220:         }
 221:     }
 222:  
 223:     void PrintPreview(bool b)
 224:     {
 225:         try
 226:         {
 227:             dhPutValue(wdApp, L".PrintPreview =  %b",b);
 228:         }
 229:         catch (string errstr)
 230:         {
 231:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 232:         }
 233:     }
 234:  
 235:     int Dialog(int dlgId)
 236:     {
 237:         try
 238:         {
 239:             CDispPtr dlg;
 240:             int timeOut = 0,ret;
 241:  
 242:             dhCheck( dhGetValue(L"%o", &dlg, wdApp, L".Dialogs(%d)",dlgId));
 243:  
 244:             /*
 245: 
 246:             http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.dialog.show.aspx
 247: 
 248:             Show方法 显示执行;
 249:             Display方法 只显示不执行
 250: 
 251:             Return value            Description
 252:             -2                        The Close button.
 253:             -1                        The OK button.
 254:             0 (zero)                The Cancel button.
 255:             > 0 (zero)                A command button: 1 is the first button, 2 is the second button, and so on.
 256: 
 257:             */
 258:  
 259:             dhCheck( dhGetValue(L"%d", &ret, dlg, L".Show(%d)",timeOut));
 260:             return ret;            
 261:         }
 262:         catch (string errstr)
 263:         {
 264:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 265:         }
 266:     }
 267:  
 268:     void GoTo(int GoToDirection)
 269:     {
 270:         try
 271:         {
 272:             int WdGoToItem = wdGoToPage;
 273:             // winWordControl1.wd.Selection.GoTo(ref what, ref  which, ref  missing, ref  missing);
 274:             dhCallMethod(wdApp, L".Selection.GoTo(%d,%d,%m,%m)",WdGoToItem,GoToDirection);
 275:         }
 276:         catch (string errstr)
 277:         {
 278:             MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
 279:         }
 280:     }
 281:  
 282:  
 283: };
 284:  
 285: IWordReport* OLEautomation_CreateWordReport()
 286: {
 287:     return new CWordReport();
 288: }

几个重要的API

hwnd = FindWindow( L"Opusapp", NULL);//找Word窗体

//去掉系统菜单
HMENU hMenu = GetSystemMenu(hwnd, false); //获取程序窗体的句柄
if (hMenu != 0)
{
    DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能
    EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能
}

::SetParent(hwnd,parent);//设置父窗体

//设置位置和大小

RECT rect;
GetClientRect(parentHwnd,&rect);

SetWindowPos(hwnd,parent,0,0,rect.right - rect.left,rect.bottom-rect.top,SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
MoveWindow(hwnd,0,0,rect.right - rect.left,rect.bottom-rect.top,true);

调用word里面的内建对话框

参考http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.dialog.show.aspx

注意3个函数的区别:

int Show( ref Object TimeOut ) ;//显示窗体,执行动作

int Display( ref Object TimeOut );//只显示窗体

void Execute();//执行动作

我的理解 Show=Display+Execute

调用代码C#

   1: Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFilePrint];
   2: dlg.Show();
   3: //or
   4: if (dlg.Display() == -1)//-1 = The OK button.
   5: {
   6:     dlg.Execute();
   7: }

C++

   1: int Dialog(int dlgId)
   2: {
   3:     try
   4:     {
   5:         CDispPtr dlg;
   6:         int timeOut = 0,ret;
   7:  
   8:         dhCheck( dhGetValue(L"%o", &dlg, wdApp, L".Dialogs(%d)",dlgId));
   9:  
  10:         /*
  11: 
  12:         http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.dialog.show.aspx
  13: 
  14:         Show方法 显示执行;
  15:         Display方法 只显示不执行
  16: 
  17:         Return value            Description
  18:         -2                        The Close button.
  19:         -1                        The OK button.
  20:         0 (zero)                The Cancel button.
  21:         > 0 (zero)                A command button: 1 is the first button, 2 is the second button, and so on.
  22: 
  23:         */
  24:  
  25:         dhCheck( dhGetValue(L"%d", &ret, dlg, L".Show(%d)",timeOut));
  26:         return ret;            
  27:     }
  28:     catch (string errstr)
  29:     {
  30:         MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"OLEautomation", MB_OK);
  31:     }
  32: }

其中 int dlgId 是下面之一:

   1: enum WdWordDialog
   2: {
   3:     wdDialogHelpAbout = 9,
   4:     wdDialogHelpWordPerfectHelp = 10,
   5:     wdDialogDocumentStatistics = 78,
   6:     wdDialogFileNew = 79,
   7:     wdDialogFileOpen = 80,
   8:     wdDialogMailMergeOpenDataSource = 81,
   9:     wdDialogMailMergeOpenHeaderSource = 82,
  10:     wdDialogFileSaveAs = 84,
  11:     wdDialogFileSummaryInfo = 86,
  12:     wdDialogToolsTemplates = 87,
  13:     wdDialogFilePrint = 88,
  14:     wdDialogFilePrintSetup = 97,
  15:     wdDialogFileFind = 99,
  16:     wdDialogFormatAddrFonts = 103,
  17:     wdDialogEditPasteSpecial = 111,
  18:     wdDialogEditFind = 112,
  19:     wdDialogEditReplace = 117,
  20:     wdDialogEditStyle = 120,
  21:     wdDialogEditLinks = 124,
  22:     wdDialogEditObject = 125,
  23:     wdDialogTextToTable = 127,
  24:     wdDialogTableToText = 128,
  25:     wdDialogTableInsertTable = 129,
  26:     wdDialogTableInsertCells = 130,
  27:     wdDialogTableInsertRow = 131,
  28:     wdDialogTableDeleteCells = 133,
  29:     wdDialogTableSplitCells = 137,
  30:     wdDialogTableRowHeight = 142,
  31:     wdDialogTableColumnWidth = 143,
  32:     wdDialogToolsCustomize = 152,
  33:     wdDialogInsertBreak = 159,
  34:     wdDialogInsertSymbol = 162,
  35:     wdDialogInsertPicture = 163,
  36:     wdDialogInsertFile = 164,
  37:     wdDialogInsertDateTime = 165,
  38:     wdDialogInsertField = 166,
  39:     wdDialogInsertMergeField = 167,
  40:     wdDialogInsertBookmark = 168,
  41:     wdDialogMarkIndexEntry = 169,
  42:     wdDialogInsertIndex = 170,
  43:     wdDialogInsertTableOfContents = 171,
  44:     wdDialogInsertObject = 172,
  45:     wdDialogToolsCreateEnvelope = 173,
  46:     wdDialogFormatFont = 174,
  47:     wdDialogFormatParagraph = 175,
  48:     wdDialogFormatSectionLayout = 176,
  49:     wdDialogFormatColumns = 177,
  50:     wdDialogFilePageSetup = 178,
  51:     wdDialogFileDocumentLayout = 178,
  52:     wdDialogFormatTabs = 179,
  53:     wdDialogFormatStyle = 180,
  54:     wdDialogFormatDefineStyleFont = 181,
  55:     wdDialogFormatDefineStylePara = 182,
  56:     wdDialogFormatDefineStyleTabs = 183,
  57:     wdDialogFormatDefineStyleFrame = 184,
  58:     wdDialogFormatDefineStyleBorders = 185,
  59:     wdDialogFormatDefineStyleLang = 186,
  60:     wdDialogFormatPicture = 187,
  61:     wdDialogToolsLanguage = 188,
  62:     wdDialogFormatBordersAndShading = 189,
  63:     wdDialogFormatFrame = 190,
  64:     wdDialogToolsThesaurus = 194,
  65:     wdDialogToolsHyphenation = 195,
  66:     wdDialogToolsBulletsNumbers = 196,
  67:     wdDialogToolsHighlightChanges = 197,
  68:     wdDialogToolsRevisions = 197,
  69:     wdDialogToolsCompareDocuments = 198,
  70:     wdDialogTableSort = 199,
  71:     wdDialogToolsOptionsGeneral = 203,
  72:     wdDialogToolsOptionsView = 204,
  73:     wdDialogToolsAdvancedSettings = 206,
  74:     wdDialogToolsOptionsPrint = 208,
  75:     wdDialogToolsOptionsSave = 209,
  76:     wdDialogToolsOptionsSpellingAndGrammar = 211,
  77:     wdDialogToolsOptionsUserInfo = 213,
  78:     wdDialogToolsMacroRecord = 214,
  79:     wdDialogToolsMacro = 215,
  80:     wdDialogWindowActivate = 220,
  81:     wdDialogFormatRetAddrFonts = 221,
  82:     wdDialogOrganizer = 222,
  83:     wdDialogToolsOptionsEdit = 224,
  84:     wdDialogToolsOptionsFileLocations = 225,
  85:     wdDialogToolsWordCount = 228,
  86:     wdDialogControlRun = 235,
  87:     wdDialogInsertPageNumbers = 294,
  88:     wdDialogFormatPageNumber = 298,
  89:     wdDialogCopyFile = 300,
  90:     wdDialogFormatChangeCase = 322,
  91:     wdDialogUpdateTOC = 331,
  92:     wdDialogInsertDatabase = 341,
  93:     wdDialogTableFormula = 348,
  94:     wdDialogFormFieldOptions = 353,
  95:     wdDialogInsertCaption = 357,
  96:     wdDialogInsertCaptionNumbering = 358,
  97:     wdDialogInsertAutoCaption = 359,
  98:     wdDialogFormFieldHelp = 361,
  99:     wdDialogInsertCrossReference = 367,
 100:     wdDialogInsertFootnote = 370,
 101:     wdDialogNoteOptions = 373,
 102:     wdDialogToolsAutoCorrect = 378,
 103:     wdDialogToolsOptionsTrackChanges = 386,
 104:     wdDialogConvertObject = 392,
 105:     wdDialogInsertAddCaption = 402,
 106:     wdDialogConnect = 420,
 107:     wdDialogToolsCustomizeKeyboard = 432,
 108:     wdDialogToolsCustomizeMenus = 433,
 109:     wdDialogToolsMergeDocuments = 435,
 110:     wdDialogMarkTableOfContentsEntry = 442,
 111:     wdDialogFileMacPageSetupGX = 444,
 112:     wdDialogFilePrintOneCopy = 445,
 113:     wdDialogEditFrame = 458,
 114:     wdDialogMarkCitation = 463,
 115:     wdDialogTableOfContentsOptions = 470,
 116:     wdDialogInsertTableOfAuthorities = 471,
 117:     wdDialogInsertTableOfFigures = 472,
 118:     wdDialogInsertIndexAndTables = 473,
 119:     wdDialogInsertFormField = 483,
 120:     wdDialogFormatDropCap = 488,
 121:     wdDialogToolsCreateLabels = 489,
 122:     wdDialogToolsProtectDocument = 503,
 123:     wdDialogFormatStyleGallery = 505,
 124:     wdDialogToolsAcceptRejectChanges = 506,
 125:     wdDialogHelpWordPerfectHelpOptions = 511,
 126:     wdDialogToolsUnprotectDocument = 521,
 127:     wdDialogToolsOptionsCompatibility = 525,
 128:     wdDialogTableOfCaptionsOptions = 551,
 129:     wdDialogTableAutoFormat = 563,
 130:     wdDialogMailMergeFindRecord = 569,
 131:     wdDialogReviewAfmtRevisions = 570,
 132:     wdDialogViewZoom = 577,
 133:     wdDialogToolsProtectSection = 578,
 134:     wdDialogFontSubstitution = 581,
 135:     wdDialogInsertSubdocument = 583,
 136:     wdDialogNewToolbar = 586,
 137:     wdDialogToolsEnvelopesAndLabels = 607,
 138:     wdDialogFormatCallout = 610,
 139:     wdDialogTableFormatCell = 612,
 140:     wdDialogToolsCustomizeMenuBar = 615,
 141:     wdDialogFileRoutingSlip = 624,
 142:     wdDialogEditTOACategory = 625,
 143:     wdDialogToolsManageFields = 631,
 144:     wdDialogDrawSnapToGrid = 633,
 145:     wdDialogDrawAlign = 634,
 146:     wdDialogMailMergeCreateDataSource = 642,
 147:     wdDialogMailMergeCreateHeaderSource = 643,
 148:     wdDialogMailMerge = 676,
 149:     wdDialogMailMergeCheck = 677,
 150:     wdDialogMailMergeHelper = 680,
 151:     wdDialogMailMergeQueryOptions = 681,
 152:     wdDialogFileMacPageSetup = 685,
 153:     wdDialogListCommands = 723,
 154:     wdDialogEditCreatePublisher = 732,
 155:     wdDialogEditSubscribeTo = 733,
 156:     wdDialogEditPublishOptions = 735,
 157:     wdDialogEditSubscribeOptions = 736,
 158:     wdDialogFileMacCustomPageSetupGX = 737,
 159:     wdDialogToolsOptionsTypography = 739,
 160:     wdDialogToolsAutoCorrectExceptions = 762,
 161:     wdDialogToolsOptionsAutoFormatAsYouType = 778,
 162:     wdDialogMailMergeUseAddressBook = 779,
 163:     wdDialogToolsHangulHanjaConversion = 784,
 164:     wdDialogToolsOptionsFuzzy = 790,
 165:     wdDialogEditGoToOld = 811,
 166:     wdDialogInsertNumber = 812,
 167:     wdDialogLetterWizard = 821,
 168:     wdDialogFormatBulletsAndNumbering = 824,
 169:     wdDialogToolsSpellingAndGrammar = 828,
 170:     wdDialogToolsCreateDirectory = 833,
 171:     wdDialogTableWrapping = 854,
 172:     wdDialogFormatTheme = 855,
 173:     wdDialogTableProperties = 861,
 174:     wdDialogEmailOptions = 863,
 175:     wdDialogCreateAutoText = 872,
 176:     wdDialogToolsAutoSummarize = 874,
 177:     wdDialogToolsGrammarSettings = 885,
 178:     wdDialogEditGoTo = 896,
 179:     wdDialogWebOptions = 898,
 180:     wdDialogToolsAutoManager = 915,
 181:     wdDialogInsertHyperlink = 925,
 182:     wdDialogFileVersions = 945,
 183:     wdDialogToolsOptionsAutoFormat = 959,
 184:     wdDialogFormatDrawingObject = 960,
 185:     wdDialogToolsOptions = 974,
 186:     wdDialogFitText = 983,
 187:     wdDialogEditAutoText = 985,
 188:     wdDialogPhoneticGuide = 986,
 189:     wdDialogToolsDictionary = 989,
 190:     wdDialogFileSaveVersion = 1007,
 191:     wdDialogToolsOptionsBidi = 1029,
 192:     wdDialogFrameSetProperties = 1074,
 193:     wdDialogTableTableOptions = 1080,
 194:     wdDialogTableCellOptions = 1081,
 195:     wdDialogIMESetDefault = 1094,
 196:     wdDialogConsistencyChecker = 1121,
 197:     wdDialogTCSCTranslator = 1156,
 198:     wdDialogHorizontalInVertical = 1160,
 199:     wdDialogTwoLinesInOne = 1161,
 200:     wdDialogFormatEncloseCharacters = 1162,
 201:     wdDialogFormatStylesCustom = 1248,
 202:     wdDialogCSSLinks = 1261,
 203:     wdDialogMailMergeFieldMapping = 1304,
 204:     wdDialogMailMergeInsertAddressBlock = 1305,
 205:     wdDialogMailMergeInsertGreetingLine = 1306,
 206:     wdDialogMailMergeInsertFields = 1307,
 207:     wdDialogMailMergeRecipients = 1308,
 208:     wdDialogInsertWebComponent = 1324,
 209:     wdDialogMailMergeFindRecipient = 1326,
 210:     wdDialogMailMergeSetDocumentType = 1339,
 211:     wdDialogToolsOptionsEditCopyPaste = 1356,
 212:     wdDialogToolsOptionsSecurity = 1361,
 213:     wdDialogSearch = 1363,
 214:     wdDialogShowRepairs = 1381,
 215:     wdDialogToolsOptionsSmartTag = 1395,
 216:     wdDialogSchemaLibrary = 1417,
 217:     wdDialogXMLOptions = 1425,
 218:     wdDialogFormattingRestrictions = 1427,
 219:     wdDialogMyPermission = 1437,
 220:     wdDialogXMLElementAttributes = 1460,
 221:     wdDialogPermission = 1469,
 222:     wdDialogMailMergeInsertAsk = 4047,
 223:     wdDialogMailMergeInsertFillIn = 4048,
 224:     wdDialogMailMergeInsertIf = 4049,
 225:     wdDialogMailMergeInsertNextIf = 4053,
 226:     wdDialogMailMergeInsertSet = 4054,
 227:     wdDialogMailMergeInsertSkipIf = 4055,
 228: };

转到第几页由void GoTo(int GoToDirection)来实现

代码下载 wxWidgets_VCTest_20120126

转载于:https://www.cnblogs.com/tiany008/archive/2012/01/26/2329855.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值