1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #include <Windows.h> #include <tchar.h> #include <strsafe.h> #include "resource.h" #pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#ifdef UNICODE #define CF_TCHAR CF_UNICODETEXT #else #define CF_TCHAR CF_TEXT #endif
typedef struct _CUSTOM_DATA{ TCHAR szName[128]; UINT uAge; }CUSTOM_DATA, * PCUSTOM_DATA;
HINSTANCE g_hInstance; HWND g_hwnd; HWND g_hwndList; UINT g_uFormat;
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); VOID OnInit(HWND hwndDlg); VOID OnBtnText(); VOID OnBtnBitmap(); VOID OnBtnCustom(); VOID OnBtnDelay(); VOID OnBtnMultiple(); VOID OnRenderFormat(UINT uFormat); VOID OnDestroyClipbord(); VOID OnRenderAllFormats(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DialogProc, NULL); return 0; }; INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: { OnInit(hwndDlg); return TRUE; }; case WM_COMMAND: { switch (LOWORD(wParam)) { case IDC_BTN_TEXT: { OnBtnText(); break; }; case IDC_BTN_BITMAP: { OnBtnBitmap(); break; }; case IDC_BTN_CUSTOM: { OnBtnCustom(); break; }; case IDC_BTN_DELAY: { OnBtnDelay(); break; }; case IDC_BTN_MULTIPLE: { OnBtnMultiple(); break; }; case IDCANCEL: { EndDialog(hwndDlg, IDCANCEL); break; }; }; return TRUE; }; case WM_RENDERFORMAT: { OnRenderFormat(wParam); return FALSE; }; case WM_DESTROYCLIPBOARD: { OnDestroyClipbord(); return FALSE; }; case WM_RENDERALLFORMATS: { OnRenderAllFormats(); return FALSE; }; }; return FALSE; };
VOID OnInit(HWND hwndDlg) { g_hwnd = hwndDlg; g_hwndList = GetDlgItem(hwndDlg, IDC_LIST_MSG); g_uFormat = RegisterClipboardFormat(TEXT("RegisterFormat")); }; VOID OnBtnText() { TCHAR szText[128] = { 0 }; LPTSTR lpStr; OpenClipboard(g_hwnd); EmptyClipboard(); GetDlgItemText(g_hwnd, IDC_EDIT_TEXT, szText, _countof(szText)); lpStr = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (_tcslen(szText) + 1) * sizeof(TCHAR)); if (lpStr) { StringCchCopy(lpStr, _tcslen(szText) + 1, szText); SetClipboardData(CF_TCHAR, lpStr); }; CloseClipboard(); }; VOID OnBtnBitmap() { OpenClipboard(g_hwnd); EmptyClipboard(); HDC hdcDesk, hdcMem; HBITMAP hBmp; int nWidth = GetSystemMetrics(SM_CXSCREEN); int nHeight = GetSystemMetrics(SM_CYSCREEN); hdcDesk = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); hdcMem = CreateCompatibleDC(hdcDesk); hBmp = CreateCompatibleBitmap(hdcDesk, nWidth, nHeight); SelectObject(hdcMem, hBmp); BitBlt(hdcMem, 0, 0, nWidth, nHeight, hdcDesk, 0, 0, SRCCOPY); SetClipboardData(CF_BITMAP, hBmp); DeleteObject(hBmp); DeleteDC(hdcMem); DeleteDC(hdcDesk); CloseClipboard(); }; VOID OnBtnCustom() { LPVOID lpMem; CUSTOM_DATA customData = { TEXT("老王"), 40 }; OpenClipboard(g_hwnd); EmptyClipboard(); lpMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CUSTOM_DATA)); if (lpMem) { memcpy_s(lpMem, sizeof(CUSTOM_DATA), &customData, sizeof(CUSTOM_DATA)); SetClipboardData(g_uFormat, lpMem); }; CloseClipboard(); }; VOID OnBtnDelay() { OpenClipboard(g_hwnd); EmptyClipboard(); SetClipboardData(CF_BITMAP, NULL); CloseClipboard(); }; VOID OnBtnMultiple() { OpenClipboard(g_hwnd); EmptyClipboard(); TCHAR szText[128] = { 0 }; LPTSTR lpStr; GetDlgItemText(g_hwnd, IDC_EDIT_TEXT, szText, _countof(szText)); lpStr = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (_tcslen(szText) + 1) * sizeof(TCHAR)); if (lpStr) { StringCchCopy(lpStr, _tcslen(szText) + 1, szText); SetClipboardData(CF_TCHAR, lpStr); }; HDC hdcDesk, hdcMem; HBITMAP hBmp; int nWidth = GetSystemMetrics(SM_CXSCREEN); int nHeight = GetSystemMetrics(SM_CYSCREEN); hdcDesk = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); hdcMem = CreateCompatibleDC(hdcDesk); hBmp = CreateCompatibleBitmap(hdcDesk, nWidth, nHeight); SelectObject(hdcMem, hBmp); BitBlt(hdcMem, 0, 0, nWidth, nHeight, hdcDesk, 0, 0, SRCCOPY); SetClipboardData(CF_BITMAP, hBmp); DeleteObject(hBmp); DeleteDC(hdcMem); DeleteDC(hdcDesk); LPVOID lpMem; CUSTOM_DATA customData = { TEXT("老王"), 40 }; lpMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CUSTOM_DATA)); if (lpMem) { memcpy_s(lpMem, sizeof(CUSTOM_DATA), &customData, sizeof(CUSTOM_DATA)); SetClipboardData(g_uFormat, lpMem); }; CloseClipboard(); }; VOID OnRenderFormat(UINT uFormat) { SendMessage(g_hwndList, LB_ADDSTRING, 0, (LPARAM)TEXT("WM_RENDERFORMAT")); HDC hdcDesk, hdcMem; HBITMAP hBmp; int nWidth = GetSystemMetrics(SM_CXSCREEN); int nHeight = GetSystemMetrics(SM_CYSCREEN); hdcDesk = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); hdcMem = CreateCompatibleDC(hdcDesk); hBmp = CreateCompatibleBitmap(hdcDesk, nWidth, nHeight); SelectObject(hdcMem, hBmp); BitBlt(hdcMem, 0, 0, nWidth, nHeight, hdcDesk, 0, 0, SRCCOPY); SetClipboardData(uFormat, hBmp); DeleteObject(hBmp); DeleteDC(hdcMem); DeleteDC(hdcDesk); }; VOID OnDestroyClipbord() { SendMessage(g_hwndList, LB_ADDSTRING, 0, (LPARAM)TEXT("WM_DESTROYCLIPBOARD")); }; VOID OnRenderAllFormats() { SendMessage(g_hwndList, LB_ADDSTRING, 0, (LPARAM)TEXT("WM_RENDERALLFORMATS")); OnBtnBitmap(); };
|