Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
489 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Monitoring the ClipboardFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
11/23/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet WM_CHANGECBCHAIN WM_DRAWCLIPBOARD clipboard Chain cut copy paste SetClipboardViewer
Times Scored:
6
Visits:
7565
Uploader: Delphi Central
Company: Delphi Tutorials
Reference: Delphi Tutorials
 
Question/Problem/Abstract:
Explains how to hook into the clipboard change, and be notified when it changes.
Answer:



Introduction

In this article I intend explain how to enable your application to join the chain of clipboard viewers, thus receiving an appropiate messages whenever the contents of the clipboard change. There are several uses to this facility, the main two being:

A clipboard monitor - This is an application that continually watches the clipboard, and stores any information passed to it
Enabling and Disabling of menu items - This is used in conjunction with the paste menu item (and toolbar buttons) to enable it whenever the clipboard contains a format that your application can paste.
I am sure that there are other uses for this facility but off hand I can not think of any.

Steps Involved

There are four main steps involved in using the clipboard chain, these are:

Joining the clipboard chain
Responding to and passing on messages when the clipboard contents change
Responding to messages when other members are added to or removed from the chain
Removing your application from the chain when it closes.
I will explain how to implement each of these stages in the following code section.



Writing the Code

The code can be crisply divided into the four steps previously mentioned, the extra declarations that needed are:

    procedure WMDRAWCLIPBOARD(var Message: TMessage); message WM_DRAWCLIPBOARD;
    procedure WMCHANGECBCHAIN(var Message: TMessage); message WM_CHANGECBCHAIN;
These are the messages that we will receive and respond too when the clipboard or clipboard chain is modified.
We also need to store the handle of the next window in the chain (the reasons for this will be explained later):
    NextHandle: THandle;
We will now fill in the code to handle these messages, and also the code for joining and leaving the chain.


Joining the Clipboard Chain

This is normally done in the onCreate procedure of the main form in the application. All we need to do here is add the following code:

  NextHandle := SetClipboardViewer(handle);
What we are doing here is adding our handle to the clipboard chain, the returned handle is the handle of the next viewer in the chain which we will need to use later.

Responding when the Clipboards Contents Change

Whenever the contents of the clipboard changes we now receive a WM_DRAWCLIPBOARD message. In the procedure which we previously declared we can now respond to clipboard changes. We also must pass on the message that we received to the next handle in the chain, as illustrated by the following code:

procedure TForm1.WMDRAWCLIPBOARD(var Message: TMessage);
begin
  { Add code here to respond to the change }
  sendmessage(NextHandle,WM_DRAWCLIPBOARD,0,0);
end;


Responding when the Chain Changes

We also receive a message whenever a handle is removed from the chain. The message we receive provides us with the handle being removed and also with the handle that imediatly follows the one being removed. In response to this we must modify our next handle property (if its being removed) and pass the message along to the next handle in the chain. This is accomplished by the following code:
procedure TForm1.WMCHANGECBCHAIN(var Message: TMessage);
begin
  if Message.WParam = NextHandle then
  begin
    NextHandle := Message.LParam;
  end
  else
  begin
    sendmessage(NextHandle,
                WM_CHANGECBCHAIN,
                Message.WParam,  // handle of window to remove  
                Message.LParam); // handle of next window
  end;
end;


Leaving the Clipboard Chain

This is normally done in the onDestroy procedure of the main form in the application. All we are doing here is removing the application from the chain. It is done by the following code:

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChangeClipboardChain(Handle,      // our handle to remove
                       NextHandle ); // handle of next window in the chain
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Wischnewski
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)