-
-
Save Zaaphod/7d94e1d712a5b9ac2bcb0bc79f039a63 to your computer and use it in GitHub Desktop.
Displays the contents of the Windows clipboard, including all available formats. Conversion to FreePascal so I can learn more about how the Windows Clipboard works for implementation in a FreePascal Console Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uses | |
Windows; | |
Type | |
BT = Record | |
BT_ID: Byte; | |
BT_Name: String; | |
End; | |
Const | |
Builtin_Type: Array [0..22] of BT = ( | |
{ 0} (BT_ID:2 ;BT_Name:'CF_BITMAP' ), | |
{ 1} (BT_ID:8 ;BT_Name:'CF_DIB' ), | |
{ 2} (BT_ID:17 ;BT_Name:'CF_DIBV5' ), | |
{ 3} (BT_ID:5 ;BT_Name:'CF_DIF' ), | |
{ 4} (BT_ID:130 ;BT_Name:'CF_DSPBITMAP' ), | |
{ 5} (BT_ID:142 ;BT_Name:'CF_DSPENHMETAFILE' ), | |
{ 6} (BT_ID:131 ;BT_Name:'CF_DSPMETAFILEPICT'), | |
{ 7} (BT_ID:129 ;BT_Name:'CF_DSPTEXT' ), | |
{ 8} (BT_ID:14 ;BT_Name:'CF_ENHMETAFILE' ), | |
{ 9} (BT_ID:15 ;BT_Name:'CF_HDROP' ), | |
{10} (BT_ID:16 ;BT_Name:'CF_LOCALE' ), | |
{11} (BT_ID:18 ;BT_Name:'CF_MAX' ), | |
{12} (BT_ID:3 ;BT_Name:'CF_METAFILEPICT' ), | |
{13} (BT_ID:7 ;BT_Name:'CF_OEMTEXT' ), | |
{14} (BT_ID:128 ;BT_Name:'CF_OWNERDISPLAY' ), | |
{15} (BT_ID:9 ;BT_Name:'CF_PALETTE' ), | |
{16} (BT_ID:10 ;BT_Name:'CF_PENDATA' ), | |
{17} (BT_ID:11 ;BT_Name:'CF_RIFF' ), | |
{18} (BT_ID:4 ;BT_Name:'CF_SYLK' ), | |
{19} (BT_ID:1 ;BT_Name:'CF_TEXT' ), | |
{20} (BT_ID:6 ;BT_Name:'CF_TIFF' ), | |
{21} (BT_ID:13 ;BT_Name:'CF_UNICODETEXT' ), | |
{22} (BT_ID:12 ;BT_Name:'CF_WAVE' )); | |
Var | |
i,j,k,l,Number_Of_Formats,Name_Length : Byte; | |
m : Dword; | |
FormatID : Word; | |
Format_ID : Array of Word; | |
FN : Array[0..255] of Byte; | |
Format_Name : Array of AnsiString; | |
Clipdata : Array of Pchar; | |
ClipLength : Array of Longint; | |
{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=} | |
function GetWinClipBoardData(fmt_id:Word;var p : pchar;var l : longint) : boolean; | |
var | |
h : HGlobal; | |
pp : pchar; | |
begin | |
p:=nil; | |
GetWinClipBoardData:=false; | |
h:=GetClipboardData(fmt_id); | |
if h<>0 then | |
begin | |
pp:=pchar(GlobalLock(h)); | |
l:=strlen(pp)+1; | |
getmem(p,l); | |
move(pp^,p^,l); | |
GlobalUnlock(h); | |
end; | |
GetWinClipBoardData:=h<>0; | |
end; | |
{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=} | |
Begin | |
OpenClipboard(0); | |
Number_Of_Formats := CountClipboardFormats(); | |
Writeln('Number of formats: ',Number_Of_Formats); | |
SetLength(Format_ID,Number_Of_Formats); | |
SetLength(Format_Name,Number_Of_Formats); | |
SetLength(ClipData,Number_Of_Formats); | |
SetLength(Cliplength,Number_Of_Formats); | |
If Number_Of_Formats>=1 Then | |
Begin | |
for i := 0 to Number_Of_Formats-1 do | |
Begin | |
Format_Name[i] := 'FAILED'; | |
FormatID:=EnumClipboardFormats(FormatID); | |
Format_ID[i]:=FormatID; | |
For J:= 0 to 22 do | |
Begin | |
If Format_ID[i] = Builtin_Type[J].BT_ID then | |
Format_Name[i] := Builtin_Type[J].BT_Name; | |
End; | |
If Format_Name[i] = 'FAILED' Then | |
Begin | |
Name_Length:=GetClipboardFormatName(Format_Id[i],LPTSTR(@FN[0]),255); | |
If Name_Length >0 Then | |
Begin | |
Format_Name[i]:=''; | |
For k:= 0 to Name_Length-1 Do | |
Format_Name[i]:=Format_Name[i]+Chr(FN[k]); | |
End; | |
End; | |
End; | |
For i := 0 to Number_Of_Formats-1 do | |
Begin | |
ClipData[i]:=''; | |
ClipLength[i]:=0; | |
GetWinClipBoardData(Format_ID[i],Clipdata[i],ClipLength[i]); | |
Writeln(Format_ID[i],' ',Format_Name[i]); | |
m:=ClipLength[i]; | |
if m>=256 then | |
m:=255; | |
If m>= 1 then | |
Begin | |
for l:= 0 to m - 1 do | |
Write('#',ORD(ClipData[i][l]),' '); | |
Writeln; | |
Write('|'); | |
for l:= 0 to m - 1 do | |
Write((ClipData[i][l])); | |
Writeln('|'); | |
End | |
Else | |
Writeln('||'); | |
End; | |
End | |
Else | |
Writeln('Clipboard Empty'); | |
CloseClipboard(); | |
End. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32clipboard as cb | |
builtin_type = { | |
2: "CF_BITMAP", | |
8: "CF_DIB", | |
17: "CF_DIBV5", | |
5: "CF_DIF", | |
130: "CF_DSPBITMAP", | |
142: "CF_DSPENHMETAFILE", | |
131: "CF_DSPMETAFILEPICT", | |
129: "CF_DSPTEXT", | |
14: "CF_ENHMETAFILE", | |
15: "CF_HDROP", | |
16: "CF_LOCALE", | |
18: "CF_MAX", | |
3: "CF_METAFILEPICT", | |
7: "CF_OEMTEXT", | |
128: "CF_OWNERDISPLAY", | |
9: "CF_PALETTE", | |
10: "CF_PENDATA", | |
11: "CF_RIFF", | |
4: "CF_SYLK", | |
1: "CF_TEXT", | |
6: "CF_TIFF", | |
13: "CF_UNICODETEXT", | |
12: "CF_WAVE", | |
} | |
cb.OpenClipboard() | |
print 'Number of formats: %d' % cb.CountClipboardFormats() | |
format_id = 0 | |
for i in xrange(cb.CountClipboardFormats()): | |
format_id = cb.EnumClipboardFormats(format_id) | |
print '\n---- ENTRY -----' | |
print format_id | |
format_name = 'FAILED' | |
try: | |
format_name = cb.GetClipboardFormatName(format_id) | |
except: | |
if builtin_type.has_key(format_id): | |
format_name = builtin_type[format_id] | |
print 'Format name: %s' % format_name | |
data = cb.GetClipboardData(format_id) | |
try: | |
data = data.decode() | |
except: | |
pass | |
print data | |
cb.CloseClipboard() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment