Clipboard communication instance of VC ++

Source: Internet
Author: User

Clipboard communication instance

 

I have Excerpted from the book 21-day student VC ++.

To better understand the functions described above, the following example is provided to describe how to use the clipboard to implement inter-process communication.
[Example 17-3] clipboard implements process communication.
Create a dialog box-based application for MFC. Click file | new to open the new dialog box. Select the [MFC Appwizard [EXE] project and name it "Process Communication ". In step 1 of Appwizard, select dialog based ]. For other steps, follow the default settings and click Finish to complete the creation.
Design dialog box. Delete all the existing controls in the dialog box. Add two text boxes, one for the user to enter data, the ID number is idc_edit_send; the other is used to display the data after the data is copied, And the ID number is idc_edit_recv. Add two buttons: Copy, idc_btn_copy, and paste. Set idc_btn_paste to idc_btn_paste. Dialog Box design result.
Add a message response function. Double-click the two buttons in the dialog box to add the onbtncopy and onbtnpaste functions of the two buttons to the cmydlg class.
Tip: Add a message Response Function in the MFC classwizard tool. Select the corresponding button control and add its command message function.
Sample Code 17-3
01 void cmydlg: onbtncopy () // copy button
02 {
03 // todo: add your control notification handler code here
04 cstring STR;
05 // define the reserved variable to save user input data
06 getdlgitemtext (idc_edit_send, STR );
07 hglobal hclip;
08 // define an hglobal handle variable to point to the allocated memory block
09 If (openclipboard ())
10 {
11 emptyclipboard (); // clear the clipboard content
12 hclip = globalalloc (gmem_moveable, str. getlength () + 1 );
13 // allocate removable memory blocks on the stack, and the program returns a memory handle
14 char * Buff; // defines the pointer variable pointing to the bytes type
15 buff = (char *) globallock (hclip );
16 // lock the allocated memory block, convert the memory block handle into a pointer, and Add 1
17 strcpy (buff, STR );
18 // copy the user input data to the pointer variable, which is actually copied to the allocated memory block
19 globalunlock (hclip );
20 // after the data is written, unlock the data and reduce the number of the referenced counter by 1.
21 setclipboarddata (cf_text, hclip );
22 // put the memory block containing data into the resource management of the clipboard
23 closeclipboard ();
24 // close the clipboard and release the occupied resources of the clipboard
25 MessageBox ("data has been saved to the Clipboard ");
26}
27}
28 void cmydlg: onbtnpaste () // paste button
29 {
30 // todo: add your control notification handler code here
31 cstring STR;
32 // keep the data copied from the clipboard
33 handle hclip;
34 // define the handle type variable to point to the handle returned by the getclipboarddata Function
35 if (openclipboard ())
36 {
37 hclip = getclipboarddata (cf_text );
38 // retrieve a memory handle from the clipboard
39 char * Buff;
40 // define the memory pointer variable to save data in the memory block
41 buff = (char *) globallock (hclip );
42 // lock the memory block, convert the memory handle value to a pointer, and add a reference counter to the memory block. The data in the memory // is also returned to the pointer variable.
43 STR = Buff;
44 // Save the data to the variable type
45 globalunlock (hclip );
46 // subtract one from the reference counter of the memory block
47 closeclipboard ();
48 // close the clipboard and release the occupied resources of the clipboard
49 setdlgitemtext (idc_edit_recv, STR );
50}
51}
[Running result] there is no error after compiling in Visual C ++ 6.0. Use the shortcut key Ctrl + F5 to run the project. Enter the string "21 days Learning Visual C ++" in the edit box on the left ". Click Copy to save the string to the clipboard.
Note: The clipboard operation function is used in the above Code. In addition, the strcpy () function is also used to copy the input string to the memory.
Click copy in, and then click paste. Extract the strings stored in the clipboard and display them in the edit box on the right, as shown in 17-18.
 
Figure 17-18 "Paste" button running result
[Code parsing] from the above code, we can see that whether to put the memory block into the clipboard resource management, or to retrieve the memory block from the clipboard, it must be locked and unlocked. This is to facilitate the conversion of handle-type variables and memory-type pointer variables and implement data writing and reading. In the above Code, the execution process of the copy and paste buttons is 17-19 and Figure 17-20.
So far, an instance that implements process communication through the clipboard is complete. In fact, the functions implemented in this example can be easily completed without the clipboard. This is just to demonstrate the clipboard usage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.