Code: Alles auswählen
EnableExplicit
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
;CompilerError "Dieser Code läuft nicht unter Windows!"
CompilerCase #PB_OS_Linux
CompilerError "Dieser Code läuft nicht unter Linux!"
CompilerCase #PB_OS_MacOS
CompilerError "Dieser Code läuft nicht unter MacOS!"
CompilerEndSelect
CompilerIf #PB_Compiler_Version < 540
CompilerError "Es muss mindestens PureBasic Version 5.40 verwendet werden!"
CompilerEndIf
CompilerSelect #PB_Compiler_Debugger
CompilerCase 0
CompilerError "Der Debugger muss eingeschaltet sein!"
CompilerCase 1
;CompilerError "Der Debugger muss ausgeschaltet sein!"
CompilerEndSelect
UsePNGImageEncoder()
Procedure CreateWindowShot(hWnd, Client=#True)
Protected.i hDC, hImageDC, Image
Protected.RECT WindowRect, ClientRect
If hWnd
If GetWindowRect_(hWnd, WindowRect) And GetClientRect_(hWnd, ClientRect)
With WindowRect
Protected WindowWidth = \right - \left
Protected WindowHeight = \bottom - \top
EndWith
With ClientRect
Protected ClientWidth = \right - \left
Protected ClientHeight = \bottom - \top
EndWith
Protected BorderWidth = (WindowWidth - ClientWidth) / 2
Protected BorderHeight = WindowHeight - ClientHeight - BorderWidth
Select Client
Case #False
Image = CreateImage(#PB_Any, WindowWidth, WindowHeight)
Case #True
Image = CreateImage(#PB_Any, ClientWidth, ClientHeight)
EndSelect
If IsImage(Image)
hImageDC = StartDrawing(ImageOutput(Image))
If hImageDC
hDC = GetWindowDC_(hWnd)
If hDC
Select Client
Case #False
BitBlt_(hImageDC, 0, 0, WindowWidth, WindowHeight, hDC, 0, 0, #SRCCOPY|#SRCPAINT)
Case #True
BitBlt_(hImageDC, -BorderWidth, -BorderHeight, ClientWidth+BorderWidth, ClientHeight+BorderHeight, hDC, 0, 0, #SRCCOPY|#SRCPAINT)
EndSelect
ReleaseDC_(hWnd, hDC)
EndIf
StopDrawing()
Else
FreeImage(Image)
EndIf
ProcedureReturn Image
EndIf
EndIf
EndIf
EndProcedure
; ╔════════════════╤════════════════╗
; ║ ┌─────┴─────┐ ║
; ╟──────────┤ Example ├──────────╢
; ║ └─────┬─────┘ ║
; ╚════════════════╧════════════════╝
Procedure OpenWindow_Window_0()
Protected Window, Button_0, Button_1, Image, File$
Window = OpenWindow(#PB_Any, 450, 200, 400, 400, "Window - ©Rudi☺", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
If IsWindow(Window)
SetWindowColor(Window, #Red)
Button_0 = ButtonGadget(#PB_Any, 1, 1, 398, 35, "Bildschirmfoto vom gesamten Fenster")
Button_1 = ButtonGadget(#PB_Any, 1, 364, 398, 35, "Bildschirmfoto vom Clientbereich")
FrameGadget(#PB_Any, 90, 90, 210, 180, "Hinweis:")
TextGadget(#PB_Any, 105, 125, 180, 110, ~"Klicke oben auf den Button, um ein Bildschirmfoto vom gesamten Fenster zu erhalten.\n\n\nKlicke unten auf den Button, um ein Bildschirmfoto nur vom Clientbereich des Fensters zu erhalten.")
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case Button_0
Image = CreateWindowShot(WindowID(Window), #False); GetForegroundWindow_() für externes Fenster
If IsImage(Image)
Select MessageRequester("Frage", ~"Soll das Bild gespeichert werden?\n\n\"Nein\" zeigt es an.", #MB_ICONQUESTION|#MB_YESNO|#MB_TOPMOST)
Case #IDYES
File$ = SaveFileRequester("", "D:\WindowImage.png", "PNG-Image (.png)|*.png", 0)
If File$
SaveImage(Image, File$, #PB_ImagePlugin_PNG)
EndIf
EndSelect
ShowLibraryViewer("Image", Image)
EndIf
Case Button_1
Image = CreateWindowShot(WindowID(Window)); GetForegroundWindow_() für externes Fenster
If IsImage(Image)
Select MessageRequester("Frage", ~"Soll das Bild gespeichert werden?\n\n\"Nein\" zeigt es an.", #MB_ICONQUESTION|#MB_YESNO|#MB_TOPMOST)
Case #IDYES
File$ = SaveFileRequester("", "D:\ClientImage.png", "PNG-Image (.png)|*.png", 0)
If File$
SaveImage(Image, File$, #PB_ImagePlugin_PNG)
EndIf
EndSelect
ShowLibraryViewer("Image", Image)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Select EventWindow()
Case Window
CloseWindow(Window)
Break
EndSelect
EndSelect
Until GetAsyncKeyState_(#VK_ESCAPE)&1
EndIf
End Window
EndProcedure
OpenWindow_Window_0()