Author: China Radio Wave Communication Research Institute (Qingdao branch) Lang Rui time: Source: Skynet
Abstract:This article provides an in-depth and comprehensive description of the Windows clipboard machine, including the use of text, bitmap, DSP, custom format clipboard, and multi-data items and delay submission technology.
Keywords:VC ++ 6.0; clipboard mechanism; data format; delayed submission
Windows clipboard
Windows clipboard is a relatively simple and relatively low-cost IPC (interprocess communication) mechanism. The basic mechanism for Windows to support the clipboard IPC is a global shared memory reserved by the system, used to store data exchanged between processes: the process that provides data creates a global memory block, the data to be transferred is moved to or copied to the memory block. The process that receives the data (or the process that provides the data) obtains the handle of the memory block, and read the data in the memory block.
To make the IPC Mechanism of the clipboard more complete and easy to use, you need to solve the following three problems: when the process providing data ends, Windows will delete the global memory block it created, the process that receives the data wants the data in the clipboard to exist after it exits, and can continue to be obtained by other processes; it can conveniently manage and transmit the Clipboard data handle; the Clipboard data format can be easily set and determined. To improve the above functions, Windows provides a set of API functions, messages, and predefined data formats that exist in user32.dll, the Clipboard data exchange between processes is managed through the use of these functions and messages.
Windows provides a set of API functions and multiple messages for the clipboard, which can basically meet programming needs. In addition, windows also predefines multiple data formats for the clipboard. With these predefined formats, the receiver can correctly reproduce the data content that the data provider places on the clipboard.
Use of text clipboard and bitmap clipboard
These two clipboard types are commonly used. The text Clipboard contains a string with the cf_text format and is one of the most frequently used clipboard. The data transmitted in the text clipboard is ASCII characters without any format information. To transfer text to the clipboard, You can first allocate a removable global memory block and write the text to be copied to this memory area. Finally, call the clipboard function to place the data to the clipboard:
DWORD dwlength = 100; // string length to be copied Handle hglobalmemory = globalalloc (ghnd, dwlength + 1); // allocate memory Lpbyte lpglobalmemory = (lpbyte) globallock (hglobalmemory); // lock the memory For (INT I = 0; I <dwlength; I ++) // copy "*" to the global memory block * Lpglobalmemory ++ = '*'; Globalunlock (hglobalmemory); // lock the memory block to unlock Hwnd = getsafehwnd (); // obtain the security window handle : Openclipboard (hwnd); // open the clipboard : Emptyclipboard (); // clear the clipboard : Setclipboarddata (cf_text, hglobalmemory); // place data in the memory to the clipboard. : Closeclipboard (); // close the clipboard |
Open the clipboard with openclipboard (). After emptyclipboard () is called, make the window pointed to by hwnd become the owner of the clipboard and continue until the closeclipboard () function is called. During this period, the clipboard is exclusive to the owner, and other processes cannot modify the clipboard content.
The process of obtaining text from the clipboard is similar. First, open the clipboard and obtain the data handle of the clipboard. If the data exists, copy the data to the program variable. Because the data handle obtained by getclipboarddata () belongs to the clipboard, the user program must use it before calling the closeclipboard () function:
Hwnd = getsafehwnd (); // obtain the security window handle : Openclipboard (hwnd); // open the clipboard Handle hclipmemory =: getclipboarddata (cf_text); // obtain the Clipboard data handle DWORD dwlength = globalsize (hclipmemory); // return the current size of the specified memory area Lpbyte lpclipmemory = (lpbyte) globallock (hclipmemory); // lock the memory M_smessage = cstring (lpclipmemory); // Save the obtained text data. Globalunlock (hclipmemory); // unlock memory : Closeclipboard (); // close the clipboard |
Most applications use the Clipboard data format of Bitmap for graphic data. The usage of the bitmap clipboard is similar to that of the text clipboard, except that the data format must be cf_bitmap and setclipboarddata () or getclipboarddata () the device-related bitmap handle is returned to or from the clipboard. The following sample code shows the bitmap data in the clipboard to the client area of the program:
Hwnd = getsafehwnd (); // obtain the security window handle : Openclipboard (hwnd); // open the clipboard Handle hbitmap =: getclipboarddata (cf_bitmap); // obtain the Clipboard data handle HDC =: getdc (hwnd); // obtain the device environment handle HDC hdcmem = createcompatibledc (HDC); // create a device-related memory Environment SelectObject (hdcmem, hbitmap); // select an object Setmapmode (hdcmem, getmapmode (HDC); // you can specify the ing mode. Bitmap bm; // obtain the bitmap object. GetObject (hbitmap, sizeof (Bitmap), & BM ); Bitblt (HDC, 0, 0, BM. bmwidth, BM. bmheight, hdcmem, 0, 0, srccopy); // bitmap copy : Releasedc (hwnd, HDC); // release the device environment handle Deletedc (hdcmem); // deletes the memory environment. : Closeclipboard (); // close the clipboard |
Multi-data item and delayed submission Technology
To put data into the clipboard, you must call the emptyclipboard () function to clear the content in the current clipboard, instead of appending new data items on the basis of the original data items. However, you can call the setclipboarddata () function multiple times between emptyclipboard () and closeclipboard () to place multiple data items in different formats. For example:
Openclipboard (hwnd ); Emptyclipboarddata (); Setclipboarddata (cf_text, hgmemtext ); Setclipboarddata (cf_bitmap, hbitmap ); Closeclipboard (); |
In this case, if isclipboardformatavailable () is called with the cf_text or cf_bitmap format mark, true is returned, indicating that the data in these formats exist in the clipboard at the same time. Call the getclipboarddata () function in different formats to obtain the corresponding data handle.
For the Clipboard data of multiple data items, you can also use the countclipboardformats () and enumclipboardformats () functions to obtain the number of data formats and specific data formats in the current clipboard. The function prototype of enumclipboardformats () is:
Uint enumclipboardformats (uint format ); |
The format parameter specifies the data format of the clipboard. If the execution is successful, the next data format value in the format specified by format is returned. If the format is the last data format value, 0 is returned. It is not difficult to write the code segment for processing all the format data items in the clipboard:
Uint format = 0; // starts from the first format Value Openclipboard (hwnd ); While (format = enumclipboardformats (Format )) { ...... // Process related format data } Closeclipboard (); |
After the data provider creates the Clipboard data, the data occupies the memory until other processes obtain the Clipboard data. If the amount of data placed on the clipboard is too large, the memory space will be wasted and the resource utilization will be reduced. To avoid this waste, you can adopt the delayed rendering technology, that is, the data provider process first creates an empty (null) Clipboard data block in the specified data format, data is submitted only when other processes need data or their own processes need to terminate the operation.
The implementation of delayed submission is not complex. You only need to set the data handle parameter to null when the clipboard owner process calls setclipboarddata. The owner process of delayed submission must process the delayed submission messages of the clipboard, such as wm_renderformat, wm_destoryclipboard, and wm_renderallformats.
When another process calls the getclipboarddata () function, the system sends the wm_renderformat message to the clipboard owner process that delays data submission. The clipboard owner process should use the corresponding format and actual data handle to call the setclipboarddata () function in the response function of this message, but it does not need to call openclipboard () and emptyclipboard () open and clear the clipboard. You do not need to call closeclipboard () to close the clipboard after setting the data. If other processes open the clipboard and call the emptyclipboard () function to clear the clipboard content, the system will send the wm_destroyclipboard message to the delayed submitted clipboard owner process when taking over the ownership of the clipboard, to notify the process of the loss of the clipboard ownership. The process that loses the clipboard ownership will not submit data to the clipboard after receiving the message. In addition, the delayed submission process also receives the message after it submits all the data to be submitted. If the process of submitting the clipboard owner is delayed, the system will send a wm_renderallformats message for it to notify it to open and clear the clipboard content. Close the clipboard after you call setclipboarddata () to set data handles.
The following code completes the delayed data submission. The message response function onrenderformat () of wm_renderformat is not executed immediately. When a process calls getclipboarddata () this message is sent only when the function reads data from the clipboard. Complete data submission in the message processing function:
Delayed submission:
Hwnd = getsafehwnd (); // obtain the security window handle : Openclipboard (hwnd); // open the clipboard : Emptyclipboard (); // clear the clipboard : Setclipboarddata (cf_text, null); // delayed submission of Clipboard data : Closeclipboard (); // close the clipboard |
In the response function of the wm_renderformat message:
DWORD dwlength = 100; // string length to be copied Handle hglobalmemory = globalalloc (ghnd, dwlength + 1); // allocate memory blocks Lpbyte lpglobalmemory = (lpbyte) globallock (hglobalmemory); // lock the memory block For (INT I = 0; I <dwlength; I ++) // copy "*" to the global memory block * Lpglobalmemory ++ = '*'; Globalunlock (hglobalmemory); // lock the memory block to unlock : Setclipboarddata (cf_text, hglobalmemory); // place data in the memory to the clipboard. |
Use of DSP and custom data formats
In Windows, three data formats with the "DSP" prefix are predefined: cf_dsptext, cf_dspbitmap, and cf_dspmetafilepict. This is a pseudo-standard format used to indicate the private Clipboard data format defined in the program. These formats are different for different programs. Therefore, these formats are meaningful only for different instances of a specific program.
To use the DSP data format, make sure that the process itself and the clipboard owner process belong to the same program. You can call the getclipboardowner () function to obtain the window handle of the clipboard owner and call getclassname () to obtain the window class name:
Hwnd hclipowner = getclipboardowner (); Getclassname (hclipowner, & classname, 255 ); |
If the name of the clipboard owner's window class is the same as that of the current process, you can use the Clipboard data format with the DSP prefix.
In addition to the Windows predefined Clipboard data format, you can also use a custom data format in the program. For the Custom Data Format lpszformat, you can call the registerclipboardformat () function to register and obtain the returned format id value:
Uint format = registerclipboardformat (lpszformat ); |
The format Identifier value returned is the same as the predefined format identifier. You can use the getclipboardformatname () function to obtain the customized ASCII name.
Summary
This article mainly discusses how to create a clipboard in Windows programming, this section describes the commonly used text, bitmap, DSP, and custom data formats, as well as important technologies such as multiple data items and delayed submission. The program sample code is provided to help readers better understand the use of the clipboard mechanism.