Repository navigation
SysWin Clipboard
Home › All Categories › sys/win › Clipboard
Puts text and other data on the clipboard, reads it back and empties the clipboard.
Each function opens the clipboard, does its work and closes it again, so there is nothing to open or close yourself. The clipboard is opened without an owner window.
Data on the clipboard lives in global memory blocks (HGLOBAL), one per format: text is
CF_UNICODETEXT, a bitmap CF_BITMAP or CF_DIB, and programs can register formats of their
own. Once a block has been placed on the clipboard it belongs to Windows; a block you read back
with CopyClipboardData is a copy, and yours to free.
Placing data replaces everything that was on the clipboard, in every format.
#import "sys/win"
using winA console program that puts a line of text on the clipboard and reads it back:
#import "sys/win"
using win
if SetClipboardText( "Copied from tiko: ½ € ж" ) then
dim text as ustring = GetClipboardText()
print "the clipboard holds"; len( text ); " characters"
else
print "the clipboard is in use by another program"
end if| Function | Does |
|---|---|
| ClearClipboard | Empties the clipboard. |
| CopyClipboardData | Returns a copy of the clipboard data in one format. |
| ReplaceClipboardData | Empties the clipboard and places a memory block on it. |
| GetClipboardText | Returns the text on the clipboard. |
| SetClipboardText | Empties the clipboard and places a text on it. |
function ClearClipboard() as booleanRemoves everything from the clipboard, in every format.
Returns true on success; false when the clipboard could not be opened (another program
has it open) or emptied.
Example
if ClearClipboard() = false then print "the clipboard is busy"Microsoft Learn: EmptyClipboard
function CopyClipboardData( byval uFormat as DWORD ) as HGLOBALCopies the clipboard data of one format into a new moveable, zero-filled global memory block of the same size. The clipboard keeps its own data.
The returned block is yours: lock it with GlobalLock to read it, and free it with
GlobalFree. This copies memory blocks only; formats whose data is a GDI handle, such as
CF_BITMAP, are not memory blocks and give NULL.
| Parameter | Meaning |
|---|---|
uFormat |
The clipboard format: CF_UNICODETEXT, CF_DIB, or a registered format. |
Returns the copy, or NULL when that format is not on the clipboard or the clipboard could
not be opened.
Example
dim hCopy as HGLOBAL = CopyClipboardData( CF_DIB )
if hCopy then
dim pih as BITMAPINFOHEADER ptr = GlobalLock( hCopy )
print "a"; pih->biWidth; " x"; pih->biHeight; " image"
GlobalUnlock( hCopy ) : GlobalFree( hCopy )
end ifMicrosoft Learn: GetClipboardData
function ReplaceClipboardData( byval uFormat as UINT, byval hData as HANDLE ) as HANDLEEmpties the clipboard and places hData on it in format uFormat. Allocate the block with
GlobalAlloc( GMEM_MOVEABLE, ... ) and fill it before the call. After a successful call the block
belongs to Windows: do not use it or free it any more.
| Parameter | Meaning |
|---|---|
uFormat |
The clipboard format of the data. |
hData |
The memory block (or, for CF_BITMAP, the bitmap handle). |
Returns the handle of the data on the clipboard, or NULL on failure; then the block is
still yours to free.
Example
dim hMem as HGLOBAL = CopyClipboardData( CF_UNICODETEXT )
if hMem then
if ReplaceClipboardData( CF_UNICODETEXT, hMem ) = NULL then GlobalFree( hMem )
end ifMicrosoft Learn: SetClipboardData
function GetClipboardText( byval uFormat as UINT = CF_UNICODETEXT ) as ustringReturns the text on the clipboard. The clipboard keeps it.
The function copies the whole memory block and treats it as UTF-16: keep the default
CF_UNICODETEXT, which Windows supplies whenever any program has put text there. The ustring
is as long as the block allows, which can be longer than the text: pass it through rtrim(),
which stops at the first null character, when its length matters.
| Parameter | Meaning |
|---|---|
uFormat |
The clipboard format; keep the default. |
Returns the text, or "" when there is none or the clipboard could not be opened.
Example
dim text as ustring = rtrim( GetClipboardText() )
if len( text ) then print "pasted"; len( text ); " characters"Microsoft Learn: Standard clipboard formats
function SetClipboardText( byref wszText as wstring, byval uFormat as UINT = CF_UNICODETEXT ) as HANDLEEmpties the clipboard and places a copy of wszText on it. The text is always stored as UTF-16,
so keep the default CF_UNICODETEXT; Windows makes the ANSI and OEM forms for programs that
ask for those.
| Parameter | Meaning |
|---|---|
wszText |
The text: a literal, a ustring or a wstring. |
uFormat |
The clipboard format; keep the default. |
Returns the handle of the data on the clipboard, which Windows owns, or NULL on failure.
Example
if SetClipboardText( GetWindowText( hCtrl ) ) = NULL then print "copy failed"Microsoft Learn: SetClipboardData
- Category: sys/win
- Bitmaps for DIBs, the format images travel in
- Strings for converting text between code pages
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing