To solve these problems, I developed the following program. When the program starts, it runs in a minimized manner. At this point, whenever the Clipboard is stored in a bitmap, a dialog box automatically pops up asking the user to save it. If the user wants to see the confirmation, you can double-click the Run program icon to select the button and the bitmap on the Clipboard will appear on the screen.
The key attributes of the part are designed as follows:
Clipsaveform:
Caption= ' Save Bitmap in Clipboard '
Panel1:
Align = ' top '
Image1:
Align = ' Client '
SAVEDIALOG1:
Fileeditstyle = Fsedit
FileName = ' *.bmp '
Filter = ' Bitmap Files (*.bmp) |*.bmp| Any Files (*.*) |*.* '
Initialdir = ' C:\bmp '
Title = ' Save Bitmap '
The Program main window is an instance of the Tform derived class Tclipsaveform. Tclipsaveform makes it possible to respond to and handle windows by defining some private data members and procedures. The following is the class definition for Tclipsaveform:
Type
Tclipsaveform = Class (Tform)
Savedialog1:tsavedialog;
Image1:timage;
Panel1:tpanel;
Button1:tbutton;
Speedbutton1:tspeedbutton;
Speedbutton2:tspeedbutton;
Button2:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Speedbutton1click (Sender:tobject);
Procedure Speedbutton2click (Sender:tobject);
Private
{Private declarations}
Mybitmap:tbitmap; {Save intercepted Bitmap}
View:boolean; {Decide whether to show}
Nextviewerhandle:hwnd; {The handle of the next Clipboard Viewer}
Procedure Wmdrawclipboard (var msg:twmdrawclipboard);
Message Wm_drawclipboard;
Procedure Wmchangecbchain (var msg:twmchangecbchain);
Message Wm_changecbchain;
{Responding to Windows Clipboard messages}
Public
{Public declarations}
End
When the window is created, log the window into the Clipboard Viewer, add it to the Clipboard Viewer chain, and initialize the variables, parts, and clipboard.
Procedure Tclipsaveform.formcreate (Sender:tobject);
Begin
View: = False;
Speedbutton2.down: = True;
Mybitmap: = tbitmap.create;
Try
Mybitmap.width: = 0;
Mybitmap.height: = 0;
Except
Application.terminate;
End
Clipboard.clear;
Nextviewerhandle: = SetClipboardViewer (Handle);
End
When the window closes, exit the Clipboard viewer chain and free memory:
Procedure Tclipsaveform.formdestroy (Sender:tobject);
Begin
Changeclipboardchain (Handle,nextviewerhandle);
Mybitmap.free;
End
The two Windows API functions SetClipboardViewer and Changeclipboardchain used in both of these procedures are for logging in and exiting the Clipboard viewer chain, respectively.
The function of the program to save the bitmap is implemented in the message response process Wmdrawclipboard. This procedure is invoked when the contents of the Clipboard have changed.
Procedure Tclipsaveform.wmdrawclipboard (var msg:twmdrawclipboard);
Var
filename:string;
Begin
If nextviewerhandle <> 0 Then
SendMessage (nextviewerhandle,msg. msg,0,0);
If Clipboard.hasformat (Cf_bitmap) Then
Begin
Mybitmap.assign (Clipboard);
If Savedialog1.execute Then
Begin
FileName: = Savedialog1.filename;
Mybitmap.savetofile (FileName);
End
If View Then
Begin
WindowState: = Wsnormal;
Image1.Picture.Bitmap: = Mybitmap;
End
End
Msg.result: = 0;
End
The program first determines whether the next observer exists in the Clipboard viewer chain. If so, pass the message down, which is the obligation of the Clipboard Viewer program. It then determines whether the content on the Clipboard is formatted as a bitmap. If so, first save the contents of the Clipboard to the data member Mybitmap and activate a File Save dialog box to save the bitmap in the file. If view=true, the window state (WindowState) is set to Wsnormal and the Mybitmap is assigned to the corresponding value of the image part so that the user can observe the bitmap on the Clipboard.