Rubrik: Oberfläche · Sonstiges | VB-Versionen: VB4, VB5, VB6 | 31.01.02 |
Bildschirm-Inhalt einfangen und drucken Zwei Funktionen, um den aktuellen Bildschirm-Inhalt in eine PictureBox zu kopieren oder auszudrucken - ohne Zwischenablage! | ||
Autor: Martoeng | Bewertung: | Views: 45.051 |
www.martoeng.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Heute möchten wir Ihnen zwei Funktionen vorstellen, mit denen man den aktuellen Bildschirm-Inhalt:
a) in einer PictureBox anzeigen kann
b) oder direkt ausdrucken kann
Das ganze wird unter Zuhilfenahme der entsprechenden WindowsAPI-Funktionen realisiert.
Zunächst benötigen wir das Handle des Desktop-Fensters:
Private Declare Function GetDesktopWindow Lib "user32" _ () As Long
hWnd = GetWindowsDektop()
Anschliessend ermitteln wir den Gerätekontext DC (DeviceContext):
Private Declare Function GetDC Lib "user32" _ (ByVal hWnd As Long) As Long
DC = GetDC(hWnd)
Nun wird der Inhalt des DeviceContect in die PictureBox "geblittet" oder direkt auf den Drucker ausgegeben:
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
' in die PictureBox BitBlt Picture.hDC, 0, 0, _ Screen.Width / Screen.TwipsPerPixelX, _ Screen.Height / Screen.TwipsPerPixelY, DC, 0, 0, _ vbSrcCopy
' oder direkt ausdrucken BitBlt Printer.hDC, 0, 0, _ Screen.Width / Screen.TwipsPerPixelX, _ Screen.Height / Screen.TwipsPerPixelY, DC, 0, 0, _ vbSrcCopy
Abschliessend wird der DeviceContext wieder freigegeben:
Private Declare Function ReleaseDC Lib "user32" _ (ByVal hWnd As Long, ByVal hdc As Long) As Long
ReleaseDC hWnd, DC
Und hier der gesamte Code zusammengefasst
' zunächst die benötigten API-Deklarationen 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 GetDC Lib "user32" ( _ ByVal hWnd As Long) 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 Long
' aktuellen Bildschirm-Inhalt in PictureBox anzeigen Public Sub ScreenToPicture(Picture_hDC As Long) Dim hWnd As Long Dim DC As Long ' Desktop-Fenster hWnd = GetDesktopWindow() ' Zugang zum Device-Context DC = GetDC(hWnd) ' in PictureBox blitten BitBlt Picture_hDC, 0, 0, _ Screen.Width / Screen.TwipsPerPixelX, _ Screen.Height / Screen.TwipsPerPixelY, DC, 0, 0, _ vbSrcCopy ' DC wieder freigeben ReleaseDC hWnd, DC End Sub
' aktuellen Bildschirminhalt drucken Public Sub ScreenToPrinter() Dim hWnd As Long Dim DC As Long ' Desktop-Fenster hWnd = GetDesktopWindow ' Zugang zum Device-Context DC = GetDC(hWnd) ' Screen drucken Printer.Print BitBlt Printer.hDC, 0, 0, _ Screen.Width / Screen.TwipsPerPixelX, _ Screen.Height / Screen.TwipsPerPixelY, DC, 0, 0, _ vbSrcCopy ' DC wieder freigeben ReleaseDC hWnd, DC ' Druckauftrag beenden Printer.EndDoc End Sub
Natürlich kann der "Screenshot" auch erst in der PictureBox angezeigt und später ausgedruckt werden. Hierzu schauen Sie sich dann am besten folgenden Tipp an: Grafik drucken