Thread: how to track every copy event

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    how to track every copy event

    How can I track every copy event in Windows?
    Suppose user copies from an editor, or from a text field, or some other places, I just want to be notified about it, so that whatever is being copied, I can get to that.

    I want to make a multiple copies Clip Board. The one that comes with XP only holds last copied item. I want to be able to copy many items at once, and then be able to select whatever I want to paste next, from my own Clip Board.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you actually need to make a program for it?
    There are software out there that does what you want, so unless you need to write it, you can have a look at some clipboard software.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    It's a usenet FAQ (since 1995...)
    See win32 api newsgroup (clipbrd vwr apis, C/C++) + MSDN

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    You must add your app's window to the clipboard viewer chain. This is done by calling SetClipbpardViewer. The return value is the handle to the next window in the chain. Next you must process WM_DRAWCLIPBOARD messages to determine when the clipboard changes. After you update your window with the clipboard changes, you must pass that data onto the next window in the chain. Finally, before closing your window (terminating you app), you must remove your window from the chain by calling ChangeClipboardChain.
    Code:
    #pragma comment( lib, "user32.lib" )
    #pragma comment( lib, "gdi32.lib" )
    
    #include <windows.h>
    #include <stdio.h>
    
    INT iFormat = -1; 
    BOOL bAuto = TRUE; 
    
    const char ClipboardCaptureName[] = "myCaptureClass";
    
    void WINAPI SetAutoView(HWND hwnd) 
    { 
        static UINT auPriorityList[] = { 
            CF_OWNERDISPLAY, 
            CF_TEXT, 
            CF_ENHMETAFILE, 
            CF_BITMAP 
        }; 
     
        iFormat = GetPriorityClipboardFormat(auPriorityList, 4); 
        bAuto = TRUE; 
        InvalidateRect(hwnd, NULL, TRUE); 
        UpdateWindow(hwnd); 
    } 
     
    LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
    { 
        static HWND hwndNextViewer; 
        HDC hdc; 
        HDC hdcMem; 
        PAINTSTRUCT ps; 
        LPPAINTSTRUCT lpps; 
        RECT rc; 
        LPRECT lprc; 
        HGLOBAL hglb; 
        LPSTR lpstr; 
        HBITMAP hbm; 
        HENHMETAFILE hemf; 
        HWND hwndOwner; 
     
        switch (uMsg) 
        { 
            case WM_PAINT: 
                hdc = BeginPaint(hwnd, &ps); 
                switch (iFormat) 
                { 
                    case CF_OWNERDISPLAY: 
                        hwndOwner = GetClipboardOwner(); 
                        hglb = GlobalAlloc(GMEM_MOVEABLE, 
                            sizeof(PAINTSTRUCT)); 
                        lpps =  (LPPAINTSTRUCT) GlobalLock(hglb);
                        memcpy(lpps, &ps, sizeof(PAINTSTRUCT)); 
                        GlobalUnlock(hglb); 
     
                        SendMessage(hwndOwner, WM_PAINTCLIPBOARD, 
                            (WPARAM) hwnd, (LPARAM) hglb); 
                         GlobalFree(hglb); 
                        break; 
                    case CF_BITMAP: 
                        hdcMem = CreateCompatibleDC(hdc); 
                        if (hdcMem != NULL) 
                        { 
                            if (OpenClipboard(hwnd)) 
                            { 
                                hbm = (HBITMAP) 
                                    GetClipboardData(iFormat); 
                                SelectObject(hdcMem, hbm); 
                                GetClientRect(hwnd, &rc); 
                                BitBlt(hdc, 0, 0, rc.right, rc.bottom, 
                                    hdcMem, 0, 0, SRCCOPY); 
                                CloseClipboard(); 
                            } 
                            DeleteDC(hdcMem); 
                        } 
                        break; 
                    case CF_TEXT: 
                        if (OpenClipboard(hwnd)) 
                        { 
                            hglb = GetClipboardData(iFormat); 
                            lpstr = (LPSTR) GlobalLock(hglb); 
                            GetClientRect(hwnd, &rc); 
                            DrawText(hdc, lpstr, -1, &rc, DT_LEFT); 
                            GlobalUnlock(hglb); 
                            CloseClipboard(); 
                        } 
                        break; 
                    case CF_ENHMETAFILE: 
                        if (OpenClipboard(hwnd)) 
                        { 
                            hemf = (HENHMETAFILE) GetClipboardData(iFormat); 
                            GetClientRect(hwnd, &rc); 
                            PlayEnhMetaFile(hdc, hemf, &rc); 
                            CloseClipboard(); 
                        } 
                        break; 
                    case 0: 
                        GetClientRect(hwnd, &rc); 
                        DrawText(hdc, "The clipboard is empty.", -1, 
                            &rc, DT_CENTER | DT_SINGLELINE | 
                            DT_VCENTER); 
                        break; 
                    default: 
                        GetClientRect(hwnd, &rc); 
                        DrawText(hdc, "Unable to display format.", -1, 
                            &rc, DT_CENTER | DT_SINGLELINE | 
                            DT_VCENTER); 
                } 
                EndPaint(hwnd, &ps); 
                break; 
            case WM_SIZE: 
                if (iFormat == CF_OWNERDISPLAY) 
                { 
                    hwndOwner = GetClipboardOwner(); 
                    hglb = GlobalAlloc(GMEM_MOVEABLE, sizeof(RECT)); 
                    lprc = (LPRECT)GlobalLock(hglb); 
                    GetClientRect(hwnd, lprc); 
                    GlobalUnlock(hglb); 
                    SendMessage(hwndOwner, WM_SIZECLIPBOARD, 
                        (WPARAM) hwnd, (LPARAM) hglb); 
                    GlobalFree(hglb); 
                } 
                break; 
            case WM_CREATE: 
                hwndNextViewer = SetClipboardViewer(hwnd); 
                break; 
            case WM_CHANGECBCHAIN: 
                if ((HWND) wParam == hwndNextViewer) 
                    hwndNextViewer = (HWND) lParam; 
                else if (hwndNextViewer != NULL) 
                    SendMessage(hwndNextViewer, uMsg, wParam, lParam); 
                break; 
            case WM_DESTROY: 
                ChangeClipboardChain(hwnd, hwndNextViewer); 
                PostQuitMessage(0); 
                break; 
            case WM_DRAWCLIPBOARD:  
                SetAutoView(hwnd); 
                SendMessage(hwndNextViewer, uMsg, wParam, lParam); 
                break; 
            default: 
                return DefWindowProc(hwnd, uMsg, wParam, lParam); 
        } 
        return (LRESULT) NULL; 
    } 
    
    BOOL WINAPI IsDisplayableFormat(INT iFormat) 
    { 
        switch (iFormat) 
        { 
            case CF_OWNERDISPLAY: 
            case CF_TEXT: 
            case CF_ENHMETAFILE: 
            case CF_BITMAP: 
                return TRUE; 
        } 
        return FALSE; 
    } 
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = MainWndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = ClipboardCaptureName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            ClipboardCaptureName,
            "My clipboard capture window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  3. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  4. Copy Function changing value?
    By GMHummerH1 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2004, 10:04 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM