类似 MSN 信息发送框的制作(上)
作者:北方工业大学 阙荣文 (querw)
下载源代码
一、引言
用 MSN 和 QQ 等聊天的时候,当用户输入特定意义的字符串时,系统回自动用一张小图片替代.比如输入" : ) "系统
会用一个小笑脸代替。我要实现的就是这样一个信息输入框 。这个信息输入框由两部分组成:图案选择器和多功能文本框。本篇介绍多功能文本框。
二、原理简介
1、主要功能用CRichEditCtrl实现,像设置字体,设置字体颜色,字号等等CRichEditCtrl都提供了很完善的支持,我就不一一赘述了。
CRichEditCtrl 主要的不足在于以下几个方面:
我扩展了CRichEditCtrl类CRichEditCtrlEx实现了上述功能.参考了很多网上的文章,对所有公开源码的开发人员表示崇高的敬意!!
2、实现右键菜单:
///生成右键菜单
void CRichEditCtrlEx::OnRButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default//设置为焦点SetFocus();//创建一个弹出式菜单CMenu popmenu;popmenu.CreatePopupMenu();//添加菜单项目popmenu.AppendMenu(0, ID_RICH_UNDO, "&Undo");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_CUT, "&Cut");popmenu.AppendMenu(0, ID_RICH_COPY, "C&opy");popmenu.AppendMenu(0, ID_RICH_PASTE, "&Paste");popmenu.AppendMenu(0, ID_RICH_CLEAR, "C&lear");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_SELECTALL, "Select &All");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_SETFONT, "Select &Font");//初始化菜单项UINT nUndo=(CanUndo() ? 0 : MF_GRAYED );popmenu.EnableMenuItem(ID_RICH_UNDO, MF_BYCOMMAND|nUndo);UINT nSel=((GetSelectionType()!=SEL_EMPTY) ? 0 : MF_GRAYED) ;popmenu.EnableMenuItem(ID_RICH_CUT, MF_BYCOMMAND|nSel);popmenu.EnableMenuItem(ID_RICH_COPY, MF_BYCOMMAND|nSel);popmenu.EnableMenuItem(ID_RICH_CLEAR, MF_BYCOMMAND|nSel);UINT nPaste=(CanPaste() ? 0 : MF_GRAYED) ;popmenu.EnableMenuItem(ID_RICH_PASTE, MF_BYCOMMAND|nPaste);//显示菜单CPoint pt;GetCursorPos(&pt);popmenu.TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this);popmenu.DestroyMenu();CRichEditCtrl::OnRButtonDown(nFlags, point);CRichEditCtrl::OnRButtonUp(nFlags, point);}3、关于如何把图片插入到RichEdit中,国外由很多文章介绍,都是(我看到的都是)通过插入OLE对象来实现.主要用两个函数,还涉及到了和多接口的调用。
(1)从文件创建OLE对象OleCreateFromFile();
void CRichEditCtrlEx::InsertBitmap(CString szFileName){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);}// attempt to create the objectsc = ::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 IOleObjectif (m_lpObject != NULL){LPUNKNOWN lpUnk = m_lpObject;m_lpObject = QUERYI