The C ++ programming language can be used in practical programming applications in a flexible manner to help us achieve various functional requirements. Here we will be fully aware of the specific implementation of the C ++ operation clipboard. I hope to help you.
- How to use C ++ Endian
- How to use C ++ switch-case statements
- How To Connect C ++ to MySqL database
- Analysis of basic application methods of C ++ youyuan Functions
- Usage of C ++ Constructor
The clipboard is embedded in windows and uses the system's internal resource RAM or virtual memory to temporarily save cut and copy information. There are various types of information that can be stored. The information on the clipboard is saved during cropping or copying. The clipboard content can be updated or cleared only when another information is cut or copied, or when a power failure, windows exit, or intentionally cleared, that is, you can copy the clipboard once and paste it multiple times.
The following describes how to operate the clipboard in C ++, mainly writing data and obtaining data. operations on the clipboard can be seen as a way of inter-process communication.
1. In VC ++ 6.0 ~ 9.0) create a dialog box-based MFC project ClipboardTest
2. Add two editing controls: IDC_EDIT_SEND and IDC_EDIT_RECV) and two buttons: IDC_BTN_SEND and IDC_BTN_RECV)
3. Add the code for writing data to the clipboard for IDC_BTN_SEND
- If (OpenClipboard () // open the clipboard
- {
- CString str;
- HANDLE hClip;
- Char * pBuf;
- EmptyClipboard (); // clear the clipboard
- GetDlgItemText (IDC_EDIT_SEND, str); // obtain data in IDC_EDIT_SEND
- // Write data
- HClip = GlobalAlloc (GMEM_MOVEABLE, str. GetLength () + 1 );
- PBuf = (char *) GlobalLock (hClip );
- Strcpy (pBuf, str );
- GlobalUnlock (hClip); // unlock
- SetClipboardData (CF_TEXT, hClip); // set the format
- // Close the clipboard
- CloseClipboard ();
- }
4. Add the code to read the Clipboard data for IDC_BTN_RECV.
- If (OpenClipboard () // open the clipboard
- {
- If (IsClipboardFormatAvailable (CF_TEXT) // determine whether the format is what we need
- {
- HANDLE hClip;
- Char * pBuf;
- // Read data
- HClip = GetClipboardData (CF_TEXT );
- PBuf = (char *) GlobalLock (hClip );
- GlobalUnlock (hClip );
- SetDlgItemText (IDC_EDIT_RECV, pBuf); // The lecture data is displayed in IDC_EDIT_RECV.
- CloseClipboard ();
- }
- }
The preceding section describes how to operate the clipboard in C ++.