Copy string to windows clipboard

zhaozj2021-02-11  208

// hwnd can be null, lpsztext is a string that needs to be copied.

Bool CopyStringToClipboard (hwnd hwnd, lpctstr lpsztext) {int Nlen = Strlen (lpsztext); if (nlen == 0) Return False;

// Copy the contents of SzText to the clipboard hglobal hglbcopy; lptstr lptstrcopy;

IF (! :: OpenClipboard (hwnd)) Return False;

Hglbcopy = Globalalloc (gmem_ddeshare, (Nlen 1) * Sizeof (char)); if (hglbcopy == null) {closeclipboard (); returnaf

EMPTYCLIPBOARD ();

lptstrCopy = (LPTSTR) GlobalLock (hglbCopy); memcpy (lptstrCopy, lpszText, nlen); lptstrCopy [nlen] = (TCHAR) 0; // null character GlobalUnlock (lptstrCopy); SetClipboardData (CF_TEXT, hglbCopy); CloseClipboard ();

Return True;}

转载请注明原文地址:https://www.9cbs.com/read-5823.html

New Post(0)