2006. október 31., kedd

Delete the clipboard content when the PrintScrn key is pressed


Problem/Question/Abstract:

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.

procedure TFormSlideShow.ApplicationIdle(Sender: TObject; var Done: Boolean);
begin
  {Get rid of anything on clipboard}
  if GetAsyncKeyState(VK_SNAPSHOT) <> 0 then
    ClipBoard.Clear;
  Done := True;
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:

uses
  Clipbrd;

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

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

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

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.}
var
  i: Integer;
  numformat: Integer;
begin
  numformat := ClipBoard.FormatCount;
  if (numformat > 0) then
  begin
    for i := 0 to numformat - 1 do
      if (ClipBoard.Formats[i] = CF_BITMAP) or (ClipBoard.Formats[i] = CF_DIB) then
        ClipBoard.Clear;
  end;
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.

Nincsenek megjegyzések:

Megjegyzés küldése