VC Get Clipboard text content

Source: Internet
Author: User
Tags assert

Objective:

This article is limited to obtaining and setting the contents of the Clipboard, but not to the picture and other resources;


Example:

One: Set Clipboard text content (support for general symbols, special symbols not tested)

BOOL Setclipboardtext (LPCSTR Text,hwnd hwnd)  
{  
	ASSERT (HWND);  
	Open the Clipboard  
	if (!::openclipboard (hWnd)) return  
		false;  
	If the Clipboard has content, close the Clipboard if
	(! EmptyClipboard ())  
	{  
		closeclipboard ();  
		return false;  
	}  
	Gets the length  
	int len=strlen (text) for which the text needs to be set;  
	Request Clipboard Space
	HANDLE Hclip=globalalloc (gmem_moveable| Gmem_ddeshare, (len+1) *sizeof (char*));  
	if (hclip==null)  
	{  
		closeclipboard ();  
		return false;  
	}  
	The space of the application is locked
	char* pbuf= (char*) GlobalLock (hclip);  
	if (pbuf==null)  
	{  
		globalfree (hclip);    
		CloseClipboard ();  
		return false;  
	}  
	Copy text content to clipboard
	memcpy ((char *) pbuf,text,len*sizeof (char*));  
	Pbuf[len]=null;  
	Operation complete, Release lock
	GlobalUnlock (hclip);  
	if (Null==setclipboarddata (cf_text,hclip))  
	{  
		globalfree (hclip);    
		CloseClipboard ();  
		return false;  
	}  
	CloseClipboard ();  
	return true;  
}  

Second: Get the Clipboard text content (the std_string here is encapsulated by a string, overloaded with the "=" number)

Gets the Clipboard text content
std_string Getclipboardtext (hwnd hwnd)  
{       
	ASSERT (HWND);  
	Std_string Clipboardtext;  
	Determines whether the Clipboard data format can be processed.  
	if (! Isclipboardformatavailable (Cf_text)) return   
		Clipboardtext;   

	Opens the Clipboard.          
	if (!::openclipboard (hWnd)) return  
		Clipboardtext;   

	Get Data  
	HANDLE Hmem = GetClipboardData (cf_text);   
	if (Hmem!= NULL)           
	{   
		//get string.  
		LPSTR LPSTR = (LPSTR) globallock (HMEM);   
		if (lpStr!= NULL)        
		{   
			clipboardtext=lpstr;  
			Release lock Memory  
			GlobalUnlock (HMEM);       
		}    
	Close clipboard        
	closeclipboard ();    
	return clipboardtext;  
}  

Cond......


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.