Author: Tomas Rutkauskas
How to delete the clipboard content when the PrintScrn key is pressed
Answer:
Solve 1:
This deletes anything on the clipboard if VK_SNAPSHOT was seen. There's only a
small chance of getting something off the clipboard.
1 2 procedure TFormSlideShow.ApplicationIdle(Sender: TObject; var Done: Boolean);
3 begin4 {Get rid of anything on clipboard}5 if GetAsyncKeyState(VK_SNAPSHOT) <> 0 then6 ClipBoard.Clear;
7 Done := True;
8 end;
Solve 2:
You could use Win32 API functions to hook the Clipboard and intercept clipboard
messages. I incorporated this into a form as follows:
9 uses10 Clipbrd;
11 12 TfrmViewer = class(TForm)
13 procedure FormActivate(Sender: TObject);
14 procedure FormDeactivate(Sender: TObject);
15 private16 fNextCB: THandle; {stored next handle in the clipboard chain}17 procedure WMDrawClipBoard(var Msg: TWMCopy); message WM_DRAWCLIPBOARD;
18 end;
19 20 procedure TfrmViewer.FormActivate(Sender: TObject);
21 begin22 fNextCB := SetClipBoardViewer(Handle);
23 end;
24 25 procedure TfrmViewer.FormDeactivate(Sender: TObject);
26 begin27 ChangeClipBoardChain(Handle, fNextCB);
28 end;
29 30 procedure TfrmViewer.WMDrawClipBoard(var Msg: TWMCopy);
31 {Intercepts the WM_DRAWCLIPBOARD message, which indicates that the contents of 32 the Windows clipboard have changed. Then it empties the clipboard. 33 This is to prevent users from doing a screen capture with the "PrintScreen" button.}34 var35 i: Integer;
36 numformat: Integer;
37 begin38 numformat := ClipBoard.FormatCount;
39 if (numformat > 0) then40 begin41 for i := 0 to numformat - 1 do42 if (ClipBoard.Formats[i] = CF_BITMAP) or (ClipBoard.Formats[i] = CF_DIB) then43 ClipBoard.Clear;
44 end;
45 end;
The clipboard hook is set in the form's OnActivate event and unhooked in the OnDeactivate event. The message handler checks for a bitmap format in the clipboard, and if it finds it, the clipboard gets cleared. This effectively captures both "Printscreen" and "ALT + PrintScreen". You do need to keep a couple of things in mind though: If other programs are running that use Copy/ Cut/ Paste functions, anything that these programs copied to the clipboard will also get cleared. There are other methods that can be used to do screen captures that do not involve either the "PrintScreen" key or the Clipboard, and they are more difficult to prevent, short of disabling all other running applications or adversely affecting the performance of your own application.