📄 subject_63856.htm
字号:
<p>
序号:63856 发表者:ct57 发表日期:2003-12-06 20:02:49
<br>主题:如何使用剪切板粘贴东西?
<br>内容:我想把一串 char 复制到剪切板上该怎么做?<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:太平冰水 回复日期:2003-12-08 13:12:24
<br>内容:使用与剪贴板相关的API:<BR>RegisterClipboardFormat<BR>IsClipboardFormatAvailable<BR>OpenClipboard<BR>GetClipboardData<BR>CloseClipboard<BR>SetClipboardData<BR><BR>Example of a Clipboard Viewer<BR>The following example shows a simple clipboard viewer application. <BR><BR>HINSTANCE hinst; <BR>UINT uFormat = (UINT)(-1); <BR>BOOL fAuto = TRUE; <BR> <BR>LRESULT APIENTRY MainWndProc(hwnd, uMsg, wParam, lParam) <BR>HWND hwnd; <BR>UINT uMsg; <BR>WPARAM wParam; <BR>LPARAM lParam; <BR>{ <BR> static HWND hwndNextViewer; <BR> <BR> HDC hdc; <BR> HDC hdcMem; <BR> PAINTSTRUCT ps; <BR> LPPAINTSTRUCT lpps; <BR> RECT rc; <BR> LPRECT lprc; <BR> HGLOBAL hglb; <BR> LPSTR lpstr; <BR> HBITMAP hbm; <BR> HENHMETAFILE hemf; <BR> HWND hwndOwner; <BR> <BR> switch (uMsg) <BR> { <BR> case WM_PAINT: <BR> hdc = BeginPaint(hwnd, &ps); <BR> <BR> // Branch depending on the clipboard format. <BR> <BR> switch (uFormat) <BR> { <BR> case CF_OWNERDISPLAY: <BR> hwndOwner = GetClipboardOwner(); <BR> hglb = GlobalAlloc(GMEM_MOVEABLE, <BR> sizeof(PAINTSTRUCT)); <BR> lpps = GlobalLock(hglb); <BR> memcpy(lpps, &ps, sizeof(PAINTSTRUCT)); <BR> GlobalUnlock(hglb); <BR> <BR> SendMessage(hwndOwner, WM_PAINTCLIPBOARD, <BR> (WPARAM) hwnd, (LPARAM) hglb); <BR> <BR> GlobalFree(hglb); <BR> break; <BR> <BR> case CF_BITMAP: <BR> hdcMem = CreateCompatibleDC(hdc); <BR> if (hdcMem != NULL) <BR> { <BR> if (OpenClipboard(hwnd)) <BR> { <BR> hbm = (HBITMAP) <BR> GetClipboardData(uFormat); <BR> SelectObject(hdcMem, hbm); <BR> GetClientRect(hwnd, &rc); <BR> <BR> BitBlt(hdc, 0, 0, rc.right, rc.bottom, <BR> hdcMem, 0, 0, SRCCOPY); <BR> CloseClipboard(); <BR> } <BR> DeleteDC(hdcMem); <BR> } <BR> break; <BR> <BR> case CF_TEXT: <BR> if (OpenClipboard(hwnd)) <BR> { <BR> hglb = GetClipboardData(uFormat); <BR> lpstr = GlobalLock(hglb); <BR> <BR> GetClientRect(hwnd, &rc); <BR> DrawText(hdc, lpstr, -1, &rc, DT_LEFT); <BR> <BR> GlobalUnlock(hglb); <BR> CloseClipboard(); <BR> } <BR> break; <BR> <BR> case CF_ENHMETAFILE: <BR> if (OpenClipboard(hwnd)) <BR> { <BR> hemf = GetClipboardData(uFormat); <BR> GetClientRect(hwnd, &rc); <BR> PlayEnhMetaFile(hdc, hemf, &rc); <BR> CloseClipboard(); <BR> } <BR> break; <BR> <BR> case 0: <BR> GetClientRect(hwnd, &rc); <BR> DrawText(hdc, "The clipboard is empty.", -1, <BR> &rc, DT_CENTER | DT_SINGLELINE | <BR> DT_VCENTER); <BR> break; <BR> <BR> default: <BR> GetClientRect(hwnd, &rc); <BR> DrawText(hdc, "Unable to display format.", -1, <BR> &rc, DT_CENTER | DT_SINGLELINE | <BR> DT_VCENTER); <BR> } <BR> EndPaint(hwnd, &ps); <BR> break; <BR> <BR> case WM_SIZE: <BR> if (uFormat == CF_OWNERDISPLAY) <BR> { <BR> hwndOwner = GetClipboardOwner(); <BR> hglb = GlobalAlloc(GMEM_MOVEABLE, sizeof(RECT)); <BR> lprc = GlobalLock(hglb); <BR> GetClientRect(hwnd, lprc); <BR> GlobalUnlock(hglb); <BR> <BR> SendMessage(hwndOwner, WM_SIZECLIPBOARD, <BR> (WPARAM) hwnd, (LPARAM) hglb); <BR> <BR> GlobalFree(hglb); <BR> } <BR> break; <BR> <BR> case WM_CREATE: <BR> <BR> // Add the window to the clipboard viewer chain. <BR> <BR> hwndNextViewer = SetClipboardViewer(hwnd); <BR> break; <BR> <BR> case WM_CHANGECBCHAIN: <BR> <BR> // If the next window is closing, repair the chain. <BR> <BR> if ((HWND) wParam == hwndNextViewer) <BR> hwndNextViewer = (HWND) lParam; <BR> <BR> // Otherwise, pass the message to the next link. <BR> <BR> else if (hwndNextViewer != NULL) <BR> SendMessage(hwndNextViewer, uMsg, wParam, lParam); <BR> <BR> break; <BR> <BR> case WM_DESTROY: <BR> ChangeClipboardChain(hwnd, hwndNextViewer); <BR> PostQuitMessage(0); <BR> break; <BR> <BR> case WM_DRAWCLIPBOARD: // clipboard contents changed. <BR> <BR> // Update the window by using Auto clipboard format. <BR> <BR> SetAutoView(hwnd); <BR> <BR> // Pass the message to the next window in clipboard <BR> // viewer chain. <BR> <BR> SendMessage(hwndNextViewer, uMsg, wParam, lParam); <BR> break; <BR> <BR> case WM_INITMENUPOPUP: <BR> if (!HIWORD(lParam)) <BR> InitMenu(hwnd, (HMENU) wParam); <BR> break; <BR> <BR> case WM_COMMAND: <BR> switch (LOWORD(wParam)) <BR> { <BR> case IDM_EXIT: <BR> DestroyWindow(hwnd); <BR> break; <BR> <BR> case IDM_AUTO: <BR> SetAutoView(hwnd); <BR> break; <BR> <BR> default: <BR> fAuto = FALSE; <BR> uFormat = LOWORD(wParam); <BR> InvalidateRect(hwnd, NULL, TRUE); <BR> } <BR> break; <BR> <BR> default: <BR> return DefWindowProc(hwnd, uMsg, wParam, lParam); <BR> } <BR> return (LRESULT) NULL; <BR>} <BR> <BR>void WINAPI SetAutoView(HWND hwnd) <BR>{ <BR> static UINT auPriorityList[] = { <BR> CF_OWNERDISPLAY, <BR> CF_TEXT, <BR> CF_ENHMETAFILE, <BR> CF_BITMAP <BR> }; <BR> <BR> uFormat = GetPriorityClipboardFormat(auPriorityList, 4); <BR> fAuto = TRUE; <BR> <BR> InvalidateRect(hwnd, NULL, TRUE); <BR> UpdateWindow(hwnd); <BR>} <BR> <BR>void WINAPI InitMenu(HWND hwnd, HMENU hmenu) <BR>{ <BR> UINT uFormat; <BR> char szFormatName[80]; <BR> LPCSTR lpFormatName; <BR> UINT fuFlags; <BR> UINT idMenuItem; <BR> <BR> // If a menu is not the display menu, no initialization is necessary. <BR> <BR> if (GetMenuItemID(hmenu, 0) != IDM_AUTO) <BR> return; <BR> <BR> // Delete all menu items except the first. <BR> <BR> while (GetMenuItemCount(hmenu) > 1) <BR> DeleteMenu(hmenu, 1, MF_BYPOSITION); <BR> <BR> // Check or uncheck the Auto menu item. <BR> <BR> fuFlags = fAuto ? MF_BYCOMMAND | MF_CHECKED : <BR> MF_BYCOMMAND | MF_UNCHECKED; <BR> CheckMenuItem(hmenu, IDM_AUTO, fuFlags); <BR> <BR> // If there are no clipboard formats, return. <BR> <BR> if (CountClipboardFormats() == 0) <BR> return; <BR> <BR> // Open the clipboard. <BR> <BR> if (!OpenClipboard(hwnd)) <BR> return; <BR> <BR> // Add a separator and then a menu item for each format. <BR> <BR> AppendMenu(hmenu, MF_SEPARATOR, 0, NULL); <BR> uFormat = EnumClipboardFormats(0); <BR> <BR> while (uFormat) <BR> { <BR> // Call an application-defined function to get the name <BR> // of the clipboard format. <BR> <BR> lpFormatName = GetPredefinedClipboardFormatName(uFormat); <BR> <BR> // For registered formats, get the registered name. <BR> <BR> if (lpFormatName == NULL) <BR> { <BR> if (GetClipboardFormatName(uFormat, szFormatName, <BR> sizeof(szFormatName))) <BR> lpFormatName = szFormatName; <BR> else <BR> lpFormatName = "(unknown)"; <BR> } <BR> <BR> // Add a menu item for the format. For displayable <BR> // formats, use the format ID for the menu ID. <BR> <BR> if (IsDisplayableFormat(uFormat)) <BR> { <BR> fuFlags = MF_STRING; <BR> idMenuItem = uFormat; <BR> } <BR> else <BR> { <BR> fuFlags = MF_STRING | MF_GRAYED; <BR> idMenuItem = 0; <BR> } <BR> AppendMenu(hmenu, fuFlags, idMenuItem, lpFormatName); <BR> <BR> uFormat = EnumClipboardFormats(uFormat); <BR> } <BR> CloseClipboard(); <BR> <BR>} <BR> <BR>BOOL WINAPI IsDisplayableFormat(UINT uFormat) <BR>{ <BR> switch (uFormat) <BR> { <BR> case CF_OWNERDISPLAY: <BR> case CF_TEXT: <BR> case CF_ENHMETAFILE: <BR> case CF_BITMAP: <BR> return TRUE; <BR> } <BR> return FALSE; <BR>} <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -