设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2388|回复: 1
打印 上一主题 下一主题

[模块/函数] 【转载 / 源码】在Access中复制图片到剪贴板

[复制链接]
跳转到指定楼层
1#
发表于 2005-8-7 22:36:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
<DIV class=quote>

调用段代码:

Private Sub B_CopyToClipboard_Click()

   Dim MyPicCtl As Control

   Set MyPicCtl = Me.Image0 '图片控件为Image0

   Call ClipBoard_SetImage(MyPicCtl)

End Sub

模块段代码:

Option Compare Database

Option Explicit

Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long

Declare Function CloseClipboard Lib "user32" () As Long

Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long

Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

Declare Function EmptyClipboard Lib "user32" () As Long

      

Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As Long) As Long

Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long

Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _

   (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Declare Function SetEnhMetaFileBits Lib "gdi32" _

   (ByVal cbBuffer As Long, lpData As Byte) As Long

Declare Function SetWinMetaFileBits Lib "gdi32" _

   (ByVal cbBuffer As Long, lpbBuffer As Byte, _

    ByVal hDCRef As Long, lpmfp As Any) As Long

Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" _

   (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long

Declare Function DeleteEnhMetaFile Lib "gdi32" (ByVal hemf As Long) As Long

      

Declare Function GetActiveWindow Lib "user32" () As Long

Declare Function GetDesktopWindow Lib "user32" () As Long

' 剪贴板格式

Public Const CF_TEXT = 1

Public Const CF_BITMAP = 2

Public Const CF_METAFILEPICT = 3

Public Const CF_DIB = 8

Public Const CF_ENHMETAFILE = 14

' 全局内存标志

Public Const GMEM_MOVEABLE = &H2

Public Const GMEM_ZEROINIT = &H40

Public Const GMEM_SHARE = &H2000



Public Function ClipBoard_SetImage(MyPicCtl As Control)

     

    Dim hClipMemory     As Long

    Dim lpClipMemory    As Long

    Dim hGlobalMemory   As Long

    Dim lpGlobalMemory  As Long

    Dim cfm             As Long

    Dim hMetafile       As Long

    Dim AccessHwnd      As Long

   

    ' 存储PictureData属性的Byte数组

    Dim bPicData() As Byte

   

    ' 重定义数组大小

    ReDim bPicData(LenB(MyPicCtl.PictureData) - 1)

    ' 复制数组

    bPicData = MyPicCtl.PictureData

   

    If (bPicData(0) <> 3 And bPicData(0) <> 14 And bPicData(0) <> 40) Then

       MsgBox ("不支持此格式。" & Chr$(13) & _

               "ictureData的第一个数据包含:" & bPicData(0))

       Exit Function

    End If

    If (bPicData(0) = 3) Then

       ' *** 头文件 ***

       Call CopyMemory(cfm, bPicData(8), Len(cfm))

       hMetafile = SetWinMetaFileBits(UBound(bPicData) + 24 + 1 - 8, bPicData(24), 0&, cfm)

    Else

       If (bPicData(0) = 14) Then

          ' *** 增强型头文件 ***

          hMetafile = SetEnhMetaFileBits(UBound(bPicData) + 1 - 8, bPicData(8))

       Else

          ' (bPicData(0) = 40)

          ' *** DIB ***

          ' 分配可移动的全局内存

          hGlobalMemory = GlobalAlloc(GMEM_MOVEABLE Or GMEM_SHARE Or GMEM_ZEROINIT, _

                                      UBound(bPicData) + 1)

          If (hGlobalMemory = 0) Then

             MsgBox ("无法分配全局内存")

             Exit Function

          End If

         

          ' 锁定内存块

          lpGlobalMemory = GlobalLock(hGlobalMemory)

          If (lpGlobalMemory = 0) Then

             MsgBox ("无法锁定全局内存")

             GlobalFree (hGlobalMemory)

             Exit Function

          End If

   

          ' 复制数据到全局内存

          Call CopyMemory(ByVal lpGlobalMemory, bPicData(0), UBound(bPicData) + 1)

       End If

    End If

      

    ' 打开剪贴板

    AccessHwnd = GetActiveWindow()

    If (OpenClipboard(AccessHwnd) = 0) Then

       MsgBox "无法打开剪贴板,可能正在被其他程序使用。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-9-11 21:04:00 | 只看该作者
找这个东西,很久了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-29 04:33 , Processed in 0.091559 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表