'Inserts the picture at the current insertion point PublicFunction InsertPicture()Function InsertPicture(RTB As RichTextBox, pic As StdPicture) Dim strRTFall AsString Dim lStart AsLong With RTB .SelText =Chr(&H9D) & .SelText &Chr(&H81) strRTFall = .TextRTF strRTFall =Replace(strRTFall, "\'9d", PictureToRTF(pic)) .TextRTF = strRTFall 'position cursor past new insertion lStart = .Find(Chr(&H81)) strRTFall =Replace(strRTFall, "\'81", "") .TextRTF = strRTFall .SelStart = lStart EndWith End Function
PictureToRTF方法:
PublicFunction PictureToRTF()Function PictureToRTF(pic As StdPicture) AsString Dim hMetaDC AsLong, hMeta AsLong, hPicDC AsLong, hOldBmp AsLong Dim Bmp As BITMAP, Sz As Size, Pt As POINTAPI Dim sTempFile AsString, screenDC AsLong Dim headerStr AsString, retStr AsString, byteStr AsString Dim ByteArr() AsByte, nBytes AsLong Dim fn AsLong, i AsLong, j AsLong sTempFile = App.Path &"\~pic"& ((Rnd*1000000) +1000000) \1&".tmp"'some temprory file IfDir(sTempFile) <>""ThenKill sTempFile 'Create a metafile which is a collection of structures that store a 'picture in a device-independent format. hMetaDC = CreateMetaFile(sTempFile) 'set size of Metafile window SetMapMode hMetaDC, MM_ANISOTROPIC SetWindowOrgEx hMetaDC, 0, 0, Pt GetObject pic.Handle, Len(Bmp), Bmp SetWindowExtEx hMetaDC, Bmp.Width, Bmp.Height, Sz 'save sate for later retrieval SaveDC hMetaDC 'get DC compatible to screen screenDC = GetDC(0) hPicDC = CreateCompatibleDC(screenDC) ReleaseDC 0, screenDC 'set out picture as new DC picture hOldBmp = SelectObject(hPicDC, pic.Handle) 'copy our picture to metafile BitBlt hMetaDC, 0, 0, Bmp.Width, Bmp.Height, hPicDC, 0, 0, vbSrcCopy 'cleanup - close metafile SelectObject hPicDC, hOldBmp DeleteDC hPicDC DeleteObject hOldBmp 'retrieve saved state RestoreDC hMetaDC, True hMeta = CloseMetaFile(hMetaDC) DeleteMetaFile hMeta 'header to string we want to insert headerStr ="{\pict\wmetafile8"& _ "\picw"& pic.Width &"\pich"& pic.Height & _ "\picwgoal"& Bmp.Width * Screen.TwipsPerPixelX & _ "\pichgoal"& Bmp.Height * Screen.TwipsPerPixelY & _ "" 'read metafile from disk into byte array nBytes =FileLen(sTempFile) ReDim ByteArr(1To nBytes) fn =FreeFile() Open sTempFile For Binary Access Read As #fn Get #fn, , ByteArr Close #fn Dim nlines AsLong 'turn each byte into two char hex value i =0 byteStr ="" Do byteStr = byteStr & vbCrLf For j =1To39 i = i +1 If i > nBytes ThenExitFor byteStr = byteStr & Hex00(ByteArr(i)) Next j LoopWhile i < nBytes 'string we will be inserting retStr = headerStr &LCase(byteStr) & vbCrLf &"}" PictureToRTF = retStr 'remove temp metafile Kill sTempFile End Function 'adds leading zero to hex value if needed. PublicFunction Hex00()Function Hex00(icolor AsByte) AsString Hex00 =Right("0"&Hex(icolor), 2) End Function
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix