我们系统现在要实现一个功能:区分输入到Tedit中的数据是 手动输入 还是 扫描输入。由于我们的扫描输入最后貌似没有带回车符:在记事本上测试时,扫描输入后光标都没有换行的!!好像只能通过限定时间区分,因此考虑禁止Tedit的复制粘贴功能,防止将用户的粘贴行为当作扫描输入。请问Tedit是否可以禁止复制粘贴?如何实现?或者是否有其他文本控件可实现这种功能?

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        procedure Edit1KeyPress(Sender: TObject; var Key: Char);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var
      Found: boolean;
      i,SelSt,j: Integer;
      TmpStr: string;
    begin
          with (Sender as TEdit) do
            begin
              j :=0;
              SelSt :=SelStart;
              if (Key = Chr(vk_Back)) and (SelLength <> 0) then
                TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)
              else if Key = Chr(vk_Back) then {SelLength = 0}
                TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
              else if Key in [‘0‘..‘9‘] then
                begin
                  TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);
                  j :=1;
                end
              else  {Key in [‘A‘..‘Z‘, etc]}
                TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelStart+1,255);          if (Key = Chr(vk_Back)) and (SelSt > 0) then
                Dec(SelSt)
              else if Key <> Chr(vk_Back) then
                Inc(SelSt);
              Key := #0; { indicate that key was handled }
              if SelSt = 0 then
                begin
                  Text:= ‘‘;
                  Exit;
                end
              else
                begin
                  Text :=TmpStr;
                  SelStart :=SelSt+j;
                end;
            end;end;end.
      

  2.   


    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if (Key=#3) or (Key=#22) then
        key:=#0;
    end;将一个空的popumenu赋给edit,防止右键菜单复制粘贴
      

  3.   

    拦截Edit1的消息,如果是复制,粘贴则什么都不做。
    就是将Edit1.WindowProc重定义。
      

  4.   

    还是拦WM_COPY/PASTE/CUT消息靠谱,剩下的根本拦不住
      

  5.   

    unit MyEdit;interface
    uses
      Messages, StdCtrls, Classes;
    type
      TMyEdit= Class(TEdit)
      private
        procedure myCopy(var Msg: TMessage); Message WM_COPY;
        procedure myPaste(var Msg: TMessage); Message WM_PASTE;
        procedure myCut(var Msg: TMessage); Message WM_CUT;
      end;
    procedure Register;
    implementationprocedure TMyEdit.myCopy(var Msg: TMessage);
    begin
      msg.Msg:= 0;
    end;procedure TMyEdit.myCut(var Msg: TMessage);
    begin
      msg.Msg:= 0;
    end;procedure TMyEdit.myPaste(var Msg: TMessage);
    begin
      msg.Msg:= 0;
    end;procedure Register;
    begin
    RegisterComponents('Hxy', [TMyEdit]);
    end;
    end.
    保存后安装控件即可实现不能剪切、复制、粘贴功能
      

  6.   

    我现在用拦截Edit1消息的方法实现~~~
    谢谢各位提供建议
    代码如下:unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
          Dialogs,   StdCtrls;   
        
      type   
          TForm1   =   class(TForm)   
              Edit1:   TEdit;   
              procedure   FormCreate(Sender:   TObject);   
              procedure   FormDestroy(Sender:   TObject);   
          private   
              {   Private   declarations   }   
              FOldWndProc:   TWndMethod;   
          public   
              {   Public   declarations   }   
              procedure   NewWndProc(var   Message:   TMessage);   
          end;   
        
      var   
          Form1:   TForm1;   
        
      implementation   
        
      {$R   *.dfm}   
        
      procedure   TForm1.NewWndProc(var   Message:   TMessage);   
      begin   
          if   (Message.Msg   <>   WM_CUT)   and   (Message.Msg   <>   WM_COPY)   and   
              (Message.Msg   <>   WM_PASTE)   then   
              FOldWndProc(Message);   
      end;   
        
      procedure   TForm1.FormCreate(Sender:   TObject);   
      begin   
          FOldWndProc   :=   Edit1.WindowProc;   
          Edit1.WindowProc   :=   NewWndProc;   
      end;   
        
      procedure   TForm1.FormDestroy(Sender:   TObject);   
      begin   
          Edit1.WindowProc   :=   FOldWndProc;   
      end;   
        
      end.