在 Windows 中设置全局剪贴板文本,本机 c++
我有一个 ASCII 字符串(一个以 null 结尾的字符数组) 在控制台应用程序中。
我想做的就是制作它,以便我的应用程序将该字符串放入“全局剪贴板”中 这样运行后,我可以在任何标准应用程序(在本例中为 Visual Studio)中按 ctrl+v,并且我的字符串将被粘贴!
我该怎么做?
我已经完成:
void SetClipboardText(char* txt)
{
if(!OpenClipboard(NULL))
{
__asm int 3;
}
int l = PIstrlen(txt);
HLOCAL la = GlobalAlloc(LMEM_MOVEABLE,l+1);
void* dest = GlobalLock(la);
PImemcpy(dest,txt,l+1);
GlobalUnlock(la);
if(!SetClipboardData(CF_OEMTEXT,la))
{
__asm int 3;
}
CloseClipboard();
}
我尝试过 CF_TEXT、CF_OEMTEXT、CF_UNICODE,我尝试过 NULL 和 GetDesktopWindow() 打开剪贴板时
似乎没有任何效果。 编辑: 上面的代码总是“有效”,它永远不会出错,它只是永远不会做我想要的!
I have an ASCII string (A null terminated char array)
in a console app.
all I want to do is make it so my app will put this string into the "global clipboard"
so that after running it, I can ctrl+v in any standard app (in this case, visual studio) and my string will be pasted!
how do I do this?
I have done:
void SetClipboardText(char* txt)
{
if(!OpenClipboard(NULL))
{
__asm int 3;
}
int l = PIstrlen(txt);
HLOCAL la = GlobalAlloc(LMEM_MOVEABLE,l+1);
void* dest = GlobalLock(la);
PImemcpy(dest,txt,l+1);
GlobalUnlock(la);
if(!SetClipboardData(CF_OEMTEXT,la))
{
__asm int 3;
}
CloseClipboard();
}
I have tried CF_TEXT, CF_OEMTEXT, CF_UNICODE, I have tried NULL and GetDesktopWindow() when opening the clipboard
nothing seems to work.
Edit:
the above code always 'works' it never errors, it just never does what I want!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何在剪贴板上设置文本
如何从剪贴板中获取文本
How to Set text on clipboard
How to get text off of the clipboard
您应该尝试使用 Raymond 的 辅助函数 进行
SetClipboardData
。部分问题可能是您在应该使用
GMEM_MOVEABLE
时将LMEM_MOVEABLE
与GlobalAlloc
一起使用,但我尚未验证这一点。You should just try using Raymond's helper function for
SetClipboardData
.Part of the problem could be you're using
LMEM_MOVEABLE
withGlobalAlloc
, when you should be usingGMEM_MOVEABLE
, but I haven't verified this.我必须首先调用 EmptyClipboard() 清空剪贴板,
我认为这是因为,在我尝试的所有 CF_XXX 中,我没有选择最“默认”的文本。
这个想法是,你可以复制图像,然后复制文本,它们都会被放入剪贴板中,这样你就可以转到图像程序,点击粘贴,它会粘贴图像,然后转到文本程序,点击粘贴,它将粘贴文本。
因此我相信我的问题是我没有选择“默认”文本格式,它只是添加到剪贴板中更“默认”格式的内容后面,因此当您在程序中点击粘贴时,它会选择更多“默认”格式的内容要粘贴。
所以是的,我不完全理想的修复方法是在 OpenClipboard() 之后添加 EmptyClipboard(),这会导致所有内容都从剪贴板中删除,并且程序默认粘贴我不完全默认的格式文本。
I had to empty the clipboard first calling EmptyClipboard()
I think this is because, of all the CF_XXX I tried, I didn't pick the most 'default' one for text.
the idea being, you can copy an image, then copy text, and they both get put in the clipboard, so you can then go to an image program, hit paste, and it will paste the image, then go to a text program, hit paste, and it will paste the text.
hence I believe my problem was I wasn't picking a 'default' text format, it was just getting added on to the clipboard behind something in the more 'default' format and so when ever you hit paste in a program, it picked the more 'default' formatted thing to paste.
so yeah, my not entirely ideal fix was to just add EmptyClipboard() after OpenClipboard(), this causes everything to be deleted out of the clipboard, and programs to default to pasting my not entirely default format text.