直接上代码吧,我也忘了哪里抄的了
#include "string" #include "iostream" #include <Windows.h> using namespace std; /*将字符串写入到剪切板*/ int ctrlc(const char *data){ int contentSize = strlen(data) + 1; HGLOBAL hMemory; LPTSTR lpMemory; if (!OpenClipboard(NULL)) return 0;/* 打开剪切板 */ if (!EmptyClipboard()) return 0; /* 清空剪切板 */ if (!(hMemory = GlobalAlloc(GMEM_MOVEABLE, contentSize))) return 0;/* 对剪切板分配内存 */ if (!(lpMemory = (LPTSTR)GlobalLock(hMemory))) return 0;/* 锁定内存区域 */ memcpy_s(lpMemory, contentSize, data, contentSize); /* 复制数据到内存区域 */ GlobalUnlock(hMemory); /* 解除内存锁定 */ if (!SetClipboardData(CF_TEXT, hMemory)) return 0; /* 设置剪切板数据 */ printf("成功复制【%s】到剪切板,字符串长度为%d。\n", data, contentSize); CloseClipboard();/* 关闭剪切板 */ return 1; } int main() { ctrlc("i love you "); while (true); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论