The surfing age, people who have used the network ants are certainly a lot, one of which monitors the change of the clipboard changes a lot of considerable consideration. This article will introduce how to use "thought board" in Delphi to enrich our software function.
Windows uses a clipboard viewer and observation chain. The clipboard viewer is a window that displays the current content of the clipboard. Usually it should be at least three common format: text cf_text, bitmap cf_bitmap, meta file CF_MetafilePict. The clipboard observation chain is a series of independently clipboard viewing windows, which can accept the content currently sent to the clipboard. We will process the contents of the clipboard in generally in the following steps in the window.
First, add an observation window to the clipboard observation chain using the SetClipboardViewer (HWND) function. When the content of the clipboard changes, the window will receive a WM_DRAWCLIPBOARD message. The parameter that the function needs to be passed is the handle of the viewing window. The return value is also a window handle type, identifies the next window to be added.
Then, in response to the change of the contents of the clipboard content in response to the WM_DRAWCLIPBOARD message.
Finally, you need to call the ChangeClipboardChain function when you exit or close it to remove yourself from the observer. Then call the SendMessage function to pass these messages to the next observation window in the observation chain. The function ChangeClipboardchain prototype is as follows:
Bool Changeclipboardchain
HWND HWNDREMOVE, // Handle of the window to be deleted
HWND HWNDNEWNEXT / / Observer Handle of the next window in the chain
);
A class TclipBoard defines a class TclipBoard in Delphi, which encapsulates a Windows clipboard to simplify a large number of complex processing processes. We can call the global function clipboard directly, which is used to return the Tclipboard object instance, using this instance to cut, copy, and paste the clipboard. Below is a brief introduction to several common methods and properties of the Tclipboard object.
method:
Assign: Place the specified object into the clipboard.
Open: Open the clipboard to prevent other programs from overwriting the clipboard. It is especially useful when adding multiple data to the clipboard.
Close: Close the clipboard. Should be used in pairs with the open clipboard.
Clear: Clear the clipboard.
GetasHandle: Returns the handle of the specified format data in the clipboard. The clipboard must be opened before use.
GetComponent: Returns a control in the clipboard. Delphi is much better.
HASFORMAT: Query the content in the specified format in the clipboard.
Attributes:
ASTEXT: Used to read and write clipboard text content.
FormatCount: Read the number of data formats in the clipboard.
FORMATS: Returns a list of various formats in the clipboard.
In addition, many controls in Delphi also encapsulates the operation of the clipboard processing. A simple example of a clipboard is processed below, just displaying the text content of the clipboard as a window title.
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Classes,
Graphics, Controls, Forms, Dialogs,
CLIPBRD; / / Add CLIPBRD unit
Type
TFORM1 = Class (TFORM)
Procedure formcreate (Sender: TOBJECT);
Procedure formclose (Sender: TOBJECT)
VAR action: tclosection;
Private
{Private Declarations}
public
NextCliphWnd: hwnd; // Observer in the next window handle
Procedure WmdrawClipboard
VAR AMESSAGE: TMESSAGE
Message wm_drawclipboard;
/ / Treat a WM_DRAWCLIPBOARD message process
END;
VAR
FORM1: TFORM1;
IMPLEMENTATION
{$ R * .dfm}
{TFORM1}
Procedure TFORM1.WMDRAWCLIP
Board (var Amessage: TMESSAGE);
Begin
/ / WM_DRAWCLIPBOARD
Message to the window in the next observation chain
SendMessage (NextCliphWnd, Amessage.
MSG, AMessage.wparam, AMessage.lparam;
/ / Query the data content of a specific format in the clipboard
IF (clipboard.hasformat) or
Clipboard.hasformat (CF_OEMText)) THEN
Begin
// Process the contents of the clipboard
Caption: = clipboard.astext;
END;
END;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
// Get the next window handle in the observation chain
NextCliphWnd: = setClipboardViewer (Handle);
END;
Procedure TFORM1.FORMCLOSE
Sender: TOBJECT; VAR ACTION: TCLOSEAction;
Begin
/ / Remove this observation window from the observation chain
Changeclipboardchain (Handle, NextCliphwnd);
/ / WM_DRAWCLIPBOARD
Message to the window in the next observation chain
SendMessage (NextCliphWnd, WM_
Changecbchain, Handle, NextCliphwnd;
END;
End.
It should be noted that the next window that uses the function sendMEssage to transfer the WM_DRAWCLIPBOARD in the process of processing the clipboard content change, and the next window is passed to the WM_DRAWCLIPBOARD or WM_CHANGECBCHAIN message to the next window in the observation chain. Other Window cannot be obtained. Similar news. In addition, the author has encountered the case where the clipboard format is used when using the network ant, and the reader will pay attention to the return value of functions such as REGISTERCLIPBOARDFORMAT.