2022年Windows程序设计源代码A .pdf
chap12-ClipText /*- CLIPTEXT.C - The Clipboard and Text (c) Charles Petzold, 1998 -*/ #include #include resource.h LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; #ifdef UNICODE #define CF_TCHAR CF_UNICODETEXT TCHAR szDefaultText = TEXT (Default Text - Unicode Version) ; TCHAR szCaption = TEXT (Clipboard Text Transfers - Unicode Version) ; #else #define CF_TCHAR CF_TEXT TCHAR szDefaultText = TEXT (Default Text - ANSI Version) ; TCHAR szCaption = TEXT (Clipboard Text Transfers - ANSI Version) ; #endif int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (ClipText) ; HACCEL hAccel ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 8 页 - - - - - - - - - wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (This program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, szCaption, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; hAccel = LoadAccelerators (hInstance, szAppName) ; while (GetMessage (&msg, NULL, 0, 0) if (!TranslateAccelerator (hwnd, hAccel, &msg) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static PTSTR pText ; BOOL bEnable ; HGLOBAL hGlobal ; HDC hdc ; PTSTR pGlobal ; PAINTSTRUCT ps ; RECT rect ; switch (message) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - case WM_CREATE: SendMessage (hwnd, WM_COMMAND, IDM_EDIT_RESET, 0) ; return 0 ; case WM_INITMENUPOPUP: EnableMenuItem (HMENU) wParam, IDM_EDIT_PASTE, IsClipboardFormatAvailable (CF_TCHAR) ? MF_ENABLED : MF_GRAYED) ; bEnable = pText ? MF_ENABLED : MF_GRAYED ; EnableMenuItem (HMENU) wParam, IDM_EDIT_CUT, bEnable) ; EnableMenuItem (HMENU) wParam, IDM_EDIT_COPY, bEnable) ; EnableMenuItem (HMENU) wParam, IDM_EDIT_CLEAR, bEnable) ; break ; case WM_COMMAND: switch (LOWORD (wParam) case IDM_EDIT_PASTE: OpenClipboard (hwnd) ; if (hGlobal = GetClipboardData (CF_TCHAR) pGlobal = GlobalLock (hGlobal) ; if (pText) free (pText) ; pText = NULL ; pText = malloc (GlobalSize (hGlobal) ; lstrcpy (pText, pGlobal) ; InvalidateRect (hwnd, NULL, TRUE) ; CloseClipboard () ; return 0 ; case IDM_EDIT_CUT: case IDM_EDIT_COPY: if (!pText) return 0 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 8 页 - - - - - - - - - hGlobal = GlobalAlloc (GHND | GMEM_SHARE, (lstrlen (pText) + 1) * sizeof (TCHAR) ; pGlobal = GlobalLock (hGlobal) ; lstrcpy (pGlobal, pText) ; GlobalUnlock (hGlobal) ; OpenClipboard (hwnd) ; EmptyClipboard () ; SetClipboardData (CF_TCHAR, hGlobal) ; CloseClipboard () ; if (LOWORD (wParam) = IDM_EDIT_COPY) return 0 ; / fall through for IDM_EDIT_CUT case IDM_EDIT_CLEAR: if (pText) free (pText) ; pText = NULL ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case IDM_EDIT_RESET: if (pText) free (pText) ; pText = NULL ; pText = malloc (lstrlen (szDefaultText) + 1) * sizeof (TCHAR) ; lstrcpy (pText, szDefaultText) ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; break ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 8 页 - - - - - - - - - if (pText != NULL) DrawText (hdc, pText, -1, &rect, DT_EXPANDTABS | DT_WORDBREAK) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: if (pText) free (pText) ; PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ; chap12-ClipText ?t /NO_DEPENDENCIES / Microsoft Developer Studio generated include file. / Used by ClipText.rc / #define IDM_EDIT_CUT 40001 #define IDM_EDIT_COPY 40002 #define IDM_EDIT_PASTE 40003 #define IDM_EDIT_CLEAR 40004 #define IDM_EDIT_RESET 40005 / Next default values for new objects / #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 103 #define _APS_NEXT_COMMAND_VALUE 40006 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif chap12-ClipView /*- CLIPVIEW.C - Simple Clipboard Viewer (c) Charles Petzold, 1998 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 8 页 - - - - - - - - - -*/ #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (ClipView) ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (This program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, TEXT (Simple Clipboard Viewer (Text Only), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 8 页 - - - - - - - - - TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static HWND hwndNextViewer ; HGLOBAL hGlobal ; HDC hdc ; PTSTR pGlobal ; PAINTSTRUCT ps ; RECT rect ; switch (message) case WM_CREATE: hwndNextViewer = SetClipboardViewer (hwnd) ; return 0 ; case WM_CHANGECBCHAIN: if (HWND) wParam = hwndNextViewer) hwndNextViewer = (HWND) lParam ; else if (hwndNextViewer) SendMessage (hwndNextViewer, message, wParam, lParam) ; return 0 ; case WM_DRAWCLIPBOARD: if (hwndNextViewer) SendMessage (hwndNextViewer, message, wParam, lParam) ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; OpenClipboard (hwnd) ; #ifdef UNICODE 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 8 页 - - - - - - - - - hGlobal = GetClipboardData (CF_UNICODETEXT) ; #else hGlobal = GetClipboardData (CF_TEXT) ; #endif if (hGlobal != NULL) pGlobal = (PTSTR) GlobalLock (hGlobal) ; DrawText (hdc, pGlobal, -1, &rect, DT_EXPANDTABS) ; GlobalUnlock (hGlobal) ; CloseClipboard () ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: ChangeClipboardChain (hwnd, hwndNextViewer) ; PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 8 页 - - - - - - - - -