Seite 2 von 2

Re: Screenshot erstellen(Windows und Linux)

Verfasst: 30.01.2012 18:09
von ts-soft
Tmp ist an dem Ort, habs trotzdem angepasst, aber das image wird dort nicht erzeugt. Ich denke mal, es liegt an der Zeile drüber, die
ich auch angepasst habe. Aber da weiß ich nicht was die macht und ob das so immer geht.

Re: Screenshot erstellen(Windows und Linux)

Verfasst: 05.02.2012 15:47
von DarkDragon
Keine Ahnung, ich hab grad kein Linux da. Das ganze hat ImageMagick verwendet. Ich weiß nicht ob das überhaupt noch mitgeliefert wird in den Distributionen und ob sich die Parameter geändert haben.

Es speichert nen Screenshot einfach ab im Ordner tmp und diesen lädt der Code auch wieder.

http://www.imagemagick.org/www/import.html

Re: Screenshot erstellen(Windows und Linux)

Verfasst: 07.02.2012 16:50
von mk-soft
Danke für das Snippet,
Konnte ich gut gebrauchen da ich mehrere Bilder in ein Word-Dokument einfügen muss.
Habe das mal mit ein Global Shortcut (ALT-C) und SetClipboardImage erweitert.

Bugfix wenn höhe oder breite Null ist.

Code: Alles auswählen

;-TOP

EnableExplicit

; ***************************************************************************************

; GlobalHotkeys library
; By Gansta93
; procedure AddHotkey
;{
ProcedureDLL AddHotkey(Window, fsModifiers, Keys, HotkeyID) ; Create a new global hotkey HotkeyID in specified Window.
  ProcedureReturn RegisterHotKey_(WindowID(Window), HotkeyID, fsModifiers, Keys)
EndProcedure

ProcedureCDLL AddHotkey_DEBUG(Window, fsModifiers, Keys, HotkeyID)
  If IsWindow(Window) = 0
    ; TB_DebugError("Specified Window is invalid.")
  EndIf
EndProcedure
;}

; Procedure RemoveHotkey
;{
ProcedureDLL RemoveHotkey(Window, HotkeyID) ; Remove Specified HotkeyID in Window.
  ProcedureReturn UnregisterHotKey_(WindowID(Window), HotkeyID)
EndProcedure

ProcedureCDLL RemoveHotkey_DEBUG(Window, HotkeyID)
  If IsWindow(Window) = 0
    ; TB_DebugError("Specified Window is invalid.")
  EndIf
EndProcedure
;}

; Sample (angepasst an PB4.x)

#PB_Event_Hotkey = 786

; ***************************************************************************************

; ***************************************************************************************

#SCREENSHOT_MOUSE = 2

ExamineDesktops()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  
  Procedure GetCurrentCursor(*pt.Point)
    Protected hWindow
    Protected dwThreadID
    Protected dwCurrentThreadID
    Protected Result, nil
   
    If GetCursorPos_(*pt)
      hWindow = WindowFromPoint_(*pt\x << 32 + *pt\y)
      If IsWindow_(hWindow)
        dwThreadID = GetWindowThreadProcessId_(hWindow, @nil)
        dwCurrentThreadID = GetCurrentThreadId_()
       
        If (dwCurrentThreadID <> dwThreadID)
          If AttachThreadInput_(dwCurrentThreadID, dwThreadID, 1)
            Result = GetCursor_()
            AttachThreadInput_(dwCurrentThreadID, dwThreadID, 0)
          EndIf
        Else
          Result = GetCursor_()
        EndIf
      EndIf
    EndIf
    ProcedureReturn Result
  EndProcedure
  
CompilerEndIf

Procedure MakeDesktopScreenshot(ImageNr,x,y,Width,Height,Flags)
  
  Protected hImage, hDC, DeskDC, hCursor
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    
    RunProgram("import", "-window root -crop "+Str(Width)+"x"+Str(Height)+"+"+Str(x)+"+"+Str(y)+" /tmp/snapshot.bmp", "/", 1)
    hImage = LoadImage(ImageNr, "/tmp/snapshot.bmp") ; ----- crashed?
    
  CompilerElse
    
    Protected pt.point
    hImage = CreateImage(ImageNr,Width,Height)
    If hImage
      hDC = StartDrawing(ImageOutput(ImageNr))
      If hDC
        DeskDC = GetDC_(GetDesktopWindow_())
        If DeskDC
          BitBlt_(hDC,0,0,Width,Height,DeskDC,x,y,#SRCCOPY)
        EndIf
        ReleaseDC_(GetDesktopWindow_(),DeskDC)
      EndIf
      If (Flags & #SCREENSHOT_MOUSE)
        hCursor = GetCurrentCursor(@pt.Point)
        DrawImage(hCursor, pt\x, pt\y)
      EndIf
      StopDrawing()
    EndIf
    
  CompilerEndIf
  
  ProcedureReturn hImage
EndProcedure

Procedure ViewImage()
  Protected Event
 
  If OpenWindow(0, 0, 0, ImageWidth(0), ImageHeight(0), "Image", #PB_Window_SystemMenu)
    ImageGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), ImageID(0))
   
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
    CloseWindow(0)
  EndIf
EndProcedure

InitSprite()
InitMouse()

Structure Area
  X.l
  Y.l
  Width.l
  Height.l
EndStructure

Procedure MouseControlScreenshot(Img, Flags)
  Protected Area.Area
  Protected Mousex, MouseY, capMouse, oMouseX, oMouseY
  Protected v, Q, ImgNew
 
  MakeDesktopScreenshot(Img,0,0,DesktopWidth(0),DesktopHeight(0), Flags)
  OpenScreen(DesktopWidth(0), DesktopHeight(0), 32, "Capture")
 
  Repeat
    If IsScreenActive()
      ExamineMouse()
     
      MouseX = MouseX()
      MouseY = MouseY()
      If MouseButton(1)
        If capMouse = 0
          oMouseX = MouseX
          oMouseY = MouseY
          capMouse = 1
        EndIf
      Else
        If capMouse = 1
          If MouseX < oMouseX : v = oMouseX : oMouseX = MouseX : MouseX = v : EndIf
          If MouseY < oMouseY : v = oMouseY : oMouseY = MouseY : MouseY = v : EndIf
          Area\X = oMouseX
          Area\Y = oMouseY
          Area\Width = MouseX-oMouseX
          Area\Height = MouseY-oMouseY
          Q = 1
        EndIf
      EndIf
     
      ClearScreen(0)
      StartDrawing(ScreenOutput())
      DrawImage(ImageID(0), 0, 0)
      DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
      If capMouse <> 0
        Box(oMouseX, oMouseY, MouseX-oMouseX, MouseY-oMouseY)
      EndIf
      DrawingMode(#PB_2DDrawing_XOr)
      Box(MouseX-1 , MouseY-10, 2 , 20)
      Box(MouseX-10, MouseY-1 , 20, 2 )
      StopDrawing()
      FlipBuffers()
    EndIf
   
    Delay(10)
  Until Q = 1
  CloseScreen()
  If Area\Height > 0 And Area\Width > 0
    ImgNew = GrabImage(Img, #PB_Any, Area\X, Area\Y, Area\Width, Area\Height)
    FreeImage(Img)
    CopyImage(ImgNew, Img)
    FreeImage(ImgNew)
    ProcedureReturn ImageID(0)
  Else
    FreeImage(Img)
    ProcedureReturn 0
  EndIf
  
EndProcedure

; ***************************************************************************************

Procedure Main()
  
  Protected hImg
  
  If OpenWindow(0, 0, 0, 240, 24, "Cut Screenshot v1.0", #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_Minimize)
    
    If CreateStatusBar(0, WindowID(0))
      AddStatusBarField(#PB_Ignore)
    Else
      ProcedureReturn
    EndIf
    StatusBarText(0, 0, "Type Alt+C for cut screenshot")
    ; We create the hotkeys, and display an error box if it is impossible to create one or the two.
    If AddHotkey(0, #MOD_ALT, #PB_Shortcut_C, 0) = 0
      MessageRequester("Error", "Cannot create hotkey Alt-C.")
      ProcedureReturn
    EndIf
      
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_Hotkey ; If an hotkey was called
          Select EventwParam() ; For know what hotkey was called, no documentation on PureBasic for this function.
            Case 0 ; Hotkey with ID 0 (Alt+C)
              hImg = MouseControlScreenshot(0, #SCREENSHOT_MOUSE)
              If hImg
                SetClipboardImage(0)
                FreeImage(0)
              EndIf
          EndSelect
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver
    ; Now we remove the hotkeys.
    RemoveHotkey(0, 0)
    
  EndIf
  
EndProcedure: Main()

Re: Screenshot erstellen(Windows und Linux)

Verfasst: 07.02.2012 21:55
von NicTheQuick
@mk-soft:
Schade, dass das mit der Plattformunabhängigkeit jetzt vorbei ist. Möchtest du deinen Code vielleicht noch so anpassen, dass er auch direkt unter Linux ausführbar oder zumindest kennzeichnen, dass dem nicht so ist? Dann würde der Threadtitel auch noch besser passen. :wink:

Re: Screenshot erstellen(Windows und Linux)

Verfasst: 08.02.2012 08:36
von mk-soft
Ich habe leider Linux zur Zeit nicht zur Hand.
Vielleicht weiß einer wie das mit Global Shortcut in Linux geht...

Die Frage habe ich mal ins englischen Forum gestellt.