Newsgroup: it.comp.lang.visual-basic
Titolo: Copiare negli appunti una form (la parte grafica)

C.D.O, vorrei sapere se esiste un sistema per copiare negli appunti
l'immagine di una form come la vedo sullo schermo (solo la form, non tutta
la schermata), inclusa la barra del titolo.
Le mie ricerche sono state finora infruttuose.
Grazie.


Questo articolo l'avevi visto?
http://support.microsoft.com/?kbid=240653
Bye
Raf 



No, no. l'avevo visto. Ma questo sistema emula la pressione dei tasti Stamp
e Alt+Stamp, che non va bene se voglio catturare una form MDIChild=True,
dato che cattura anche la MDI.
Forse avrei dovuto essere pi� preciso.

 dato che cattura anche la MDI.
Const LR_LOADFROMFILE = &H10
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const IMAGE_CURSOR = 2
Const IMAGE_ENHMETAFILE = 3
Const CF_BITMAP = 2
Const RC_PALETTE As Long = &H100
Const SIZEPALETTE As Long = 104
Const RASTERCAPS As Long = 38
Private Type PALETTEENTRY
    peRed As Byte
    peGreen As Byte
    peBlue As Byte
    peFlags As Byte
End Type
Private Type LOGPALETTE
    palVersion As Integer
    palNumEntries As Integer
    palPalEntry(255) As PALETTEENTRY
End Type
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type
Private Type PicBmp
    Size As Long
    Type As Long
    hBmp As Long
    hPal As Long
    Reserved As Long
End Type
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As 
Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As 
Long, ByVal hMem As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal 
wFormat As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" 
(PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic 
As IPicture) As Long
Private 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 SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal 
hObject 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 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 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 Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Dim P As IPictureDisp
Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) 
As Picture
    Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
    With IID_IDispatch
        .Data1 = &H20400
        .Data4(0) = &HC0
        .Data4(7) = &H46
    End With
    With Pic
        .Size = Len(Pic)
        .Type = vbPicTypeBitmap
        .hBmp = hBmp
        .hPal = hPal
    End With
    R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    Set CreateBitmapPicture = IPic
End Function
Private Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, 
ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As 
Picture
    Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
    Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, 
HasPaletteScrn As Long
    Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
    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)
    R = DeleteDC(hDCMemory)
    Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
End Function
Private Sub Command1_Click()
    Dim hDC As Long, hBitmap As Long
    Set P = hDCToPicture(GetDC(hwnd), 0, 0, ScaleWidth / 
Screen.TwipsPerPixelX, ScaleHeight / Screen.TwipsPerPixelY)
    OpenClipboard hwnd
    EmptyClipboard
    SetClipboardData CF_BITMAP, P
    If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then MsgBox "There was an 
error while pasting the bitmap to the clipboard!"
    CloseClipboard
    Command2.Enabled = True
End Sub
Private Sub Command2_Click()
    SavePicture P, "c:\prova.bmp"
End Sub
-- 
Ciao Fede
By Chicco Production
http://bcp.freetools.it
 

 Ciao Fede
Questo � ok, salvo il fatto che non copiala barra del titolo.
Ho risolto taroccando un po' i parametri della chiamata a hDCToPicture.
Grazie!

 Ho risolto taroccando un po' i parametri della chiamata a hDCToPicture.
Complimenti sei intervenuto nel punto giusto, infatti ScaleWidth e 
ScaleHeight ti restituiscono l'area interna,
mentre Width ed Height ti restituiscono quella interna, quindi da cos�:
Set P = hDCToPicture(GetDC(hwnd), 0, 0, ScaleWidth /
Screen.TwipsPerPixelX, ScaleHeight / Screen.TwipsPerPixelY)
diventa cos�:
Set P = hDCToPicture(GetDC(hwnd), 0, 0, Width /
Screen.TwipsPerPixelX, Height / Screen.TwipsPerPixelY)
PS: Buttando gi� il codice, mi sono dimenticato quel piccolo particolare!
