Anyone have an example of placing a bmp in a rich edit control?
Larry
Open paint, open a bitmap, select-all and copy.
Ctrl->V in the rich edit control.
http://www.ionicwind.com/forums/index.php/topic,3264.0.html
Paul.
Is there a way to do it under program control and at a specific location?
No user interaction.
Larry
Not directly. You can put a bitmap on the clipboard and then send the control a WM_PASTE message.
Paul.
Larry,
I have two functions. The First one RicheditInsertImage2 first creates a copy of current clipboard, puts a bitmap to clipboard, sends the WM_PASTE and restores the clipboard. Only text, bitmaps and DIB's are saved/restored.
Second one (RicheditInsertImage1) is the official way, but one time is working, another time hungs somewhere in XP's GDI driver.
Take a lookCONST RICHEDIT_1000 = 1000
DIALOG d1
CREATEDIALOG d1,0,0,300,202,0x80CA0080,0,"Caption",&d1_handler
CONTROL d1,@RICHEDIT,"",12,14,269,163,0x50B01004,RICHEDIT_1000
SHOWDIALOG d1
RicheditInsertImage1(d1, RICHEDIT_1000, L"D:\\EBDev\\examples\\button_bmp_normal.bmp")
'RicheditInsertImage2(d1, RICHEDIT_1000, "D:\\EBDev\\examples\\button_bmp_normal.bmp")
Waituntil IsWindowClosed(d1)
SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
/* Initialize any controls here */
CASE @IDCLOSEWINDOW
CLOSEDIALOG d1,@IDOK
CASE @IDCONTROL
SELECT @CONTROLID
CASE RICHEDIT_1000
/* respond to control notifications here */
ENDSELECT
ENDSELECT
RETURN
ENDSUB
'///////////////////////////
$include "windowssdk.inc"
TYPE MYNODE
UINT format
HANDLE hData
ENDTYPE
sub RicheditInsertImage2(WINDOW win, int id, string pszPath)
' 1. save all current (handled) clipboard data formats
pointer p
HGLOBAL mem
HANDLE hData
pointer list = ListCreate()
OpenClipboard(win.hwnd)
UINT format = EnumClipboardFormats(0)
while (format)
hData = 0
' check if we can make a copy of current clipboard data
select format
case CF_TEXT
case& CF_BITMAP
case& CF_DIB
hData = GetClipboardData(format)
endselect
if (hData)
' backup of clipboard format is implemented
if (format=CF_TEXT)
mem = GlobalAlloc(GMEM_MOVEABLE, len(*<string>hData)+1)
p = GlobalLock(mem)
*<string>p = *<string>hData
GlobalUnlock(mem)
hData = mem
elseif (format=CF_BITMAP)
hData = CopyImage(hData, IMAGE_BITMAP,0,0,0)
elseif (format=CF_DIB)
hData = CopyImage(hData, IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION)
endif
p = ListAdd(list, new(MYNODE, 1))
*<MYNODE>p.format = format
*<MYNODE>p.hData = hData
endif
format = EnumClipboardFormats(format)
endwhile
' 2. put bitmap to the clipboard
EmptyClipboard()
SetClipboardData(CF_BITMAP, LoadImage(pszPath, @IMGBITMAP))
CloseClipboard()
SendMessage(win, WM_PASTE, 0, 0, id)
' 3. restore saved clipboard data
OpenClipboard(win.hwnd)
EmptyClipboard()
for p = each list as MYNODE
SetClipboardData(*p.format, *p.hData)
next
ListRemoveAll(list, TRUE)
CloseClipboard()
return
endsub
$include "richedit.inc"
$include "richole.inc"
$include "olestd.inc"
' http://support.microsoft.com/default.aspx?scid=kb;en-us;220844
' this is the "pro" way, but it may hung
sub RicheditInsertImage1(WINDOW win, int id, wstring wszPath)
IRichEditOle reole
if (SendMessage(win, EM_GETOLEINTERFACE, 0, &reole, id))
ILockBytes lpLockBytes
IUnknown lpUnk
REOBJECT reobj
FORMATETC formatEtc
ZeroMemory(&reobj, len(reobj))
ZeroMemory(&formatEtc, len(formatEtc))
formatEtc.dwAspect = DVASPECT_CONTENT
formatEtc.lindex = -1
reobj.cbStruct = len(reobj)
reobj.cp = REO_CP_SELECTION
reobj.dvaspect = formatEtc.dwAspect
reobj.dwFlags = REO_INVERTEDSELECT
if (!CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes))
if (!StgCreateDocfileOnILockBytes(lpLockBytes, STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &reobj.pstg))
if (!reole->GetClientSite(&reobj.polesite))
if (!OleCreateFromFile(_CLSID_NULL, wszPath, _IID_IUnknown, OLERENDER_DRAW, &formatEtc, reobj.polesite, reobj.pstg, &lpUnk))
if (!lpUnk->QueryInterface(_IID_IOleObject, &reobj.poleobj))
OleSetContainedObject(reobj.poleobj, TRUE)
reobj.poleobj->GetUserClassID(&reobj.clsid)
reole->InsertObject(&reobj)
reobj.poleobj->Release()
endif
lpUnk->Release()
endif
reobj.polesite->Release()
endif
reobj.pstg->Release()
endif
lpLockBytes->Release()
endif
reole->Release()
endif
return
endsub
as always, thanks Sapero, and Paul
Larry