RTFBitmapDlg.cpp
上传用户:zhoumin726
上传日期:2007-01-02
资源大小:38k
文件大小:8k
源码类别:

RichEdit

开发平台:

Visual C++

  1. // RTFBitmapDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "RTFBitmap.h"
  5. #include <afxodlgs.h>
  6. #include <afxpriv.h>
  7. #include "oleimpl2.h"
  8. #include "Richole.h"
  9. #include "RTFBitmapDlg.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. LPUNKNOWN AFXAPI _AfxQueryInterface(LPUNKNOWN lpUnknown, REFIID iid)
  15. {
  16. ASSERT(lpUnknown != NULL);
  17. LPUNKNOWN lpW = NULL;
  18. if (lpUnknown->QueryInterface(iid, (LPLP)&lpW) != S_OK)
  19. return NULL;
  20. return lpW;
  21. }
  22. #endif
  23. #define RTF_CTRL_ID   WM_USER+101
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26. class CAboutDlg : public CDialog
  27. {
  28. public:
  29. CAboutDlg();
  30. // Dialog Data
  31. //{{AFX_DATA(CAboutDlg)
  32. enum { IDD = IDD_ABOUTBOX };
  33. //}}AFX_DATA
  34. // ClassWizard generated virtual function overrides
  35. //{{AFX_VIRTUAL(CAboutDlg)
  36. protected:
  37. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38. //}}AFX_VIRTUAL
  39. // Implementation
  40. protected:
  41. //{{AFX_MSG(CAboutDlg)
  42. //}}AFX_MSG
  43. DECLARE_MESSAGE_MAP()
  44. };
  45. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  46. {
  47. //{{AFX_DATA_INIT(CAboutDlg)
  48. //}}AFX_DATA_INIT
  49. }
  50. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  51. {
  52. CDialog::DoDataExchange(pDX);
  53. //{{AFX_DATA_MAP(CAboutDlg)
  54. //}}AFX_DATA_MAP
  55. }
  56. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  57. //{{AFX_MSG_MAP(CAboutDlg)
  58. // No message handlers
  59. //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61. void CRTFBitmapDlg::InsertPlotBitmap()
  62. {
  63. USES_CONVERSION;
  64. SCODE sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &m_lpLockBytes);
  65. if (sc != S_OK)
  66. AfxThrowOleException(sc);
  67. ASSERT(m_lpLockBytes != NULL);
  68. sc = ::StgCreateDocfileOnILockBytes(m_lpLockBytes,
  69. STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
  70. if (sc != S_OK)
  71. {
  72. VERIFY(m_lpLockBytes->Release() == 0);
  73. m_lpLockBytes = NULL;
  74. AfxThrowOleException(sc);
  75. }
  76.     CString szFileName( _T("C:\Dev\RTFBitmap\reportplot.bmp") );
  77. // attempt to create the object
  78. sc = ::OleCreateFromFile(CLSID_NULL, T2COLE(szFileName),
  79.  IID_IUnknown, OLERENDER_DRAW, NULL, NULL, 
  80.  m_lpStorage, (void **)&m_lpObject);
  81. if ( sc != S_OK )
  82. {
  83. TCHAR * lpMsgBuf;
  84. ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
  85.  FORMAT_MESSAGE_FROM_SYSTEM, NULL, 
  86.  ::GetLastError(),
  87.  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  88.  (LPTSTR) &lpMsgBuf, 0, NULL );
  89. CString msg( lpMsgBuf );
  90. msg += _T("nnnThe following file, created inn"
  91.   "Simulation->Plot, may be missing duen"
  92.   "to not doing a File->Save Workspace:nn" );
  93. msg += szFileName;
  94. AfxMessageBox( msg, MB_OK );
  95. ::LocalFree( lpMsgBuf );
  96. return;
  97. }
  98. // m_lpObject is currently an IUnknown, convert to IOleObject
  99. if (m_lpObject != NULL)
  100. {
  101. LPUNKNOWN lpUnk = m_lpObject;
  102. m_lpObject = QUERYINTERFACE(lpUnk, IOleObject);
  103. lpUnk->Release();
  104. if (m_lpObject == NULL)
  105. AfxThrowOleException(E_OUTOFMEMORY);
  106. }
  107. // cache the IViewObject interface
  108. m_lpViewObject = QUERYINTERFACE(m_lpObject, IViewObject2);
  109. if (m_lpViewObject == NULL)
  110. return;
  111. // setup for advises; we assume that OLE cleans them up properly
  112. LPADVISESINK lpAdviseSink =
  113. (LPADVISESINK)GetInterface(&IID_IAdviseSink);
  114. // set up view advise
  115. VERIFY(m_lpViewObject->SetAdvise(DVASPECT_CONTENT, 0, lpAdviseSink)
  116. == S_OK);
  117. // the server shows these in its user-interface
  118. //  (as document title and in File Exit menu)
  119. m_lpObject->SetHostNames(T2COLE(AfxGetAppName()),
  120. T2COLE(_T("Test")));
  121. // all items are "contained" -- this makes our reference to this object
  122. //  weak -- which is needed for links to embedding silent update.
  123. OleSetContainedObject(m_lpObject, TRUE);
  124. CHARRANGE cr;
  125. m_reportCtrl.GetSel( cr );
  126. cr.cpMin = cr.cpMax -1;
  127. m_reportCtrl.SetSel( cr );
  128. REOBJECT reo;
  129. memset( &reo, 0, sizeof( reo ) );
  130. reo.cbStruct = sizeof( reo );
  131. CLSID classID;
  132. if ( m_lpObject->GetUserClassID( &classID ) != S_OK)
  133. classID = CLSID_NULL;
  134. reo.clsid = classID;
  135. reo.cp = REO_CP_SELECTION;
  136. reo.poleobj = m_lpObject;
  137. reo.pstg = m_lpStorage;
  138. LPOLECLIENTSITE lpClientSite;
  139. m_reportCtrl.GetIRichEditOle()->GetClientSite( &lpClientSite );
  140. reo.polesite = lpClientSite;
  141. SIZEL sizel;
  142. sizel.cx = sizel.cy = 0; // let richedit determine initial size
  143. reo.sizel = sizel;
  144. reo.dvaspect = DVASPECT_CONTENT;
  145. reo.dwFlags = REO_RESIZABLE;
  146. reo.dwUser = 0;
  147. HRESULT hr = m_reportCtrl.GetIRichEditOle()->InsertObject( &reo );
  148. }
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CRTFBitmapDlg dialog
  151. CRTFBitmapDlg::CRTFBitmapDlg(CWnd* pParent /*=NULL*/)
  152. : CDialog(CRTFBitmapDlg::IDD, pParent)
  153. {
  154. //{{AFX_DATA_INIT(CRTFBitmapDlg)
  155. // NOTE: the ClassWizard will add member initialization here
  156. //}}AFX_DATA_INIT
  157. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  158. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  159. }
  160. void CRTFBitmapDlg::DoDataExchange(CDataExchange* pDX)
  161. {
  162. CDialog::DoDataExchange(pDX);
  163. //{{AFX_DATA_MAP(CRTFBitmapDlg)
  164. // NOTE: the ClassWizard will add DDX and DDV calls here
  165. //}}AFX_DATA_MAP
  166. }
  167. BEGIN_MESSAGE_MAP(CRTFBitmapDlg, CDialog)
  168. //{{AFX_MSG_MAP(CRTFBitmapDlg)
  169. ON_WM_SYSCOMMAND()
  170. ON_WM_PAINT()
  171. ON_WM_QUERYDRAGICON()
  172. //}}AFX_MSG_MAP
  173. END_MESSAGE_MAP()
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CRTFBitmapDlg message handlers
  176. BOOL CRTFBitmapDlg::OnInitDialog()
  177. {
  178. CDialog::OnInitDialog();
  179. // Add "About..." menu item to system menu.
  180. // IDM_ABOUTBOX must be in the system command range.
  181. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  182. ASSERT(IDM_ABOUTBOX < 0xF000);
  183. CMenu* pSysMenu = GetSystemMenu(FALSE);
  184. if (pSysMenu != NULL)
  185. {
  186. CString strAboutMenu;
  187. strAboutMenu.LoadString(IDS_ABOUTBOX);
  188. if (!strAboutMenu.IsEmpty())
  189. {
  190. pSysMenu->AppendMenu(MF_SEPARATOR);
  191. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  192. }
  193. }
  194. // Set the icon for this dialog.  The framework does this automatically
  195. //  when the application's main window is not a dialog
  196. SetIcon(m_hIcon, TRUE); // Set big icon
  197. SetIcon(m_hIcon, FALSE); // Set small icon
  198. RECT rect;
  199. GetClientRect( &rect );
  200. m_reportCtrl.Create( WS_VISIBLE | WS_CHILD | ES_MULTILINE |
  201.  ES_WANTRETURN | WS_HSCROLL | WS_VSCROLL | 
  202.  ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  203.  rect, this, RTF_CTRL_ID );
  204. m_reportCtrl.ReplaceSel( _T("Here is a line of text.nnn") );
  205. InsertPlotBitmap();
  206. m_reportCtrl.ReplaceSel( _T("nnHere is another line of text.") );
  207. return TRUE;  // return TRUE  unless you set the focus to a control
  208. }
  209. void CRTFBitmapDlg::OnSysCommand(UINT nID, LPARAM lParam)
  210. {
  211. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  212. {
  213. CAboutDlg dlgAbout;
  214. dlgAbout.DoModal();
  215. }
  216. else
  217. {
  218. CDialog::OnSysCommand(nID, lParam);
  219. }
  220. }
  221. // If you add a minimize button to your dialog, you will need the code below
  222. //  to draw the icon.  For MFC applications using the document/view model,
  223. //  this is automatically done for you by the framework.
  224. void CRTFBitmapDlg::OnPaint() 
  225. {
  226. if (IsIconic())
  227. {
  228. CPaintDC dc(this); // device context for painting
  229. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  230. // Center icon in client rectangle
  231. int cxIcon = GetSystemMetrics(SM_CXICON);
  232. int cyIcon = GetSystemMetrics(SM_CYICON);
  233. CRect rect;
  234. GetClientRect(&rect);
  235. int x = (rect.Width() - cxIcon + 1) / 2;
  236. int y = (rect.Height() - cyIcon + 1) / 2;
  237. // Draw the icon
  238. dc.DrawIcon(x, y, m_hIcon);
  239. }
  240. else
  241. {
  242. CDialog::OnPaint();
  243. }
  244. }
  245. // The system calls this to obtain the cursor to display while the user drags
  246. //  the minimized window.
  247. HCURSOR CRTFBitmapDlg::OnQueryDragIcon()
  248. {
  249. return (HCURSOR) m_hIcon;
  250. }