2006. október 31., kedd

Delete the clipboard content when the PrintScrn key is pressed


How to delete the clipboard content when the PrintScrn key is pressed


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.

procedure TFormSlideShow.ApplicationIdle(Sender: TObject; var Done: Boolean);
  {Get rid of anything on clipboard}
  if GetAsyncKeyState(VK_SNAPSHOT) <> 0 then
  Done := True;

Solve 2:

You could use Win32 API functions to hook the Clipboard and intercept clipboard messages. I incorporated this into a form as follows:


TfrmViewer = class(TForm)
  procedure FormActivate(Sender: TObject);
  procedure FormDeactivate(Sender: TObject);
  fNextCB: THandle; {stored next handle in the clipboard chain}
  procedure WMDrawClipBoard(var Msg: TWMCopy); message WM_DRAWCLIPBOARD;

procedure TfrmViewer.FormActivate(Sender: TObject);
  fNextCB := SetClipBoardViewer(Handle);

procedure TfrmViewer.FormDeactivate(Sender: TObject);
  ChangeClipBoardChain(Handle, fNextCB);

procedure TfrmViewer.WMDrawClipBoard(var Msg: TWMCopy);
{Intercepts the WM_DRAWCLIPBOARD message, which indicates that the contents of
the Windows clipboard have changed.  Then it empties the clipboard.  
This is to prevent users from doing a screen capture with the "PrintScreen" button.}
  i: Integer;
  numformat: Integer;
  numformat := ClipBoard.FormatCount;
  if (numformat > 0) then
    for i := 0 to numformat - 1 do
      if (ClipBoard.Formats[i] = CF_BITMAP) or (ClipBoard.Formats[i] = CF_DIB) then

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.

Nincsenek megjegyzések:

Megjegyzés küldése