User Tools

Site Tools


k20:k20.4:k20.4.2:start

20.4.2 Project 2 - Clipboard Images Demonstration Program

Project 2 demonstrates mainly the use of constants, properties and methods of the Clipboard class:

B1

Figure 20.4.2.1: Clipboard class demonstration programme.

You can only be encouraged to experiment heavily with the program. This includes inserting screenshots you have taken yourself, for example:

  • with the key combination CTRL+print key or.
  • with the programmes Shutter or
  • KSnapShot or
  • with the console programme Gnome-ScreenShot with “gnome-screenshot -w -b -c -delay=1”.

For a first test, you should press the buttons in the given order and observe the displays in the two picture boxes and in the window title. You should then press button 6 at least 3 times.

The source text of project 2 is given in full. You should pay special attention to the function DesktopGrab(iMode As Integer), which allows you to take screenshots of the desktop or the active top-level window - i.e. the programme window - and display them in a picture box. For the argument iMode=0, a screenshot of the entire desktop is returned as a function value (data type Picture). For all other modes iMode> 0 you get a screenshot of the programme window.

Complete source code for the demonstration programme 'Clipboard Pictures':

' Gambas class file
' Es wird nur EIN Objekt im ClipBoard gespeichert.
 
Public Sub Form_Open()
  FMain.Resizable = False
  If Clipboard.Type <> Clipboard.None Then '-- Clipboard not empty?
     FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0]
  Endif
End
 
Public Sub btnShowPicture_Click()
  PictureBox1.Picture = Picture.Load("Pictures/fraktal_1.jpg")
End
 
Public Sub btnCopyPicture_Click()
  If PictureBox1.Picture <> Null Then
     Clipboard.Copy(PictureBox1.Picture.Image)
     FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0]
  Endif
 
End
 
Public Sub btnPastePicture_Click()
  If Clipboard.Type = Clipboard.Image Then
     PictureBox2.Picture = Clipboard.Paste().Picture '-- Note parenthesis → Function
 '-- PictureBox2.Picture = Clipboard.Paste("image/tiff").Picture '-- Option: Default MIME type
     FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0]
  Endif
  txaFormats.Clear()
End
 
Public Sub btnScreenshotDesktop_Click()
 
  PictureBox2.Picture = DesktopGrab(0) '-- Screenshot from the desktop
  Clipboard.Copy(PictureBox2.Picture.Image)
  FMain.Caption = "ClipBoard - Screenshot MIME-Type: " & Clipboard.Formats[0]
  txaFormats.Clear()
 
End
 
Public Sub btnScreenshotTopLevelWindow_Click()
 
  PictureBox2.Picture = DesktopGrab(1)
  Clipboard.Copy(PictureBox2.Picture.Image)
  FMain.Caption = "ClipBoard - Screenshot MIME-Type: " & Clipboard.Formats[0]
  txaFormats.Clear()
 
End
 
Public Sub btnShowFormats_Click()
  Dim sElement As String
 
  txaFormats.Clear()
  For Each sElement In Clipboard.Formats
      txaFormats.Insert(sElement & gb.NewLine)
  Next
 
End
 
Public Sub btnClear1_Click()
  PictureBox1.Picture = Null
  txaFormats.Clear()
  PictureBox1.Refresh()
End
 
Public Sub btnClear2_Click()
  PictureBox2.Picture = Null
  txaFormats.Clear()
  PictureBox2.Refresh()
End
 
Public Sub btnClearClipboard_Click()
  Clipboard.Clear()
  FMain.Caption = "ClipBoard"
  txaFormats.Clear()
End
 
Public Function DesktopGrab(iModus As Integer) As Picture
  Dim DTWindow As DesktopWindow
  Dim myDesktopPicture As Picture
 
  DTWindow = New DesktopWindow(Desktop.ActiveWindow)
 
  If iModus = 0 Then
     Me.Visible = False
       Wait 0.1
       myDesktopPicture = Desktop.Screenshot()
     Me.Visible = True
     Return myDesktopPicture
  Else
     Return Desktop.Screenshot(DTWindow.X, DTWindow.Y, DTWindow.W, DTWindow.H)
  Endif
 
End
 
Public Sub Form_Close()
  If Not (Clipboard.Type = Clipboard.None) Then Clipboard.Clear()
  FMain.Close()
End

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.4/k20.4.2/start.txt · Last modified: 22.10.2023 by emma

Page Tools