Step5Dlg.cpp
上传用户:tjyzx0508
上传日期:2013-02-16
资源大小:254k
文件大小:5k
- // Step5Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Step5.h"
- #include "Step5Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CStep5Dlg dialog
- CStep5Dlg::CStep5Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CStep5Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CStep5Dlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CStep5Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CStep5Dlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CStep5Dlg, CDialog)
- //{{AFX_MSG_MAP(CStep5Dlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CStep5Dlg message handlers
- BOOL CStep5Dlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- SetDlgItemText(IDC_EDIT1,_T("中华人民共和国"));
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CStep5Dlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CStep5Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- /***************** WORD 中录制的宏,按笔画排序(全部选择,菜单表格排序) ********
- MsgBox wdSortFieldStroke '5 使用了常量,所以使用MsgBox得到具体的数值
- MsgBox wdSortOrderAscending '0
- MsgBox wdSortFieldSyllable '3
- MsgBox wdSeparateByTabs '1
- MsgBox wdSimplifiedChinese '2052
-
- Selection.WholeStory '全选
- Selection.Sort ExcludeHeader:=False, FieldNumber:="段落数", SortFieldType:= _
- wdSortFieldStroke, SortOrder:=wdSortOrderAscending, FieldNumber2:="", _
- SortFieldType2:=wdSortFieldSyllable, SortOrder2:=wdSortOrderAscending, _
- FieldNumber3:="", SortFieldType3:=wdSortFieldSyllable, SortOrder3:= _
- wdSortOrderAscending, Separator:=wdSortSeparateByTabs, SortColumn:=False, _
- CaseSensitive:=False, LanguageID:=wdSimplifiedChinese
- Selection.Copy '把排序后的结果,复制到剪贴板
- *********************************************************************************/
- #include "msword9.h"
- #include <AtlBase.h>
- void CStep5Dlg::OnOK()
- {
- CString str;
- GetDlgItemText(IDC_EDIT1,str);
- str.TrimLeft(); str.TrimRight();
- if(str.IsEmpty()) return;
- ::CoInitialize(NULL);
- _Application app;
- app.CreateDispatch(_T("Word.Application"));
- //app.SetVisible(FALSE); //这次不调用显示,因为我们要偷偷摸摸的转换:)
- Documents docs=app.GetDocuments();
- CComVariant Template(""),NewTemplate(false),DocumentType(0),Visible;
- docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
- Selection sel=app.GetSelection();
- for(int i=0;i<str.GetLength()/2;i++)
- { //这里只考虑了输入为纯汉字的情况,你自己修改为可以支持中英文混合的情况
- sel.TypeText(str.Mid(i*2,2)+"rn"); //2个字符表示一个汉字,用回车换行分隔
- }
- sel.WholeStory(); //全部选择
- CComVariant ExcludeHeader(false);
- CComVariant FieldNumber(_T("段落数")),SortFieldType(5),SortOrder(0);
- CComVariant FieldNumber2(_T("")),SortFieldType2(3),SortOrder2(0);
- CComVariant FieldNumber3(_T("")),SortFieldtype3(3),SortOrder3(0);
- CComVariant SortColumn(false),Separator(1),LanguageID(2052);
- CComVariant CaseSensitive(false),BidiSort,IgnoreThe;
- CComVariant IgnoreKashida,IgnoreDiacritics,IgnoreHe;
- //排序
- sel.Sort(&ExcludeHeader,&FieldNumber,&SortFieldType,&SortOrder,
- &FieldNumber2,&SortFieldType2,&SortOrder2,&FieldNumber3,
- &SortFieldtype3,&SortOrder3,&SortColumn,&Separator,
- &CaseSensitive,&BidiSort,&IgnoreThe,&IgnoreKashida,
- &IgnoreDiacritics,&IgnoreHe,&LanguageID);
-
- //其实,这里可以直接调用sel.GetText()取得文本。
- //但现在选择复制到剪贴板的方式。
- sel.Copy(); //复制到剪贴板
- if(OpenClipboard())
- { //从剪贴板取出排序后的文字
- HGLOBAL hMem=::GetClipboardData(CF_TEXT);
- LPCTSTR lp=(LPCTSTR)::GlobalLock(hMem);
- str=lp;
- ::GlobalUnlock(hMem);
- CloseClipboard();
- str.Replace("rn",""); //删除回车换行
- SetDlgItemText(IDC_EDIT2,str);
- }
- sel.ReleaseDispatch();
- docs.ReleaseDispatch();
- CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
- app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
- app.ReleaseDispatch();
- ::CoUninitialize();
- }