wxWidgets에서 단어 인스턴스 조작하기

18127 단어 wxwidgets
 =====文件名:wxWordAutomationMain.cpp=====
/***************************************************************
 * Name:      wxWordAutomationMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    emonkey ()
 * Created:   2012-08-10
 * Copyright: emonkey ()
 * License:
 **************************************************************/

#include "wxWordAutomationMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(wxWordAutomationDialog)
#include <wx/settings.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat
{
    short_f, long_f
};

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(wxWordAutomationDialog)
const long wxWordAutomationDialog::ID_STATICTEXT1 = wxNewId();
const long wxWordAutomationDialog::ID_BUTTON1 = wxNewId();
const long wxWordAutomationDialog::ID_STATICLINE1 = wxNewId();
const long wxWordAutomationDialog::ID_BUTTON2 = wxNewId();
//*)

BEGIN_EVENT_TABLE(wxWordAutomationDialog,wxDialog)
    //(*EventTable(wxWordAutomationDialog)
    //*)
END_EVENT_TABLE()

wxWordAutomationDialog::wxWordAutomationDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(wxWordAutomationDialog)
    Create(parent, id, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Welcome to
wxWidgets"
), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1")); wxFont StaticText1Font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); if ( !StaticText1Font.Ok() ) StaticText1Font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); StaticText1Font.SetPointSize(20); StaticText1->SetFont(StaticText1Font); BoxSizer1->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 10); BoxSizer2 = new wxBoxSizer(wxVERTICAL); Button1 = new wxButton(this, ID_BUTTON1, _("操纵word"), wxDefaultPosition, wxSize(-1,30), 0, wxDefaultValidator, _T("ID_BUTTON1")); Button1->SetDefault(); BoxSizer2->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4); StaticLine1 = new wxStaticLine(this, ID_STATICLINE1, wxDefaultPosition, wxSize(10,-1), wxLI_HORIZONTAL, _T("ID_STATICLINE1")); BoxSizer2->Add(StaticLine1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4); Button2 = new wxButton(this, ID_BUTTON2, _("退出程序"), wxDefaultPosition, wxSize(-1,20), 0, wxDefaultValidator, _T("ID_BUTTON2")); BoxSizer2->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4); BoxSizer1->Add(BoxSizer2, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4); SetSizer(BoxSizer1); BoxSizer1->Fit(this); BoxSizer1->SetSizeHints(this); Center(); Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxWordAutomationDialog::OnAbout); Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxWordAutomationDialog::OnQuit); //*) } wxWordAutomationDialog::~wxWordAutomationDialog() { //(*Destroy(wxWordAutomationDialog) //*) } void wxWordAutomationDialog::OnQuit(wxCommandEvent& event) { Close(); } #include <wx/stdpaths.h> #include <wx/msw/ole/automtn.h> void wxWordAutomationDialog::OnAbout(wxCommandEvent& event) { enum Alignment { c_ALIGN_LEFT = 0, c_ALIGN_CENTER = 1, c_ALIGN_RIGHT = 2, c_ALIGN_JUSTIFY = 3 }; enum PageOrientation { c_PORTRAIT, c_LANDSCAPE }; wxAutomationObject oWord, oDoc; wxStandardPaths opath; wxString filepath; filepath=wxString::Format(wxT("%s\\test.doc"),opath.GetDataDir()); if(!oWord.GetInstance(_("Word.Application"))) { if (!oWord.CreateInstance(_("Word.Application"))) { wxMessageBox(wxT("Can not create word application")); oWord.CallMethod("Word.Close"); return; } } oWord.PutProperty(wxT("Visible"), true); oWord.CallMethod("Documents.Add"); oWord.GetObject(oDoc, "ActiveDocument"); //添加页眉 wxAutomationObject header(oDoc.CallMethod("Sections.Last.Headers.Item",1L).GetVoidPtr());//可取值1,2,3 header.PutProperty("Range.Font.Size", 50); header.CallMethod("Range.InsertAfter","页眉文字"); //页脚添加页码 wxAutomationObject footer(oDoc.CallMethod("Sections.Last.Footers.Item", 3L).GetVoidPtr()); footer.CallMethod("PageNumbers.Add",1);//0左对齐;1居中;2右对齐 // if (!filename.IsEmpty()) // oDoc.PutProperty("Name", filename); //Add Text Paragraph oDoc.CallMethod("Content.InsertAfter","标题文字"); wxAutomationObject paragraph; oDoc.GetObject(paragraph, "Paragraphs.Last"); paragraph.PutProperty("Alignment", long(c_ALIGN_CENTER)); paragraph.PutProperty("SpaceAfter", long(0)); paragraph.PutProperty("SpaceBefore", long(0)); paragraph.PutProperty("Range.Font.Name", _("Arial Black")); paragraph.PutProperty("Range.Font.Size", 20); paragraph.PutProperty("Range.Font.Bold", true); paragraph.PutProperty("Range.Font.Italic", false); wxColour cr = wxColour(120,19,10); paragraph.PutProperty("Range.Font.Color", (long)cr.GetRGB()); //Add Paragraph oDoc.CallMethod("Content.InsertParagraphAfter"); oDoc.GetObject(paragraph, "Paragraphs.Last"); paragraph.PutProperty("Alignment", long(c_ALIGN_CENTER)); paragraph.PutProperty("SpaceAfter", long(0)); paragraph.PutProperty("SpaceBefore", long(0)); paragraph.PutProperty("Range.Font.Name", _("Arial Black")); paragraph.PutProperty("Range.Font.Size", 20); paragraph.PutProperty("Range.Font.Bold", true); paragraph.PutProperty("Range.Font.Italic", false); cr = wxColour(23,83,21); paragraph.PutProperty("Range.Font.Color", (long)cr.GetRGB()); //Add Table wxVariant args[3]; args[0] = oDoc.GetProperty("Paragraphs.Last.Range"); args[1] = 3L; args[2] = 5L; oDoc.CallMethod("Tables.Add", 3, args); wxVariant index = oDoc.GetProperty("Tables.Count"); // wxMessageBox(index.GetString()); wxAutomationObject oTable(oDoc.CallMethod("Tables.Item", index).GetVoidPtr()); oTable.PutProperty("Borders.InsideLineStyle", 4L);// wdLineStyleSingle // oTable.PutProperty("Borders.InsideLineWidth", 1);//不知道为什么设置不了 cr = wxColour(255,0,0); oTable.PutProperty("Borders.InsideColor", (long)cr.GetRGB());//内边框颜色 oTable.PutProperty("Borders.OutsideLineStyle", 9L);// cr = wxColour(20,82,0); oTable.PutProperty("Borders.OutsideColor",(long)cr.GetRGB());//外边框颜色 //Add TableCellText wxAutomationObject cell(oTable.CallMethod("Cell", 2L, 3L).GetVoidPtr()); cell.PutProperty("Range.Paragraphs.Alignment", long(c_ALIGN_CENTER)); cell.PutProperty("Range.Paragraphs.SpaceAfter", long(0)); cell.PutProperty("Range.Paragraphs.SpaceBefore", long(0)); wxString ar_text="测试文字"; cell.CallMethod("Range.InsertAfter", ar_text);//cell.PutProperty("Range.Text",ar_text); cr = wxColour(255,254,152); cell.PutProperty("Shading.BackgroundPatternColor",(long)cr.GetRGB());//单元格背景颜色 cell.PutProperty("Shading.ForegroundPatternColor",(long)cr.GetRGB());//单元格前景颜色 cell.PutProperty("Shading.Texture",30);//单元格背景纹理,注释前景色和背景色就可显示出来 // oDoc.CallMethod("SaveAs", filepath);//save document // oWord.PutProperty("DisplayAlerts", false); // oWord.CallMethod("Quit", false); }

좋은 웹페이지 즐겨찾기