如何在按下CTRL+V后对粘贴内容进行检查
如果有非法字符则给出提示
请给出具体代码,谢谢~!

解决方案 »

  1.   

    處理控件
    procedure WMPaste(var Message: TMessage); message WM_PASTE; //消息
    ...procedure TMyEdit.WMPaste(var Message: TMessage); 
    var 
      Accept: Boolean; 
      Handle: THandle; 
      CText: string; 
      LText: string; 
      AText: string; 
    begin 
      if FPreventPaste then 
        Exit; 
      if IsClipboardFormatAvailable(CF_TEXT) then 
      begin 
        try 
          OpenClipBoard(Self.Handle); 
          Handle := GetClipboardData(CF_TEXT); 
          if Handle = 0 then 
            Exit; 
          CText := StrPas(GlobalLock(Handle)); 
          GlobalUnlock(Handle); 
          Accept := True; 
       //在這裹加入你要檢查的規則
          if Assigned(FOnPaste) then 
            FOnPaste(Self, CText, Accept); 
          if not Accept then 
            Exit; 
          LText := ''; 
          if SelStart > 0 then 
            LText := Copy(Text, 1, SelStart); 
          LText := LText + CText; 
          AText := ''; 
          if (SelStart + 1) < Length(Text) then 
            AText := Copy(Text, SelStart + SelLength + 1, Length(Text) - SelStart + SelLength + 1); 
          Text := LText + AText; 
        finally 
          CloseClipBoard; 
        end; 
      end; 
    end; 
      

  2.   

    剪贴板监视:
    unit Main;interfaceuses
      SysUtils, Windows, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Menus, ComCtrls,
      Clipbrd,Shellapi,Printers,ImgList,about;const
      WM_ICONMESSAGE=WM_USER+$100;type
      TCountArray = record
        Count:Integer;
        ReType:Boolean;
    end;type
      TMainForm = class(TForm)
        MainMenu: TMainMenu;
        FileSaveItem: TMenuItem;
        FilePrintItem: TMenuItem;
        FilePrintSetupItem: TMenuItem;
        FileExitItem: TMenuItem;
        EditCopyItem: TMenuItem;
        StatusLine: TStatusBar;
        SaveDialog: TSaveDialog;
        PrintDialog: TPrintDialog;
        PrintSetupDialog: TPrinterSetupDialog;
        ListBox1: TListBox;
        ClipImage: TImage;
        PMenuImage: TPopupMenu;
        PMenuText: TPopupMenu;
        MPopTCopyToClip: TMenuItem;
        MPopTSaveToFile: TMenuItem;
        MPopICopyToClip: TMenuItem;
        MPopISaveToFile: TMenuItem;
        TextString: TListBox;
        M_About: TMenuItem;
        PopBarMenu: TPopupMenu;
        MPopBarOPen: TMenuItem;
        N2: TMenuItem;
        MPopBarAbout: TMenuItem;
        MPOpBarExit: TMenuItem;
        ImageEnabled: TImage;
        ImageDisable: TImage;
        FindDialog1: TFindDialog;
        MEditFind: TMenuItem;
        Edit1: TRichEdit;
        Memo1: TMemo;
        MEditFindNext: TMenuItem;
        procedure FormCreate(Sender: TObject);
        procedure ShowHint(Sender: TObject);
        procedure FileSave(Sender: TObject);    procedure FilePrint(Sender: TObject);
        procedure FilePrintSetup(Sender: TObject);
        procedure FileExit(Sender: TObject);
        procedure EditCopy(Sender: TObject);
        procedure FormResize(Sender: TObject);    //Owner produced message
        procedure WMDrawClip(var Message:TMessage);message WM_DRAWCLIPBOARD;
        procedure WMHotKey(var Message: TMessage); message WM_HOTKEY;
        procedure WMBarIcon(var Message:TMessage);message WM_ICONMESSAGE;
        procedure WMSysCommand(var Message:TMessage);message WM_SYSCOMMAND;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure MPopISaveToFileClick(Sender: TObject);
        procedure ListBox1Click(Sender: TObject);
        procedure MPopTSaveToFileClick(Sender: TObject);
        procedure MPopICopyToClipClick(Sender: TObject);
        procedure MPopTCopyToClipClick(Sender: TObject);
        procedure M_AboutClick(Sender: TObject);
        procedure MPOpBarExitClick(Sender: TObject);
        procedure MPopBarAboutClick(Sender: TObject);
        procedure MPopBarOPenClick(Sender: TObject);
        procedure FindDialog1Find(Sender: TObject);
        procedure MEditFindClick(Sender: TObject);
        procedure MEditFindNextClick(Sender: TObject);
    end;procedure ExtFindText;var
      MainForm: TMainForm;
      bCanPaste,bEnabled:Boolean;
      hwndNextClip,hwndCurrent,CountPointer,ImageCount:integer;
      ListArray : array[0..1023]of TCountArray;
      BitMapArr : array[0..1023]of TBitMap;
      strFindText:String;
    implementation{$R *.DFM}procedure ExtFindText;
    var
      FoundAt: LongInt;
      StartPos, ToEnd: integer;
    begin
      with MainForm.Edit1 do
      begin
        { begin the search after the current selection if there is one }
        { otherwise, begin at the start of the text }
        if SelLength <> 0 then
          StartPos := SelStart + SelLength
        else
          StartPos := 0;    { ToEnd is the length from StartPos to the end of the text in
        the rich edit control }
        ToEnd := Length(Text) - StartPos;    FoundAt := FindText(strFindText, StartPos,
                   ToEnd, [stMatchCase]);
        if FoundAt <> -1 then
        begin
          SetFocus;
          SelStart := FoundAt;
          SelLength := Length(strFindText);
        end;
      end;
    end;procedure TMainForm.WMDrawClip(var Message:TMessage);
    begin
       SendMessage(hwndNextClip,Message.Msg,Message.WParam ,Message.LParam );
       if bCanPaste then
         if Boolean(ClipBoard.HasFormat(CF_TEXT) or
           ClipBoard.HasFormat(CF_OEMTEXT))then
           begin
             ClipImage.Visible := False;
             Edit1.Visible := True;
             Memo1.Text := '';
             Memo1.PasteFromClipboard;
             Edit1.Text := Memo1.Text;         ListArray[CountPointer].ReType:=False;   //Set count tpye to text
             ListArray[CountPointer].Count:=TextString.Items.Count;
             TextString.items.Add(Memo1.Text);         CountPointer:=CountPointer+1;
             ListBox1.Items.Add(Memo1.Lines.Strings[0]);
           end;
         if Boolean(ClipBoard.HasFormat(CF_BITMAP))then
           begin
             ClipImage.Visible := True;
             Edit1.Visible := False;
             BitMapArr[ImageCount]:=TBitMap.Create;
             BitMapArr[ImageCount].LoadFromClipboardFormat(CF_BITMAP,
                     ClipBoard.GetAsHandle(CF_BITMAP),0);
             ClipImage.Picture.Graphic := BitMapArr[ImageCount];         ListBox1.Items.Add('位图');
             ListArray[CountPointer].ReType:=True;   //Set count tpye to Image
             ListArray[CountPointer].Count:=ImageCount;
             CountPointer:=CountPointer+1;
             ImageCount:=ImageCount+1;
           end;
      ClipBoard.Clear;
    end;procedure TMainForm.WMHotKey (var Message:TMessage);
    begin
       hwndCurrent:=GetForegroundWindow;
       MainForm.Visible := True;
       ShowWindow(MainForm.Handle,SW_SHOWNOACTIVATE);
    end;
      

  3.   

    procedure TMainForm.WMBarIcon (var Message:TMessage);
    var
       CurRect:TPoint;
       MyRect:PRect;
       MlpData:PNotifyIconData;
    begin
       if (Message.LParam = WM_RBUTTONDOWN) then
       begin
          MyRect:=New(PRect);
          GetCursorPos(CurRect);
          PopBarMenu.Popup(CurRect.x-5,CurRect.y);
          dispose(MyRect);
       end;
       if (Message.LParam = WM_LBUTTONDOWN) then
       begin
         if bEnabled then
           begin     //Set disabled the clipboard watch
             bEnabled:=False;
             ChangeClipboardChain(MainForm.Handle,hwndNextClip);
             SendMessage(hwndNextClip,WM_CHANGECBCHAIN,MainForm.Handle ,hwndNextClip);
             MlpData := new(PNotifyIconDataA);
             MlpData.cbSize := 88;
             MlpData.Wnd := MainForm.Handle;
             MlpData.hIcon := ImageDisable.Picture.Icon.Handle;
             MlpData.uCallbackMessage := WM_ICONMESSAGE;
             MlpData.uID :=0;
             MlpData.szTip := '剪贴版监视';
             MlpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;         Shell_NotifyIcon(NIM_MODIFY,MlpData);
             dispose(MlpData);
           end
         else       //Set enabled the clipboard watch
           begin
             bEnabled:=True;
             hwndNextClip:=SetClipboardViewer(MainForm.Handle);
             MlpData := new(PNotifyIconDataA);
             MlpData.cbSize := 88;
             MlpData.Wnd := MainForm.Handle;
             MlpData.hIcon := ImageEnabled.Picture.Icon.Handle;
             MlpData.uCallbackMessage := WM_ICONMESSAGE;
             MlpData.uID :=0;
             MlpData.szTip := '剪贴版监视';
             MlpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;         Shell_NotifyIcon(NIM_MODIFY,MlpData);
             dispose(MlpData);
           end;
       end;
    end;procedure TMainForm.WMSysCommand(var Message:TMessage);
    begin
      if (Message.WParam = SC_MINIMIZE)then
        MainForm.Hide
      else
        DefWindowProc(MainForm.Handle,Message.Msg,Message.WParam,Message.LParam);
    end;procedure TMainForm.FormCreate(Sender: TObject);
    var
       MlpData:PNotifyIconData;
    begin
       MlpData := new(PNotifyIconDataA);
       MlpData.cbSize := 88;
       MlpData.Wnd := MainForm.Handle;
       MlpData.hIcon := MainForm.Icon.Handle;
       MlpData.uCallbackMessage := WM_ICONMESSAGE;
       MlpData.uID :=0;
       MlpData.szTip := '剪贴版监视';
       MlpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;   Shell_NotifyIcon(NIM_ADD,MlpData);
       dispose(MlpData);   RegisterHotKey(MainForm.handle,0,MOD_ALT or MOD_CONTROL or MOD_SHIFT,65);
       ClipBoard.Clear;
       hwndNextClip:=SetClipboardViewer(MainForm.Handle);
       bCanPaste:=True;
       bEnabled:=True;
       Application.OnHint := ShowHint;
    end;procedure TMainForm.ShowHint(Sender: TObject);
    begin
      StatusLine.SimpleText := Application.Hint;
    end;procedure TMainForm.FileSave(Sender: TObject);
    begin
       if ClipImage.Visible then
         MPopISaveToFileClick(Sender)
       else
         MPopTSaveToFileClick(Sender);
    end;procedure TMainForm.FilePrint(Sender: TObject);
    begin
      if PrintDialog.Execute then
        if ClipImage.Visible then
        with Printer do
          begin
            BeginDoc;
            ClipImage.Canvas.Draw((PageWidth - ClipImage.Picture.graphic.Width) div 2,
                        (PageHeight - ClipImage.Picture.graphic.Height) div 2,
                        ClipImage.Picture.graphic);
            EndDoc;
          end
        else       //Text file print
          begin
            Printer.Canvas.Font := Edit1.Font;
            Printer.Canvas.TextOut(0,0,Edit1.Text);
          end;
    end;procedure TMainForm.FilePrintSetup(Sender: TObject);
    begin
      PrintSetupDialog.Execute;
    end;procedure TMainForm.FileExit(Sender: TObject);
    begin
      Close;
    end;procedure TMainForm.EditCopy(Sender: TObject);
    begin
      { Add code to perform Edit Copy }
      if ClipImage.Visible then
        MPopICopyToClipClick(Sender)
      Else
        MPopTCopyToClipClick(Sender);
    end;procedure TMainForm.FormResize(Sender: TObject);
    begin
       if MainForm.Width >30 then
       begin
         ListBox1.Height := MainForm.ClientHeight - 42;
         ClipImage.Height := MainForm.ClientHeight - 42;
         ClipImage.Width := MainForm.ClientWidth - 190;
         Edit1.Width := MainForm.ClientWidth - 190;
         Edit1.Height := MainForm.ClientHeight - 42;
       end;
    end;procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
    var
       lpData:PNotifyIconData;
    begin
       ChangeClipboardChain(MainForm.Handle,hwndNextClip);
       SendMessage(hwndNextClip,WM_CHANGECBCHAIN,MainForm.Handle ,hwndNextClip);
       lpData := new(PNotifyIconDataA);
       lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);
       lpData.Wnd := MainForm.Handle;
       lpData.hIcon := MainForm.Icon.Handle;
       lpData.uCallbackMessage := WM_ICONMESSAGE;
       lpData.uID :=0;
       lpData.szTip := '剪贴版监视';
       lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
       Shell_NotifyIcon(NIM_DELETE,lpData);
       dispose(lpData);
    end;procedure TMainForm.MPopISaveToFileClick(Sender: TObject);
    begin
      with SaveDialog do
        begin
          DefaultExt := GraphicExtension(TBitmap);
          Filter := GraphicFilter(TBitmap);
          Execute;
          if FileName<>'' then
            ClipImage.Picture.BitMap.SaveToFile(FileName);
        end;
    end;procedure TMainForm.ListBox1Click(Sender: TObject);
    begin
      if ListArray[ListBox1.ItemIndex].ReType then
      begin
        ClipImage.Picture.Graphic := BitMapArr[ImageCount-1];
        ClipImage.Visible := True;
        Edit1.Visible := False;
      end
      else
      begin
        ClipImage.Visible := False;
        Edit1.Visible := True;
        Edit1.Text := TextString.Items[ListArray[ListBox1.ItemIndex].Count];
      end;
    end;procedure TMainForm.MPopTSaveToFileClick(Sender: TObject);
    begin
      With SaveDialog do
        begin
          DefaultExt:='Txt';
          Filter := '文本文件(*.txt)|*.TXT';
          Execute;
          if FileName<>'' then
            Edit1.Lines.SaveToFile(FileName);
        end;
    end;procedure TMainForm.MPopICopyToClipClick(Sender: TObject);
    begin
      bCanPaste:=False;
      //Set image data to clipboard
      Clipboard.SetAsHandle(CF_BITMAP,ClipImage.Picture.Bitmap.Handle);
      bCanPaste:=True;
    end;procedure TMainForm.MPopTCopyToClipClick(Sender: TObject);
    begin
      bCanPaste:=False;
      //Set text data to clipboard
      Edit1.CopyToClipboard;
      bCanPaste:=True;
    end;procedure TMainForm.M_AboutClick(Sender: TObject);
    begin
      about.AboutBox.Show;
    end;procedure TMainForm.MPOpBarExitClick(Sender: TObject);
    begin
      if (MessageDlg('退出剪贴板监视吗?', mtInformation,
         [mbYes,mbNo], 0)= mrYes) then
      begin
        MainForm.Close;
      end;
    end;procedure TMainForm.MPopBarAboutClick(Sender: TObject);
    begin
      about.AboutBox.Show;
    end;procedure TMainForm.MPopBarOPenClick(Sender: TObject);
    begin
      hwndCurrent:=GetForegroundWindow;
      MainForm.Visible := True;
      ShowWindow(MainForm.Handle,SW_SHOWNOACTIVATE);
    end;procedure TMainForm.FindDialog1Find(Sender: TObject);
    begin
      //Set the FindText to global varibel----strFindText
      //And call ExitFindText to find first text
      strFindText:=FindDialog1.FindText;
      ExtFindText;
      FindDialog1.CloseDialog;
      Edit1.SetFocus;
    end;procedure TMainForm.MEditFindClick(Sender: TObject);
    begin
      FindDialog1.Execute;
    end;procedure TMainForm.MEditFindNextClick(Sender: TObject);
    begin
      ExtFindText;
    end;end.
      

  4.   

    //两种方法~~
    uses Clipbrd;//重载TEdit控件,截获WM_PASTE消息~~
    type
      TEditEx = class(TEdit)
      protected
        procedure WMPASTE(var Msg: TWMPASTE); message WM_PASTE;
      end;{ TEditEx }procedure TEditEx.WMPASTE(var Msg: TWMPASTE);
    begin
      if SameText(Clipboard.AsText, 'Shift') then Exit;
      inherited;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      with TEditEx.Create(Self) do begin
        Parent := Self;
      end;
    end;//处理TEdit.OnChange事件
    procedure TForm1.Edit1Change(Sender: TObject);
    {$J+}
    const
      vUpdating: Boolean = False;
    {$J-}
    var
      S: string;
      I: Integer;
    begin
      if vUpdating then Exit;
      vUpdating := True;
      try
        if SameText(TEdit(Sender).Text, 'Shift') then begin
          TEdit(Sender).Text := '';
        end;
      finally
        vUpdating := False;
      end;
    end;