technofantasy

博客园 首页 新随笔 联系 订阅 管理
  22 随笔 :: 16 文章 :: 75 评论 :: 39377 阅读
'Inserts the picture at the current insertion point
Public Function InsertPicture(RTB As RichTextBox, pic As StdPicture)
Dim strRTFall As String
Dim lStart As Long
    
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
    
End With
End Function

PictureToRTF方法:
Public Function PictureToRTF(pic As StdPicture) As String
    
Dim hMetaDC As Long, hMeta As Long, hPicDC As Long, hOldBmp As Long
    
Dim Bmp As BITMAP, Sz As Size, Pt As POINTAPI
    
Dim sTempFile As String, screenDC As Long
    
Dim headerStr As String, retStr As String, byteStr As String
    
Dim ByteArr() As Byte, nBytes As Long
    
Dim fn As Long, i As Long, j As Long

    sTempFile 
= App.Path & "\~pic" & ((Rnd * 1000000+ 1000000\ 1 & ".tmp"  'some temprory file
    If Dir(sTempFile) <> "" Then Kill 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, 
00, 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, 00, Bmp.Width, Bmp.Height, hPicDC, 00, 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(1 To nBytes)
    fn 
= FreeFile()
    Open sTempFile 
For Binary Access Read As #fn
    
Get #fn, , ByteArr
    Close #fn
    
Dim nlines As Long
        
    
'turn each byte into two char hex value
    i = 0
    byteStr 
= ""
    
Do
        byteStr 
= byteStr & vbCrLf
        
For j = 1 To 39
            i 
= i + 1
            
If i > nBytes Then Exit For
            byteStr 
= byteStr & Hex00(ByteArr(i))
        
Next j
    
Loop While 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.
Public Function Hex00(icolor As ByteAs String
    Hex00 
= Right("0" & Hex(icolor), 2)
End Function

posted on   陈锐  阅读(2215)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· 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
点击右上角即可分享
微信分享提示