Ne sachant pas si tu captures une fenêtre depuis une autre fenêtre, ce code comporte deux fenêtres.
-La première est une fenêtre qui contient uniquement un bouton Print Screen.
-La deuxième sera la fenêtre à capturer et à imprimer.
-Employant des API Windows, ce code n'est pas Cross Platform.
Code : Tout sélectionner
Enumeration File
#Library
EndEnumeration
Enumeration Window
#Window1
#Window2
EndEnumeration
Enumeration
#PrintScreen
#List
#Button1
#Button2
EndEnumeration
Declare CaptureScreen()
Declare Window_Open1()
Declare Window_Open2()
Prototype.i ptPrintWindow(hWnd, hdc, flags)
If OpenLibrary(#Library, "User32.dll")
Global PrintWindow.ptPrintWindow = GetFunction(#Library, "PrintWindow")
Else
MessageRequester("Information","Souci durant l'ouverture du fichier user32.dll")
End
EndIf
;Cette première fenêtre contient le bouton de capture de la seconde fenêtre
Procedure Window_Open1()
ExamineDesktops()
OpenWindow(#Window1, 0, 0, DesktopWidth(0), DesktopHeight(0), "#Window1", #PB_Window_SystemMenu)
ButtonGadget(#PrintScreen, 100, 10, 80, 22, "Capture")
ProcedureReturn #True
EndProcedure
;Fenêtre à capturer
Procedure Window_Open2()
OpenWindow(#Window2, 0, 0, 800, 600, "#Window2", #PB_Window_SystemMenu|#PB_Window_WindowCentered)
ListViewGadget(#List, 10, 10, 675, 580)
ButtonGadget(#Button1, 700, 10, 80, 22, "Button1")
ButtonGadget(#Button2, 700, 35, 80, 22, "Button2")
StickyWindow(#Window2, #True) ;Fenetre au premier plan
EndProcedure
Procedure CaptureScreen()
Protected Window.s = GetWindowTitle(#Window2)
Protected Left
Protected Top
Protected Width
Protected Height
Protected Image
hWnd = FindWindow_(0, Window)
If hWnd
SetWindowLongPtr_(hwnd,#GWL_EXSTYLE,#WS_EX_LAYERED)
SetLayeredWindowAttributes_(hwnd,0,0,#LWA_ALPHA)
ShowWindow_(hWnd,#SW_RESTORE)
GetWindowRect_(hWnd, r.RECT)
Width = r\Right-r\Left
Height = r\Bottom-r\Top
Image = CreateImage(#PB_Any, Width, Height, 24)
Result = StartDrawing(ImageOutput(Image))
Delay(100)
PrintWindow(hWnd, Result, 0)
StopDrawing()
If PrintRequester()
If StartPrinting("Test d'impression")
If StartDrawing(PrinterOutput())
BackColor(RGB(255, 255, 255))
FrontColor(RGB(0, 0, 0))
Scale = PrinterPageWidth()/ImageWidth(Image)
Width = ImageWidth(Image) * Scale
Height = ImageHeight(Image) * Scale
DrawImage(ImageID(Image), 0, 0, Width, Height)
StopDrawing()
EndIf
StopPrinting()
EndIf
EndIf
Else
MessageRequester("Information", "La fenêtre " + Window + " N'est pas ouverte")
EndIf
EndProcedure
If Window_Open1()
Window_Open2()
EndIf
BindGadgetEvent(#PrintScreen, @CaptureScreen())
Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow