我需要包括什么才能使用 OpenClipboard()?

What do I need to include to use OpenClipboard()?

我写了一个小程序来测试 OpenClipboard 功能。我从 here 复制而来,看起来您不需要包含任何内容。我正在使用记事本和命令提示符进行编程,所以我没有第三方程序告诉我出了什么问题。错误消息只是代码片段中使用的所有内容未在此范围内声明:

programm1.cpp: In function 'void toClipboard(const string&)':
programm1.cpp:65:17: error: 'OpenClipboard' was not declared in this scope
  OpenClipboard(0);
                 ^
programm1.cpp:66:17: error: 'EmptyClipboard' was not declared in this scope
  EmptyClipboard();
                 ^
programm1.cpp:67:2: error: 'HGLOBAL' was not declared in this scope
  HGLOBAL hg=GlobalAlloc(GMEM_MOVEABLE,s.size());
  ^
programm1.cpp:67:10: error: expected ';' before 'hg'
  HGLOBAL hg=GlobalAlloc(GMEM_MOVEABLE,s.size());
          ^
programm1.cpp:68:7: error: 'hg' was not declared in this scope
  if (!hg){
       ^
programm1.cpp:69:18: error: 'CloseClipboard' was not declared in this scope
   CloseClipboard();
                  ^
programm1.cpp:72:20: error: 'hg' was not declared in this scope
  memcpy(GlobalLock(hg),s.c_str(),s.size());
                    ^
programm1.cpp:72:22: error: 'GlobalLock' was not declared in this scope
  memcpy(GlobalLock(hg),s.c_str(),s.size());
                      ^
programm1.cpp:72:42: error: 'memcpy' was not declared in this scope
  memcpy(GlobalLock(hg),s.c_str(),s.size());
                                          ^
programm1.cpp:73:17: error: 'GlobalUnlock' was not declared in this scope
  GlobalUnlock(hg);
                 ^
programm1.cpp:74:19: error: 'CF_TEXT' was not declared in this scope
  SetClipboardData(CF_TEXT,hg);
                   ^
programm1.cpp:74:29: error: 'SetClipboardData' was not declared in this scope
  SetClipboardData(CF_TEXT,hg);
                             ^
programm1.cpp:75:17: error: 'CloseClipboard' was not declared in this scope
  CloseClipboard();
                 ^
programm1.cpp:76:15: error: 'GlobalFree' was not declared in this scope
  GlobalFree(hg);
               ^

OpenClipboard 文档说明要使用的头文件是

Winuser.h (include Windows.h)

#include <Windows.h>

vvv 将文本复制到剪贴板的工作函数 - 如果需要就使用它vvv

此函数将文本放入剪贴板

没有警告

bool copyTextToClipboard(HWND hwnd, LPWSTR text, INT textLength) {

    /// <summary>
    /// This Function Puts Text into ClipBoard
    /// </summary>
    /// <param name="hwnd">newClipBoardOwner</param>
    /// <param name="text">Text into ClipBoard</param>
    /// <param name="textLength">TextLength</param>
    /// <returns>If Succeeded Returns True, but If not False</returns>

    if (OpenClipboard(hwnd)) {
        HGLOBAL CopyData = { 0 };
        void *buffer = NULL;
        EmptyClipboard();
        CopyData = GlobalAlloc(GMEM_FIXED, sizeof(TCHAR) * textLength);
        if (CopyData != NULL) {
            buffer = GlobalLock(CopyData);
        }
        if (buffer != NULL) {
            memcpy(buffer, (void*)text, sizeof(TCHAR) * textLength);
            GlobalUnlock(CopyData);
            SetClipboardData(CF_UNICODETEXT, CopyData);
            CloseClipboard();
            return true;
        }
        CloseClipboard();
        return false;
    }
    return false;

}