접기
IDD_RICHTEXT_DIALOG DIALOGEX 0, 0, 457, 374
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "RichText"
FONT 9, "굴림"
BEGIN
CONTROL "",IDC_RICHEDIT1,"RICHEDIT",ES_MULTILINE |
ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN |
WS_BORDER | WS_VSCROLL | WS_TABSTOP,7,7,443,328
PUSHBUTTON "Button1",IDC_BUTTON1,391,344,50,14
END
BOOL CRichTextApp::InitInstance()
{
// {
AfxInitRichEdit ();
// }
AfxEnableControlContainer();
...
}
#include <Richole.h>
class CRichTextDlg : public CDialog
{
// {
BOOL CreateFromFile(LPCTSTR lpszFileName);
LPRICHEDITOLE m_pRichEditOle;
LPOLEOBJECT m_lpObject;
LPSTORAGE m_lpStorage;
LPOLECLIENTSITE m_lpClientSite;
// }
// Construction
public:
CRichTextDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CRichTextDlg)
enum { IDD = IDD_RICHTEXT_DIALOG };
CRichEditCtrl m_ctlRichText;
//}}AFX_DATA
...
// Generated message map functions
//{{AFX_MSG(CRichTextDlg)
...
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnDestroy();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CRichTextDlg::CRichTextDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRichTextDlg::IDD, pParent)
{
...
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// {
m_pRichEditOle = NULL;
m_lpObject = NULL;
m_lpStorage = NULL;
m_lpClientSite = NULL;
// }
}
...
void CRichTextDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRichTextDlg)
DDX_Control(pDX, IDC_RICHEDIT1, m_ctlRichText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRichTextDlg, CDialog)
//{{AFX_MSG_MAP(CRichTextDlg)
...
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
BOOL CRichTextDlg::OnInitDialog()
{
...
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// {
::SendMessage(m_ctlRichText.m_hWnd, EM_GETOLEINTERFACE , 0, (LPARAM)&m_pRichEditOle);
ASSERT(m_pRichEditOle != NULL);
// }
return TRUE; // return TRUE unless you set the focus to a control
}
...
void CRichTextDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (m_pRichEditOle)
{
m_pRichEditOle->Release();
m_pRichEditOle = NULL;
}
}
BOOL CRichTextDlg::CreateFromFile(LPCTSTR lpszFileName)
{
USES_CONVERSION;
ASSERT_VALID(this);
ASSERT(m_lpObject == NULL); // one time only
ASSERT(m_lpStorage == NULL);
ASSERT(m_lpClientSite == NULL);
LPLOCKBYTES lpLockBytes = NULL;
CLSID clsid = CLSID_NULL;
OLERENDER render = OLERENDER_DRAW;
CLIPFORMAT cfFormat = 0;
LPFORMATETC lpFormatEtc = NULL;
SCODE sc;
sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
if (sc != S_OK)
AfxThrowOleException(sc);
ASSERT(lpLockBytes != NULL);
sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
if (sc != S_OK)
{
VERIFY(lpLockBytes->Release() == 0);
lpLockBytes = NULL;
AfxThrowOleException(sc);
}
ASSERT(m_lpStorage != NULL);
// fill in FORMATETC struct
FORMATETC formatEtc;
lpFormatEtc = &formatEtc;
lpFormatEtc->cfFormat = cfFormat;
lpFormatEtc->ptd = NULL;
lpFormatEtc->dwAspect = DVASPECT_CONTENT;
lpFormatEtc->lindex = -1;
lpFormatEtc->tymed = TYMED_NULL;
// attempt to create the object
m_pRichEditOle->GetClientSite(&m_lpClientSite);
sc = ::OleCreateFromFile (clsid, T2COLE(lpszFileName),
IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite, m_lpStorage,
(void**)&m_lpObject);
if (sc != S_OK)
AfxThrowOleException(sc);
// m_lpObject is currently an IUnknown, convert to IOleObject
if (m_lpObject != NULL)
{
LPUNKNOWN lpUnk = m_lpObject;
lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
lpUnk->Release();
if (m_lpObject == NULL)
AfxThrowOleException(E_OUTOFMEMORY);
}
// 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);
ASSERT_VALID(this);
return TRUE;
}
void CRichTextDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CreateFromFile("...");
REOBJECT reobject;
ZeroMemory(&reobject, sizeof(REOBJECT));
reobject.cbStruct = sizeof(REOBJECT);
CLSID clsid;
SCODE sc = m_lpObject->GetUserClassID (&clsid);
if (sc != S_OK)
AfxThrowOleException(sc);
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_RESIZABLE | REO_BELOWBASELINE;
reobject.dwUser = 0;
reobject.poleobj = m_lpObject;
ASSERT(m_lpClientSite != NULL);
reobject.polesite = m_lpClientSite;
ASSERT(m_lpStorage != NULL);
reobject.pstg = m_lpStorage;
SIZEL sizel;
sizel.cx = sizel.cy = 0;
reobject.sizel = sizel;
HWND hWndRT = m_ctlRichText.m_hWnd;
::SendMessage(hWndRT, EM_SETSEL , 0, -1);
DWORD dwStart, dwEnd;
::SendMessage(hWndRT, EM_GETSEL , (WPARAM)&dwStart, (LPARAM)&dwEnd);
::SendMessage(hWndRT, EM_SETSEL , dwEnd+1, dwEnd+1);
CString strCr = "\r\n";
m_ctlRichText. ReplaceSel (strCr,TRUE);
m_pRichEditOle->InsertObject(&reobject);
::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
if (m_lpObject)
{
m_lpObject->Release();
m_lpObject = NULL;
}
if (m_lpStorage)
{
m_lpStorage->Release();
m_lpStorage = NULL;
}
if (m_lpClientSite)
{
m_lpClientSite->Release();
m_lpClientSite = NULL;
}
}
접기