Windows programming clipboard mechanism

xiaoxiao2021-03-06  48

Windows programming clipboard mechanism

Tianji Network 2004-9-21 9:51:00 / Lang Rui Summary: This article is in-depth, comprehensive elaboration for Windows clipboard mechanisms, including text, bitmap, DSP, custom format clipboard usage And multi-data items and delay submission techniques. Key words: VC 6.0; clipboard mechanism; data format The Basic Mechanism of the Windows System supports the clipboard IPC is a global shared memory that is reserved by the system, which is used to temporarily exchange data to exchange between the processes: Provide data to create a global memory block, and move the data to be transferred or Copy to the memory block; the process of accepting data (or the process itself providing data itself) obtains the handle of this memory block and completes reading of the memory block data. In order to make this IPC mechanism of the clipboard more perfect and easy to use, you need to solve the following three problems: Providing data processes at the end, the Windows system will delete the global memory block created, and the process of accepting data is hoped After exiting the data in the clipboard still exists, you can continue to get to other processes; convenient manages and transfer the clipboard data handle; convenient setting and determination of the clipboard data format. In order to improve the above functions, Windows provides a set of API functions, messages, and predefined data formats existing in user32.dll, and manages the clipboard data exchange between the processes by using the use of these functions. The Windows system provides a set of API functions and a variety of messages for the clipboard, which can basically meet the needs of programming. And Windows also predefines a variety of data formats for clipboards. Through these predefined formats, the receiver can reproduce the data provider placed in the data content in the clipboard. The use of the text clipboard and bitmap shear plates is more commonly used. The text clipboard is a clipboard containing a string with format CF_Text, one of the most frequently used clipboards. The data transmitted in the text clipboard is an ASCII character without any format information. To transfer text to a clipboard, you can allocate a mobile global memory block, then write the text content to be copied to this memory area. Finally call the clipboard function to place the data to the clipboard:

DWORD dwlength = 100; // To copy the string length Handle HglobalMemory = Globalalloc (GHND, DWLENGTH 1); // Assign Memory LPBYTE LPGLOBALMORY = (lpbyte) Globalock (HglobalMemory); // Lock Memory for (INT i = 0 i

HWND HWND = getSafehWnd (); // Get Safety Window Handle :: OpenClipboard (HWND); // Open Clipboard Handle HbitMap = :: getClipboardData (cf_bitmap); // Get Clipboard Data Handle HDC HDC = :: Getdc (HWnd ); // Get the device environment handle HDC HDCMEM = CREATECOMPALEDC (HDC); // Create a memory-related memory environment SelectObject (HDCMEM, HBitmap); // Select Object SetMAPMODE (HDCMEM, GetMapMode (HDC)); // Setting Mapping Mode Bitmap BM; // Get bitmap object GetObject (Hbitmap, Sizeof (Bitmap), & B); Bitblt (HDC, 0, 0, BM.BMWIDTH, BM.BMHEIGHT, HDCMEM, 0, 0, SRCCOPY); // Diagram Copy: ReleaseDC (HWND, HDC); // Release Device Environment Handle DELETEDC (HDCMEM); // Delete Memory Environment :: CloseClipboard (); // Close Clipboard Multi-data Items and Delay Submit Technology To put data in Clipboard, you must call the EMPTYCLIPBOARD () function after opening the clipboard to clear the contents of the current clipboard without adding new data items based on the original data items. However, you can call the setClipboardData () function multiple times in the EmptyClipboard () and CloseClipboard () calls to place multiple different formatted data items. For example: OpenClipboard (hWnd); EmptyClipboardData (); SetClipboardData (CF_TEXT, hGMemText); SetClipboardData (CF_BITMAP, hBitmap); CloseClipboard (); if the case or the like CF_TEXT CF_BITMAP formatting tags to call IsClipboardFormatAvailable () will returns TRUE, indicates that The data in these formats also exists in the clipboard. The getClipboardData () function can be used to obtain the corresponding data handle with different format marks. For the clipboard data of the multi-data item, you can also get the number of data formats and specific data formats present in the current clipboard with the countClipboardFormats () and the EnumClipboardFormats () function. EnumclipboardFormats () Function prototype:

UINT EnumClipboardFormats (uint format); Parameters format specifies the data format of the clipboard. If successful execution will return the next data format value of format specified by Format, if format is the final data format, then 0 will be returned. This is not difficult to write block code for all format data items in the clipboard:

UINT FORMAT = 0; // From the first format value to enumerate OpenClipboard (HWND); while (Format = EnumClipboardFormats (Format) {... // Treatment of related formatted data} CloseClipboard (); provide processes in data After creating the clipboard data, these data have to occupy the memory space until there are other processes to get the clipboard data. If the amount of data placed in the clipboard is too large, it will waste memory space to reduce the utilization of resources. To avoid this waste, you can take a delayed rendering technology, which is created by the data providing process to create an empty (NULL) clipboard data block specified in the data format until there are other processes that require data or their own processes to terminate the run. Really submit data. The implementation of the delay is not complex, and only the clipboard owner process is called to set the data handle parameter to NULL in calling setClipboardData (). The main job that the delayed owner process needs to do is the process of delaying the submission messages for clipboards such as WM_RENDERFORMAT, WM_DESTORYCLIPBOARD and WM_RENDERALLFORMATS. When another process calls the getClipboardData () function, the system will send a WM_RENDERFORMAT message to the delayed submitting data. The clipboard owner process should use the corresponding format and the actual data handle to call the setClipboardData () function in the response function of this message, but no need to call OpenClipboard () and EMPTYCLIPBOARD () to open and empty the clipboard. There is no need to call CloseClipboard () to close the clipboard. If other processes open the clipboard and call the EmptyClipboard () function to clear the contents of the clipboard, when the ownership of the clipboard, the system will send a WM_DESTROYCLIPBOARD message to the delayed clipboard owner process to inform the process to have the clipboard. Loss of rights. The process that lost the clipboard ownership will not submit data to the clipboard after receiving the message. In addition, this message will also be received after the delay submitting process is submitted after the data to be submitted. If the delay submitting the clipboard owner process will terminate, the system will send a WM_RENDERLFORMATS message to it, inform it to open and clear the clipboard content. After calling setClipboardData () Sets each data handle and close the clipboard. The following segment will complete the delay of the data, the WM_RenderFormat message response function onRenderFormat () does not execute immediately, and when the process calls the getClipboardData () function reads the data from the clipboard, it will be issued. Complete submission of data in the message processing function: Late submission: hwnd hwnd = getsafehwnd (); // Get security window handle :: OpenClipboard (hwnd); // Open Clipboard :: EmptyClipboard (); // Empty Clipart Board: SetClipboardData (cf_text, null); // Delayed submission for clipboard data: CloseClipboard (); // Turns off the clipboard in the response function of the WM_RenderFormat message:

DWORD DWLENGTH = 100; // To copy the string length Handle HglobalMemory = GlobalLoc (GHND, DWLENGTH 1); // Assign Inclusion Block LPBYTE LPGLOBALMORY = (LPBYTE) Globalock (HglobalMemory); // Lock Core Block For (INT I = 0; i

Uint format = registerclipboardFormat (LPSZFORMAT); the use of the format identifier value returned to this is the same as the system predefined format identifier. You can get the ASCII name of the custom format through the getClipboardFormatName () function. Summary This article mainly made a more in-depth discussion on the clipboard mechanism in Windows programming, and has made important techniques such as the usage of text, bitmaps, DSPs, and custom data formats, and multi-data items and delay submission. Elaborate. The specific program sample code is given to enable the reader to better grasp the use of the clipboard mechanism.

转载请注明原文地址:https://www.9cbs.com/read-79547.html

New Post(0)