VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 把图片框的完整内容复制到剪贴板
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:10259, 日期:2001-01-04
把图片框的完整内容复制到剪贴板  

vb本身不允许你将一个form、usercontrol或picturebox上的完整图片复制到剪贴板。如果你使用clipboard.setdata,将只复制被载入这个对象的位图。通过使用api方法,你可以超越这个限制,保证全部内容都被复制,包括你刚刚画上的任何图形。
  开始一个新项目,在窗体上放置一个command和一个picturebox。将picturebox的autoredraw属性设为1。然后添加一个标准模块,把以下代码复制进去。

private type rect
  left as long
  top as long
  right as long
  bottom as long
end type

注释:gdi函数:
private declare function bitblt lib "gdi32" (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
private const srccopy = &hcc0020 注释: (dword) dest = source
注释:创建一个memory dc:
declare function createcompatibledc lib "gdi32" (byval hdc as long) as long
注释:在memory中建立一个位图:
declare function createcompatiblebitmap lib "gdi32" (byval hdc as long, byval nwidth as long, byval nheight as long) as long
注释:把一个gdi对象放入dc,返回原先的那个:
declare function selectobject lib "gdi32" (byval hdc as long, byval hobject as long) as long
注释:删除gdi对象:
declare function deleteobject lib "gdi32" (byval hobject as long) as long

注释:剪贴板函数:
private declare function openclipboard lib "user32" (byval hwnd as long) as long
private declare function closeclipboard lib "user32" () as long
private declare function setclipboarddata lib "user32" (byval wformat as long, byval hmem as long) as long
private declare function emptyclipboard lib "user32" () as long
private const cf_bitmap = 2


public function copyentirepicture(byref objfrom as object) as boolean
dim lhdc as long
dim lhbmp as long
dim lhbmpold as long

  注释:在内存中建立一个指向我们将要复制对象的dc:
  lhdc = createcompatibledc(objfrom.hdc)
  if (lhdc <> 0) then
    注释:建立一张指向将要复制对象的位图:
    lhbmp = createcompatiblebitmap(objfrom.hdc, objfrom.scalewidth \ screen.twipsperpixelx, objfrom.scaleheight \ screen.twipsperpixely)
    if (lhbmp <> 0) then
      注释:把位图选入我们刚才建立的dc中,并贮存原先在那里的老位图:
      lhbmpold = selectobject(lhdc, lhbmp)
      
      注释:把objfrom的内容复制到建立的位图里:
      bitblt lhdc, 0, 0, objfrom.scalewidth \ screen.twipsperpixelx, objfrom.scaleheight \ screen.twipsperpixely, objfrom.hdc, 0, 0, srccopy
      
      注释:恢复dc中的内容:
      selectobject lhdc, lhbmpold
            
      注释:现在把位图装入剪贴板:
      emptyclipboard
      openclipboard 0
      setclipboarddata cf_bitmap, lhbmp
      closeclipboard

      注释:我们在这里不用删除建立的位图——
      注释:它现在属于剪贴板,当剪贴板变化时,windows将为我们删除它。
    end if
    
    注释:清除刚才建立的dc:
    deleteobject lhdc
  end if
end function

  为了试验这个方法,把这些代码加入窗体:

private sub command1_click()
  copyentirepicture picture1
end sub

private sub form_load()
  dim i as long
  注释:在picturebox中画些东西:
  with picture1.font
    .name = "arial"
    .bold = true
    .size = 12
  end with
  for i = 1 to 20
    picture1.forecolor = qbcolor(i mod 15)
    picture1.print "http://www.archtide.com"
  next i
end sub

  窗体加载后,picturebox中将会有一些文本。当你点击command后,picturebox中的全部内容都将会被复制到剪贴板里,你可以把它粘贴到别的程序里,比如画笔、word等等。