在 Windows 中设置全局剪贴板文本,本机 c++

发布于 2024-09-08 08:14:57 字数 702 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-09-15 08:14:57

如何在剪贴板上设置文本

CString source; 
//put your text in source
if(OpenClipboard())
{
    HGLOBAL clipbuffer;
    char * buffer;
    EmptyClipboard();
    clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
    buffer = (char*)GlobalLock(clipbuffer);
    strcpy(buffer, LPCSTR(source));
    GlobalUnlock(clipbuffer);
    SetClipboardData(CF_TEXT,clipbuffer);
    CloseClipboard();
}

如何从剪贴板中获取文本

char * buffer;
if(OpenClipboard())
{

    buffer = (char*)GetClipboardData(CF_TEXT);
    //do something with buffer here 
    //before it goes out of scope

}

CloseClipboard(); 

How to Set text on clipboard

CString source; 
//put your text in source
if(OpenClipboard())
{
    HGLOBAL clipbuffer;
    char * buffer;
    EmptyClipboard();
    clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
    buffer = (char*)GlobalLock(clipbuffer);
    strcpy(buffer, LPCSTR(source));
    GlobalUnlock(clipbuffer);
    SetClipboardData(CF_TEXT,clipbuffer);
    CloseClipboard();
}

How to get text off of the clipboard

char * buffer;
if(OpenClipboard())
{

    buffer = (char*)GetClipboardData(CF_TEXT);
    //do something with buffer here 
    //before it goes out of scope

}

CloseClipboard(); 
药祭#氼 2024-09-15 08:14:57

您应该尝试使用 Raymond 的 辅助函数 进行 SetClipboardData

部分问题可能是您在应该使用 GMEM_MOVEABLE 时将 LMEM_MOVEABLEGlobalAlloc 一起使用,但我尚未验证这一点。

You should just try using Raymond's helper function for SetClipboardData.

Part of the problem could be you're using LMEM_MOVEABLE with GlobalAlloc, when you should be using GMEM_MOVEABLE, but I haven't verified this.

伴梦长久 2024-09-15 08:14:57

我必须首先调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文