RTFBitmapDlg.cpp
资源名称:rtfbitmap.zip [点击查看]
上传用户:zhoumin726
上传日期:2007-01-02
资源大小:38k
文件大小:8k
源码类别:
RichEdit
开发平台:
Visual C++
- // RTFBitmapDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "RTFBitmap.h"
- #include <afxodlgs.h>
- #include <afxpriv.h>
- #include "oleimpl2.h"
- #include "Richole.h"
- #include "RTFBitmapDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- LPUNKNOWN AFXAPI _AfxQueryInterface(LPUNKNOWN lpUnknown, REFIID iid)
- {
- ASSERT(lpUnknown != NULL);
- LPUNKNOWN lpW = NULL;
- if (lpUnknown->QueryInterface(iid, (LPLP)&lpW) != S_OK)
- return NULL;
- return lpW;
- }
- #endif
- #define RTF_CTRL_ID WM_USER+101
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CRTFBitmapDlg::InsertPlotBitmap()
- {
- USES_CONVERSION;
- SCODE sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &m_lpLockBytes);
- if (sc != S_OK)
- AfxThrowOleException(sc);
- ASSERT(m_lpLockBytes != NULL);
- sc = ::StgCreateDocfileOnILockBytes(m_lpLockBytes,
- STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
- if (sc != S_OK)
- {
- VERIFY(m_lpLockBytes->Release() == 0);
- m_lpLockBytes = NULL;
- AfxThrowOleException(sc);
- }
- CString szFileName( _T("C:\Dev\RTFBitmap\reportplot.bmp") );
- // attempt to create the object
- sc = ::OleCreateFromFile(CLSID_NULL, T2COLE(szFileName),
- IID_IUnknown, OLERENDER_DRAW, NULL, NULL,
- m_lpStorage, (void **)&m_lpObject);
- if ( sc != S_OK )
- {
- TCHAR * lpMsgBuf;
- ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM, NULL,
- ::GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf, 0, NULL );
- CString msg( lpMsgBuf );
- msg += _T("nnnThe following file, created inn"
- "Simulation->Plot, may be missing duen"
- "to not doing a File->Save Workspace:nn" );
- msg += szFileName;
- AfxMessageBox( msg, MB_OK );
- ::LocalFree( lpMsgBuf );
- return;
- }
- // m_lpObject is currently an IUnknown, convert to IOleObject
- if (m_lpObject != NULL)
- {
- LPUNKNOWN lpUnk = m_lpObject;
- m_lpObject = QUERYINTERFACE(lpUnk, IOleObject);
- lpUnk->Release();
- if (m_lpObject == NULL)
- AfxThrowOleException(E_OUTOFMEMORY);
- }
- // cache the IViewObject interface
- m_lpViewObject = QUERYINTERFACE(m_lpObject, IViewObject2);
- if (m_lpViewObject == NULL)
- return;
- // setup for advises; we assume that OLE cleans them up properly
- LPADVISESINK lpAdviseSink =
- (LPADVISESINK)GetInterface(&IID_IAdviseSink);
- // set up view advise
- VERIFY(m_lpViewObject->SetAdvise(DVASPECT_CONTENT, 0, lpAdviseSink)
- == S_OK);
- // the server shows these in its user-interface
- // (as document title and in File Exit menu)
- m_lpObject->SetHostNames(T2COLE(AfxGetAppName()),
- T2COLE(_T("Test")));
- // all items are "contained" -- this makes our reference to this object
- // weak -- which is needed for links to embedding silent update.
- OleSetContainedObject(m_lpObject, TRUE);
- CHARRANGE cr;
- m_reportCtrl.GetSel( cr );
- cr.cpMin = cr.cpMax -1;
- m_reportCtrl.SetSel( cr );
- REOBJECT reo;
- memset( &reo, 0, sizeof( reo ) );
- reo.cbStruct = sizeof( reo );
- CLSID classID;
- if ( m_lpObject->GetUserClassID( &classID ) != S_OK)
- classID = CLSID_NULL;
- reo.clsid = classID;
- reo.cp = REO_CP_SELECTION;
- reo.poleobj = m_lpObject;
- reo.pstg = m_lpStorage;
- LPOLECLIENTSITE lpClientSite;
- m_reportCtrl.GetIRichEditOle()->GetClientSite( &lpClientSite );
- reo.polesite = lpClientSite;
- SIZEL sizel;
- sizel.cx = sizel.cy = 0; // let richedit determine initial size
- reo.sizel = sizel;
- reo.dvaspect = DVASPECT_CONTENT;
- reo.dwFlags = REO_RESIZABLE;
- reo.dwUser = 0;
- HRESULT hr = m_reportCtrl.GetIRichEditOle()->InsertObject( &reo );
- }
- /////////////////////////////////////////////////////////////////////////////
- // CRTFBitmapDlg dialog
- CRTFBitmapDlg::CRTFBitmapDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CRTFBitmapDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CRTFBitmapDlg)
- // 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 CRTFBitmapDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CRTFBitmapDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CRTFBitmapDlg, CDialog)
- //{{AFX_MSG_MAP(CRTFBitmapDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CRTFBitmapDlg message handlers
- BOOL CRTFBitmapDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 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
- RECT rect;
- GetClientRect( &rect );
- m_reportCtrl.Create( WS_VISIBLE | WS_CHILD | ES_MULTILINE |
- ES_WANTRETURN | WS_HSCROLL | WS_VSCROLL |
- ES_AUTOHSCROLL | ES_AUTOVSCROLL,
- rect, this, RTF_CTRL_ID );
- m_reportCtrl.ReplaceSel( _T("Here is a line of text.nnn") );
- InsertPlotBitmap();
- m_reportCtrl.ReplaceSel( _T("nnHere is another line of text.") );
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CRTFBitmapDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 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 CRTFBitmapDlg::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 CRTFBitmapDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }