c++调用win api实现获取鼠标选中的内容思路是1 枚举Clipboard存在的各种格式的数据2 读取各种类型的数据3 模拟ctrl+c4 枚举Clipboard存在的各种格式的数据并读取5将第一步读取的数据恢复到Clipboard使用C++将上面的思路实现
#include <Windows.h>
#include
using namespace std;
int main() { // 1. 枚举Clipboard存在的各种格式的数据 if (!OpenClipboard(NULL)) { cout << "OpenClipboard failed!" << endl; return 1; }
HANDLE hData = NULL;
int i = 0;
while (hData = EnumClipboardFormats(hData))
{
TCHAR szFormatName[MAX_PATH] = { 0 };
if (GetClipboardFormatName(hData, szFormatName, MAX_PATH))
{
cout << "Format " << i << ": " << szFormatName << endl;
}
i++;
}
// 2. 读取各种类型的数据
HANDLE hText = NULL;
hText = GetClipboardData(CF_UNICODETEXT);
if (hText)
{
wcout << "CF_UNICODETEXT: " << (wchar_t*)GlobalLock(hText) << endl;
GlobalUnlock(hText);
}
// 3. 模拟ctrl+c
keybd_event(VK_CONTROL, 0, 0, 0);
keybd_event('C', 0, 0, 0);
keybd_event('C', 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
// 4. 枚举Clipboard存在的各种格式的数据并读取
i = 0;
hData = NULL;
while (hData = EnumClipboardFormats(hData))
{
TCHAR szFormatName[MAX_PATH] = { 0 };
if (GetClipboardFormatName(hData, szFormatName, MAX_PATH))
{
cout << "Format " << i << ": " << szFormatName << endl;
}
HANDLE hData = GetClipboardData(hData);
if (hData)
{
DWORD dwSize = GlobalSize(hData);
LPVOID pData = GlobalLock(hData);
if (pData != NULL)
{
if (IsClipboardFormatText(hData))
{
cout << "Text: " << (TCHAR*)pData << endl;
}
else if (IsClipboardFormatBitmap(hData))
{
// 处理位图数据
}
else if (IsClipboardFormatHDrop(hData))
{
// 处理文件路径
}
}
GlobalUnlock(hData);
}
i++;
}
// 5. 将第一步读取的数据恢复到Clipboard
EmptyClipboard();
if (!SetClipboardData(CF_UNICODETEXT, hText))
{
cout << "SetClipboardData failed!" << endl;
}
CloseClipboard();
return 0;
原文地址: http://www.cveoy.top/t/topic/g7nG 著作权归作者所有。请勿转载和采集!