如何获得桌面指定位置的背景,放到窗体上?比如,桌面是 1024*768,我要从 X、Y 获得,大小为 W、H。然后绘到我的窗体上,性能最快的方式是什么?请给出代码,不要就给几个 API ,偶要看代码的 -_-

解决方案 »

  1.   

    Option Explicit'²¶×½ÆÁÄ»µÄͼÏñPrivate Type PALETTEENTRY
       peRed As Byte
       peGreen As Byte
       peBlue As Byte
       peFlags As Byte
    End TypePrivate Type LOGPALETTE
       palVersion As Integer
       palNumEntries As Integer
       palPalEntry(255) As PALETTEENTRY
    End TypePrivate Type GUID
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(7) As Byte
    End TypePrivate Const RASTERCAPS As Long = 38
    Private Const RC_PALETTE As Long = &H100
    Private Const SIZEPALETTE As Long = 104Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End TypePrivate Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
    Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Type PicBmp
       Size As Long
       Type As Long
       hBmp As Long
       hPal As Long
       Reserved As Long
    End TypePrivate Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPictureDisp) As Long'²¶×½Í¼Ïó
    Public Function CaptureScreen(x As Long, y As Long, w As Long, h As Long) As Picture
        Dim hWndScreen As Long    '»ñµÃ×ÀÃæµÄ´°¿Ú¾ä±ú
        hWndScreen = GetDesktopWindow()
        Set CaptureScreen = CaptureWindow(hWndScreen, False, x, y, w, h)
    End FunctionPublic Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal _
        LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc _
        As Long) As Picture    Dim hDCMemory As Long
        Dim hBmp As Long
        Dim hBmpPrev As Long
        Dim r As Long
        Dim hDCSrc As Long
        Dim hPal As Long
        Dim hPalPrev As Long
        Dim RasterCapsScrn As Long
        Dim HasPaletteScrn As Long
        Dim PaletteSizeScrn As Long
        Dim LogPal As LOGPALETTE    If Client Then
            hDCSrc = GetDC(hWndSrc)
        Else
            hDCSrc = GetWindowDC(hWndSrc)
        End If    hDCMemory = CreateCompatibleDC(hDCSrc)
        hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
        hBmpPrev = SelectObject(hDCMemory, hBmp)    '»ñµÃÆÁÄ»ÊôÐÔ
        RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
        HasPaletteScrn = RasterCapsScrn And RC_PALETTE
        PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)    'Èç¹ûÆÁÄ»¶ÔÏóÓе÷É«°åÔò»ñµÃÆÁÄ»µ÷É«°å
        If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            '½¨Á¢ÆÁÄ»µ÷É«°åµÄ¿½±´
            LogPal.palVersion = &H300
            LogPal.palNumEntries = 256
            r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
            hPal = CreatePalette(LogPal)
            '½«Ð½¨Á¢µÄµ÷É«°åÑ¡È罨Á¢µÄÄÚ´æ»æͼ¾ä±úÖÐ
            hPalPrev = SelectPalette(hDCMemory, hPal, 0)
            r = RealizePalette(hDCMemory)
        End If    '¿½±´Í¼Ïó
        r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)    hBmp = SelectObject(hDCMemory, hBmpPrev)    If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            hPal = SelectPalette(hDCMemory, hPalPrev, 0)
        End If    'ÊÍ·Å×ÊÔ´
        r = DeleteDC(hDCMemory)
        r = ReleaseDC(hWndSrc, hDCSrc)    Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    End Function
    Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
      Dim r As Long   Dim Pic As PicBmp
       Dim IPic As IPicture
       Dim IID_IDispatch As GUID   'Ìî³äIDispatch½çÃæ
       With IID_IDispatch
          .Data1 = &H20400
          .Data4(0) = &HC0
          .Data4(7) = &H46
       End With   'Ìî³äPic
       With Pic
          .Size = Len(Pic)          ' Pic½á¹¹³¤¶È
          .Type = vbPicTypeBitmap   ' Í¼ÏóÀàÐÍ
          .hBmp = hBmp              ' Î»Í¼¾ä±ú
          .hPal = hPal              ' µ÷É«°å¾ä±ú
       End With   '½¨Á¢PictureͼÏó
       r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)   '·µ»ØPicture¶ÔÏó
       Set CreateBitmapPicture = IPic
    End Function
      

  2.   

    在一樓的基礎加了點東東。新建一個窗體,放一PICTUREBOX 就可以了、
    -------------------
    Option Explicit'²¶×½ÆÁÄ»µÄͼÏñPrivate Type PALETTEENTRY
       peRed As Byte
       peGreen As Byte
       peBlue As Byte
       peFlags As Byte
    End TypePrivate Type LOGPALETTE
       palVersion As Integer
       palNumEntries As Integer
       palPalEntry(255) As PALETTEENTRY
    End TypePrivate Type GUID
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(7) As Byte
    End TypePrivate Const RASTERCAPS As Long = 38
    Private Const RC_PALETTE As Long = &H100
    Private Const SIZEPALETTE As Long = 104Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End TypePrivate Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
    Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Type PicBmp
       Size As Long
       Type As Long
       hBmp As Long
       hPal As Long
       Reserved As Long
    End TypePrivate Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPictureDisp) As Long'²¶×½Í¼Ïó
    Public Function CaptureScreen(x As Long, y As Long, w As Long, h As Long) As Picture
        Dim hWndScreen As Long    '»ñµÃ×ÀÃæµÄ´°¿Ú¾ä±ú
        hWndScreen = GetDesktopWindow()
        Set CaptureScreen = CaptureWindow(hWndScreen, False, x, y, w, h)
    End FunctionPublic Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal _
        LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc _
        As Long) As Picture    Dim hDCMemory As Long
        Dim hBmp As Long
        Dim hBmpPrev As Long
        Dim r As Long
        Dim hDCSrc As Long
        Dim hPal As Long
        Dim hPalPrev As Long
        Dim RasterCapsScrn As Long
        Dim HasPaletteScrn As Long
        Dim PaletteSizeScrn As Long
        Dim LogPal As LOGPALETTE    If Client Then
            hDCSrc = GetDC(hWndSrc)
        Else
            hDCSrc = GetWindowDC(hWndSrc)
        End If    hDCMemory = CreateCompatibleDC(hDCSrc)
        hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
        hBmpPrev = SelectObject(hDCMemory, hBmp)    '»ñµÃÆÁÄ»ÊôÐÔ
        RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
        HasPaletteScrn = RasterCapsScrn And RC_PALETTE
        PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)    'Èç¹ûÆÁÄ»¶ÔÏóÓе÷É«°åÔò»ñµÃÆÁÄ»µ÷É«°å
        If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            '½¨Á¢ÆÁÄ»µ÷É«°åµÄ¿½±´
            LogPal.palVersion = &H300
            LogPal.palNumEntries = 256
            r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
            hPal = CreatePalette(LogPal)
            '½«Ð½¨Á¢µÄµ÷É«°åÑ¡È罨Á¢µÄÄÚ´æ»æͼ¾ä±úÖÐ
            hPalPrev = SelectPalette(hDCMemory, hPal, 0)
            r = RealizePalette(hDCMemory)
        End If    '¿½±´Í¼Ïó
        r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)    hBmp = SelectObject(hDCMemory, hBmpPrev)    If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            hPal = SelectPalette(hDCMemory, hPalPrev, 0)
        End If    'ÊÍ·Å×ÊÔ´
        r = DeleteDC(hDCMemory)
        r = ReleaseDC(hWndSrc, hDCSrc)    Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    End Function
    Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
      Dim r As Long   Dim Pic As PicBmp
       Dim IPic As IPicture
       Dim IID_IDispatch As GUID   'Ìî³äIDispatch½çÃæ
       With IID_IDispatch
          .Data1 = &H20400
          .Data4(0) = &HC0
          .Data4(7) = &H46
       End With   'Ìî³äPic
       With Pic
          .Size = Len(Pic)          ' Pic½á¹¹³¤¶È
          .Type = vbPicTypeBitmap   ' Í¼ÏóÀàÐÍ
          .hBmp = hBmp              ' Î»Í¼¾ä±ú
          .hPal = hPal              ' µ÷É«°å¾ä±ú
       End With   '½¨Á¢PictureͼÏó
       r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)   '·µ»ØPicture¶ÔÏó
       Set CreateBitmapPicture = IPic
    End FunctionPrivate Sub Form_Load()
     Set Me.Picture1.Picture = CaptureScreen(0, 0, 800, 600)
    End Sub
    ------------------
    www.vicmiao.com
    努力就有美好時光!
      

  3.   

    上面代码是把HDC的内容保存为BMP文件,再加载到图片框,如果你不需要文件形式的话可以直接把内容拷贝到你的窗体HDC的,简单很多。
      

  4.   

    (雍昊) ,对,我就是要直接把我要的 COPY 到我的 HDC ,不需要中间进行转存给简单的代码吧  -_-