简体   繁体   中英

C++ WINAPI Button Clipboard - Copy action

I am a bit new to C++ but I know the basics and how to use it properly. I am just wondering how to copy to a clipboard in a windows application. I want 4 buttons to be able to copy a message when clicked and store it in clipboard. Here is the code for the button, the if statement is the action that is carried out when clicked:

case WM_CREATE:{

            CreateWindow(TEXT("BUTTON"), TEXT("Copythistext"),
            WS_CHILD | WS_VISIBLE,
            12,60,10,20,
            hwnd, (HMENU) ID_BUTTON, NULL, NULL
            );

            break;
       }



case WM_COMMAND:{      //this is where the button performs it's task 
            if(LOWORD(wParam)== ID_BUTTON) { 
        //this is where the task goes

       }
       break;
     }

有一种奇怪的方式将文本从按钮发送到剪贴板:通过SetDlgItemText + WM_LBUTTONDBLCLK到带有SS_NOTIFY的STATIC(STATIC的大小可能为= 0)。

Sequence of events:

void AddToClipboard(char* pszText)
{
int nStrLen = strlen(pszText);
HGLOBAL hMem = GlobalAlloc(nStrLen + 1, GMEM_SHARE);
char* pCopyTo = (char*) GlobalLock(hMem);
strcpy(pCopyTo, pszText);
GlobalUnlock(hMem);
OpenClipboard(NULL); // or HWND handle instead of NULL
EmptyClipboard();


SetClipboardData(CF_TEXT, hMem); // hMem is handle to memory allocated with GlobalAlloc
CloseClipboard();
}

For the hMem, you usually do a GlobalAlloc(), GlobalLock(), copy string to the pointer returned by GlobalLock(), then do a GlobalUnlock() on the handle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM