C++实现复制文本粘贴文本功能

复制功能、参考链接:https://www.cnblogs.com/xuan52rock/p/6061155.html
粘贴功能参考链接:https://bbs.csdn.net/topics/390008354?list=1963771
整合代码如下:

/**
 * 复制功能
 */
BOOL CopyToClipboard(const char* pszData, const int nDataLen)
{
	if(::OpenClipboard(NULL))
	{
		::EmptyClipboard();
		HGLOBAL clipbuffer;
		char *buffer;
		clipbuffer = ::GlobalAlloc(GMEM_DDESHARE, nDataLen+1);
		buffer = (char *)::GlobalLock(clipbuffer);
		strcpy(buffer, pszData);
		::GlobalUnlock(clipbuffer);
		::SetClipboardData(CF_TEXT, clipbuffer);
		::CloseClipboard();
		return TRUE;
	}
	return FALSE;
}
/*
*  粘贴功能
*/
void PasteInfo()
{
	keybd_event(0x11, 0, 0, 0);// press ctrl
	keybd_event(0x56, 0, 0, 0); // press v
	keybd_event(0x56, 0, 2, 0); //release v
	keybd_event(0x11, 0, 2, 0); //release ctrl
}

你可能感兴趣的:(C++)