Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with clipboard

Posted on 1999-07-03
8
Medium Priority
?
406 Views
Last Modified: 2010-04-06
I'm trying to watch the clipboard and see if the user tries to copy a text in a TMemo. I've started with this code that will be run when the clipboard is used and it should get the  content of the clipboard so I could compare with the text in TMemo, but instead it raises an exception every time it is used, what is the problem and how should I write this procedure instead?

If IsClipboardFormatAvailable(CF_TEXT) then begin
    OpenClipboard(0);
    memoryplace := GetClipboardData(CF_TEXT);
    If memoryplace <> NULL then begin
      test := GlobalLock(memoryplace);
      If test^ <> NULL then begin
        ShowMessage(test^);
        GlobalUnlock(memoryplace);
      end
    end;
    CloseClipboard;
  end
0
Comment
Question by:Myr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386098
I not sure what you are tying to do but this code tells you when the contents of the cilpboard change:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    procedure ClipboardChanged(var message: TMessage); message WM_DRAWCLIPBOARD;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ClipboardChanged(var message: TMessage);
begin
  ShowMessage(''Clipboard changed...');
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  SetClipboardViewer(Form1.Handle);
end;

end.
0
 

Author Comment

by:Myr
ID: 1386099
I know, I already have it in my program, this is some lines from the TForm1.ClipboardChanged procedure.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386100
The following example demonstrates trapping the Ctrl-V key in a TMemo component:

uses ClipBrd;

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if ((Key = ord('V')) and (ssCtrl in Shift)) then begin
    if Clipboard.HasFormat(CF_TEXT) then
      ClipBoard.Clear;
    Memo1.SelText := 'Epsylon was here';
    key := 0;
  end;
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Epsylon
ID: 1386101
And fetching text from the clipboard goes like this in Delphi:


uses ClipBrd;

var buf: array[0..255] of Char;
begin
  Clipboard.GetTextBuf(buf, sizeof(buf));
  ShowMessage(buf);
end;

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386102
Or better:

ShowMessage(ClipBoard.AsText);


0
 

Author Comment

by:Myr
ID: 1386103
Thanks it worked, hm, why didn't I look for some built in function...
Well make an answer and I'll give you the points. Do you also know if using windows commands like OpenClipboard(0) etc is faster than using the Clipboard component?
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 1386104
Thanks for the points Myr   :o)

The answer can be found below.....
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386105
I think there won't be much difference.
Since the ClipBoard global variable is there anyway I like to use that one. I guess it internally uses OpenClipboard.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question