当前位置: 代码迷 >> Delphi >> Delphi统制编辑类控件不能粘贴
  详细解决方案

Delphi统制编辑类控件不能粘贴

热度:3900   发布时间:2013-02-26 00:00:00.0
Delphi控制编辑类控件不能粘贴

在一些C/S软件和登录网站输入账号密码时经常只能进行输入,而不让用户直接粘贴,防止上次用户登录账号没清除,下一个用户直接粘贴该账号而导致账号信息泄露,刚好在公司开发中要实现该功能,下面记录下在Delphi中实现该功能的方法。粘贴主要是利用了windows的剪贴板功能,因此清除剪贴板中内容即可实现。

打开Delphi,新建一个应用程序,在窗体上放置2个Edit控件和一个popupmenu控件。窗体文件和源码如下:

object Form1: TForm1
  Left = 333
  Top = 327
  Width = 328
  Height = 199
  Caption = '禁用粘贴功能'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object edt1: TEdit
    Left = 64
    Top = 32
    Width = 121
    Height = 21
    PopupMenu = pm1
    TabOrder = 0
    OnKeyDown = edt1KeyDown
  end
  object edt2: TEdit
    Left = 64
    Top = 72
    Width = 121
    Height = 21
    TabOrder = 1
    OnKeyDown = edt2KeyDown
  end
  object pm1: TPopupMenu
    Left = 216
    Top = 64
  end
end

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus;

type
  TForm1 = class(TForm)
    pm1: TPopupMenu;
    edt1: TEdit;
    edt2: TEdit;
    procedure edt1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edt2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses clipbrd;

procedure TForm1.edt1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key in [86..118]) and (ssCtrl in Shift) then
   edt1.ReadOnly:=True
  else
   edt1.ReadOnly:=False;
end;

procedure TForm1.edt2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key in [86..118]) and (ssCtrl in Shift) then
   begin
     if clipboard.HasFormat(CF_Text) then
       clipboard.SetTextBuf('');
   end;
end;

end.

方法一利用了控件的只读属性,而方法二直接给剪贴板内容设置为空串,这样其他地方也不能再使用该内容。效果如下:

   屏蔽了右键菜单,只能输入不能粘贴内容。

 

 

  相关解决方案