工控编程吧

标题: MFC如何将RTF流拷贝到剪接板 [打印本页]

作者: qq263946146    时间: 2019-7-14 15:38
标题: MFC如何将RTF流拷贝到剪接板
可以通过下面几个函数实现将多个从CRichEditView派生的视的内容拷贝到剪接板上。
void CUIMainWindow::OnEditBothPanes()
{
  // Get RTF streams from pane 0 and pane 1.
  COleDataSource* pDataSource = new COleDataSource;

  CString strPane0;
  CString strPane1;

  CUIView* pView;

  SetActiveView((CView *)m_SplitterWnd.GetPane(0,0));
  pView = CUIView::GetView();
  pView->StreamRead(strPane0);

  SetActiveView((CView *)m_SplitterWnd.GetPane(0,1));
  pView = CUIView::GetView();
  pView->StreamRead(strPane1);

  // we are going to concatenate the two rtf streams together
  // therefore drop the ending paren on the first stream as well
  // as the starting header on the second stream

  CSharedFile file (GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
  CString strTemp;

  // find ending parenthesis and position just before it
  strTemp = strPane0.Left (strPane0.ReverseFind('}'));
  file.Write (strTemp, strTemp.GetLength());

  // drop RTF header information through \fonttbl data because
  // you can not, rather, should not nest RTF with header information
  // notice I break the rules till I finish my RTF reader
  //
  // RTF Version 1.5 Specification defines the following header syntax:
  //    \rtf<charset> \deff? <fonttbl><filetb>?<colortbl>?<stylesheet>?
  //    <listtables>?<revtbl>?
  //
  strTemp = strPane1;
  char *pDest = strstr((LPCTSTR)strTemp, _T("{\\colortbl"));
  int  offset = 0;
  if (pDest != NULL)
  {
    offset = pDest - (LPCTSTR)strTemp;
  }

  // complete rtf stream with a closing parenthesis and write to shared file
  //
  strTemp = strPane1.Mid (offset);
  strTemp += "}";
  file.Write (strTemp, strTemp.GetLength());

  UINT format = ::RegisterClipboardFormat (CF_RTF);
  HGLOBAL hMem = file.Detach();
#if _MFC_VER <= 0x0421
  ::GlobalUnlock(hMem);
#endif
  pDataSource->CacheGlobalData (format, hMem);
  pDataSource->SetClipboard();
}


void CUIMainWindow::OnUpdateEditBothPanes(CCmdUI* pCmdUI)
{
  // if either pane isn't a CUIView class disable button
  // CUIView is a CRichEditView derived class

  CObject* objPane0;
  CObject* objPane1;

  objPane0 = m_SplitterWnd.GetPane(0,0);
  objPane1 = m_SplitterWnd.GetPane(0,1);

  if ( (objPane0->IsKindOf(RUNTIME_CLASS(CUIView))) &&
       (objPane1->IsKindOf(RUNTIME_CLASS(CUIView))) )
    pCmdUI->Enable(1);
  else
    pCmdUI->Enable(0);
}


void CUIView::StreamRead (CString& rtfString)
{
  CSharedFile sf;

  EDITSTREAM es;
  es.dwCookie = (DWORD)&sf;
  es.dwError = 0;
  es.pfnCallback = StreamOutCtrl;

  GetRichEditCtrl().StreamOut(SF_RTF , es);

  DWORD dw = sf.GetLength();
  int nSize = INT_MAX;

  if (dw Write(pbBuff, cb);
  *pcb = cb;

  return 0;
}







欢迎光临 工控编程吧 (https://www.gkbc8.com/) Powered by Discuz! X3.4