The Clipboard class (gb.qt4) is static and can be used to manipulate the system's clipboard. The clipboard is a storage area for the simple exchange of data (text and images) between programmes. First, the data is copied or cut and stored in the clipboard. Then the data stored in the clipboard can be pasted into suitable Gambas components for text or images, thus realising the copying or moving of data. When the Gambas programme that uses the clipboard is terminated, the clipboard object is also destroyed and with it its contents. A remedy is to install a clipboard manager:
The Clipboard class uses 3 constants to indicate the type of content of the clipboard:
Constant | Constant | Description |
---|---|---|
None | 0 | The clipboard content could not be interpreted as either text or image. |
Text | 1 | The clipboard content was interpreted as text. |
Image | 2 | The clipboard content was interpreted as an image. |
Table 20.4.0.1.1 : Constants of the Clipboard class
The Clipboard class has these properties:
Property | Data type | Description |
---|---|---|
Format | String | Returns the text format of the text stored in the clipboard or NULL if the clipboard is empty. |
Formats | String[] | Returns a string array with the MIME types of the data stored in the clipboard. |
Type | Integer | Returns the type of the content of the clipboard. This is either Clipboard.Text (=1) for text or Clipboard.Image (=2) for an image or Clipboard.None (=0) if the clipboard is empty. |
Table 20.4.0.2.1 : Properties of the Clipboard class
The Clipboard class has three methods, the description of which can be found in the next table:
Method | Description |
---|---|
Clear | The call to the method Clipboard.Clear deletes the contents of the clipboard. |
Copy ( Data AS Variant [ , Format AS String ] ) | Copies data to the clipboard. If the data is of the type 'String', you can specify the MIME type with the parameter 'Format', for example with “text/plain”. |
Function Paste ( [ Format As String ] ) As Variant | Inserts the content of the clipboard at the intended position. You can specify the MIME type using the optional Format parameter. NULL is returned if the content of the clipboard is empty or cannot be interpreted as text or image. |
Table 20.4.0.3.1 : Methods of the Clipboard class
Data copied from a programme to the clipboard usually exists there in several formats.
If you know the formats of your preferred (source) applications, then you can later already put the texts or images in a suitable format in the clipboard or paste them from it in the specified format into the (target) application that will further process this data.
There are several ways to put data on the clipboard or in the clipboard. What all data have in common is that under Gambas you can only paste text or images into the clipboard and from there into a (target) application. This does not apply to text or image files! You must first assign their content to a property of a usable Gambas object with the data type Picture or Image.
Example 1 - Copy a picture from a file to the clipboard (data type Image):
Clipboard.Copy(Image.Load("Pictures/fraktal_1.jpg")) Clipboard.Copy(Picture.Load("Pictures/fraktal_1.jpg").Image)
Example 2 - Putting a picture from a picture box on the clipboard:
If PictureBox1.Picture <> Null Then Clipboard.Copy(PictureBox1.Picture.Image) Endif
Example 3 - Display and save a desktop screenshot
The following procedure returns a screen copy that is immediately displayed in a PictureBox and then saved in a specified directory as a png graphic with the highest quality (→ 100 %):
[1] Public Sub btnGetScreenShot_Click() [2] FMain.Hide() [3] Wait 0.1 '-- In practice, increase to 3 seconds! [4] PictureBox1.Picture = Desktop.Screenshot() [5] PictureBox1.Picture.Save(User.Home &/ "Bilder/current_screenshot.png", 100) [6] FMain.Show() [7] [8] '-- Alternative without direct display -> Insert save dialogue option [9] '-- FMain.Hide() [10] '-- Wait 0.05 [11] '-- Desktop.Screenshot().Save(User.Home &/ "Bilder/current_screenshot.png", 100) [12] '-- FMain.Show() [13] [14] End
Under KDE and Gnome, screenshots can be created by simply pressing the Print key. With the key combination Alt+Print key you place an image of the active window in the clipboard, the entire desktop in contrast with CTRL+Print key. If you use a special programme to take a screenshot, its programme options may override the above-mentioned effects of the keys or key combinations. These programmes include Shutter and KSnapshot - but also Gimp (File> Create> Screenshot).
The console programme 'Gnome-Screenshot' is interesting in connection with the instructions SHELL and EXEC:
gnome-screenshot -w -b -c --delay=2 gnome-screenshot -i
-c, --clipboard Sending a recording directly to the clipboard -w, --window Record only one window instead of the whole screen -b, --include-border Include the window frame in the screenshot -d, --delay=Sekunden Take a screenshot only after the specified time [Time in seconds]. -i, --interactive Set options interactively
Example 4 - Screenshot of the active top-level window.
SHELL "gnome-screenshot -c -w -b --delay=1" Wait PictureBox2.Picture = Clipboard.Paste().Picture
The screenshot of the top-level window is displayed in a PictureBox.
Chapter