Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1924to1928
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
VBA: Screenshot von Frame im Userform
27.03.2023 19:45:03
MarC

Hallo zusammen,

ich habe vor kurzem hier im Forum gefragt ob es möglich ist vom Userform per Button einen Screenshot zu machen. Das funktioniert dank eurer hilfe ohne Probleme. Jetzt würde ich wissen ob man auch einen Screenshot per Button nur vom Frame und nicht vom ganze Userforma machen kann? Der Code für das ganze Userform sieht wie folgt aus. Kann mir hier jemand weiterhelfen? Kann ich das Frame1 im Userform direkt ansprechen?

Private Sub Screenshot_Click()

    Dim doc As Object, rng As Range
    Application.SendKeys "(%{1068})"
    DoEvents
End Sub


2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA: Screenshot von Frame im Userform
27.03.2023 20:05:38
Nepumuk
Hallo Marc,

im Modul des UserForms:

Option Explicit

Private Declare PtrSafe Function GetWindowRect Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByRef lpRect As RECT) As Long

Private Sub CommandButton1_Click()

    Dim udtRectangularForm As RECT

    Call GetWindowRect(Frame1.[_GethWnd], udtRectangularForm)

    Call FrameToClipboard(udtRectangularForm)

End Sub
In einem Standardmodul:

Option Explicit

Private Declare PtrSafe Function CreateCompatibleDC Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr) As LongPtr
Private Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long) As LongPtr
Private Declare PtrSafe Function SelectObject Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr, _
    ByVal hObject As LongPtr) As LongPtr
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr, _
    ByVal nIndex As Long) As Long
Private Declare PtrSafe Function GetSystemPaletteEntries Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr, _
    ByVal wStartIndex As Long, _
    ByVal wNumEntries As Long, _
    ByRef lpPaletteEntries As PALETTEENTRY) As Long
Private Declare PtrSafe Function CreatePalette Lib "gdi32.dll" ( _
    ByRef lpLogPalette As LOGPALETTE) As LongPtr
Private Declare PtrSafe Function SelectPalette Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr, _
    ByVal hPalette As LongPtr, _
    ByVal bForceBackground As Long) As LongPtr
Private Declare PtrSafe Function RealizePalette Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr) As Long
Private Declare PtrSafe Function BitBlt Lib "gdi32.dll" ( _
    ByVal hDestDC As LongPtr, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal hSrcDC As LongPtr, _
    ByVal xSrc As Long, _
    ByVal ySrc As Long, _
    ByVal dwRop As Long) As Long
Private Declare PtrSafe Function DeleteDC Lib "gdi32.dll" ( _
    ByVal hdc As LongPtr) As Long
Private Declare PtrSafe Function DeleteObject Lib "gdi32.dll" ( _
    ByVal hObject As LongPtr) As Long
Private Declare PtrSafe Function GetDC Lib "user32.dll" ( _
    ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function OpenClipboard Lib "user32.dll" ( _
    ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function SetClipboardData Lib "user32.dll" ( _
    ByVal wFormat As Long, _
    ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32.dll" ( _
    ByVal wFormat As Long) As Long
Private Declare PtrSafe Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32.dll" () As Long

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

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 Const HWND_DESKTOP As LongPtr = 0
Private Const RASTERCAPS As Long = 38&
Private Const RC_PALETTE As Long = &H100&
Private Const SIZEPALETTE  As Long = 104&
Private Const SRCCOPY  As Long = &HCC0020
Private Const CF_BITMAP  As Long = 2&

Public Sub FrameToClipboard(ByRef prudtRectangle As RECT)

    Call OpenClipboard(0)
    Call EmptyClipboard
    Call SetClipboardData(CF_BITMAP, DC_To_Picture(prudtRectangle))
    Call CloseClipboard

End Sub

Private Function DC_To_Picture( _
    ByRef prudtRect As RECT) As LongPtr

    Dim lngLeftSrc As Long, lngTopSrc As Long, lngWidthSrc As Long, lngHeightSrc As Long
    Dim lngptrhDCMemory As LongPtr, lngptrhBmp As LongPtr, lngptrhDCScr As LongPtr
    Dim lngptrhPal As LongPtr, lngptrhPalPrev As LongPtr, lngptrhBmpPrev As LongPtr
    Dim lngRasterCapsScrn As Long
    Dim lngHasPaletteScrn As Long, lngPaletteSizeScrn As Long
    Dim udtLogPal As LOGPALETTE

    lngLeftSrc = prudtRect.Left
    lngTopSrc = prudtRect.Top
    lngWidthSrc = prudtRect.Right - prudtRect.Left
    lngHeightSrc = prudtRect.Bottom - prudtRect.Top

    lngptrhDCScr = GetDC(HWND_DESKTOP)

    lngptrhDCMemory = CreateCompatibleDC(lngptrhDCScr)
    lngptrhBmp = CreateCompatibleBitmap(lngptrhDCScr, lngWidthSrc, lngHeightSrc)
    lngptrhBmpPrev = SelectObject(lngptrhDCMemory, lngptrhBmp)
    lngRasterCapsScrn = GetDeviceCaps(lngptrhDCScr, RASTERCAPS)
    lngHasPaletteScrn = lngRasterCapsScrn And RC_PALETTE
    lngPaletteSizeScrn = GetDeviceCaps(lngptrhDCScr, SIZEPALETTE)

    If lngHasPaletteScrn And (lngPaletteSizeScrn = &H100&) Then
        udtLogPal.palVersion = &H300&
        udtLogPal.palNumEntries = &H100&
        Call GetSystemPaletteEntries(lngptrhDCScr, 0&, _
            &H100&, udtLogPal.palPalEntry(0&))
        lngptrhPal = CreatePalette(udtLogPal)
        lngptrhPalPrev = SelectPalette(lngptrhDCMemory, lngptrhPal, 0&)
        Call RealizePalette(lngptrhDCMemory)
    End If

    Call BitBlt(lngptrhDCMemory, 0&, 0&, lngWidthSrc, lngHeightSrc, _
        lngptrhDCScr, lngLeftSrc, lngTopSrc, SRCCOPY)

    lngptrhBmp = SelectObject(lngptrhDCMemory, lngptrhBmpPrev)

    If lngHasPaletteScrn And (lngPaletteSizeScrn = 256&) Then _
        lngptrhPal = SelectPalette(lngptrhDCMemory, lngptrhPalPrev, 0&)

    Call DeleteDC(lngptrhDCMemory)

    DC_To_Picture = lngptrhBmp

End Function
Gruß
Nepumuk


Anzeige
Danke :-)
27.03.2023 20:29:06
MarC
Vielen Dank Nepumuk es läuft. Wieso ist der Code eigentlich so unglaublich lang?

300 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige