Step6Dlg.cpp
上传用户:tjyzx0508
上传日期:2013-02-16
资源大小:254k
文件大小:3k
- // Step6Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Step6.h"
- #include "Step6Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CStep6Dlg dialog
- CStep6Dlg::CStep6Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CStep6Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CStep6Dlg)
- // 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 CStep6Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CStep6Dlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CStep6Dlg, CDialog)
- //{{AFX_MSG_MAP(CStep6Dlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CStep6Dlg message handlers
- BOOL CStep6Dlg::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
-
- 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 CStep6Dlg::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 CStep6Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- #include "msword9.h"
- void CStep6Dlg::OnOK()
- {
- CLSID clsid;
- HRESULT hr;
- hr=::CLSIDFromProgID(L"Word.Application",&clsid); //通过ProgID取得CLSID
- if(FAILED(hr))
- {
- AfxMessageBox(_T("不会吧,竟然没有安装OFFICE"));
- return;
- }
-
- IUnknown *pUnknown=NULL;
- IDispatch *pDispatch=NULL;
- _Application app=NULL;
- Selection sel=NULL;
- hr=::GetActiveObject(clsid,NULL,&pUnknown); //查找是否有WORD程序在运行
- if(FAILED(hr))
- {
- AfxMessageBox(_T("没有正在运行中的WORD应用程序"));
- return;
- }
- try
- {
- hr=pUnknown->QueryInterface(IID_IDispatch,(LPVOID *)&app);
- if(FAILED(hr)) throw(_T("没有取得IDispatchPtr"));
- pUnknown->Release(); pUnknown=NULL;
-
- sel=app.GetSelection();
- if(!sel) throw(_T("没有正在编辑的文档"));
- sel.WholeStory(); //全部选择
- CString str=sel.GetText(); //取得文本
- SetDlgItemText(IDC_EDIT1,str); //显示到编辑窗中
- }
- catch(LPCTSTR lpErr)
- {
- AfxMessageBox(lpErr);
- }
- if(pUnknown) pUnknown->Release();
- if(sel) sel.ReleaseDispatch();
- if(app) sel.ReleaseDispatch();
- }