2011年5月16日星期一

  剪贴板使用示例

拷贝信息到剪贴板:
1. 调用OpenClipboard打开剪帖板
2. 调用EmptyClipboard清空剪贴板
3. 调用SetClipboardData提供程序数据
4. 调用CloseClipboard关闭剪贴板

GlobalAlloc 函数在全局堆上分配内存,  指定GMEM_MOVEABLE使其可移动到默认堆.

#include
#include


#define ID_COPY   150
#define ID_PASTE  151
#define ID_TEXT   152
#define ID_VIEW   153

#define HK_CTRL_C 1

#define MAX_STRING 1024


LPWORD lpwAlign(LPWORD lpIn)
{
    ULONG ul;

    ul = (ULONG)lpIn;
    ul +=3;
    ul >>=2;
    ul <<=2;
    return (LPWORD)ul;
}

void CopyStringToChilpboard(HWND hwnd, LPCSTR lpString)
{
    if (lpString == NULL)
        return;

    size_t size = strlen(lpString) + 1;
    HGLOBAL hglb = GlobalAlloc(GHND, size);
    if (hglb == NULL) return;
    LPSTR buffer = (LPSTR)GlobalLock(hglb);

    if (buffer)
    {
        strcpy(buffer, lpString);
        GlobalUnlock(hglb);

        if (OpenClipboard(hwnd))
        {
            EmptyClipboard();
            SetClipboardData(CF_TEXT, hglb);
            CloseClipboard();
        }
    }
}

BOOL PasteStringFromChilpboard(HWND hwnd, LPSTR lpString, LPDWORD lpSize)
{
    BOOL fOk = FALSE;

    if (IsClipboardFormatAvailable(CF_TEXT))
    {
        if (OpenClipboard(hwnd))
        {
            HGLOBAL hglb = GetClipboardData(CF_TEXT);

            if (hglb)
            {
                LPCSTR buffer = (LPCSTR)GlobalLock(hglb);

                if (buffer && lpSize)
                {
                    DWORD size = strlen(buffer) + 1;

                    if (size <= *lpSize && lpString)
                        strcpy(lpString, buffer);
                       
                    *lpSize = size;
                    GlobalUnlock(hglb);
                    fOk = TRUE;
                }
            }
            CloseClipboard();
        }
    }
    return fOk;
}

INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    char buffer[MAX_STRING] = {0};
    DWORD length = MAX_STRING;

    switch (uMsg)
    {
    case WM_INITDIALOG:
        RegisterHotKey(hwndDlg, HK_CTRL_C, MOD_CONTROL, 0x43);  /* handle Ctrl+C */
        return TRUE;

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDCANCEL:
            UnregisterHotKey(hwndDlg, HK_CTRL_C);
            EndDialog(hwndDlg, wParam);
            return TRUE;

        case ID_PASTE:
            PasteStringFromChilpboard(hwndDlg, buffer, &length);
            SetDlgItemTextA(hwndDlg, ID_VIEW, buffer);
            return TRUE;

        case ID_COPY:
            GetDlgItemTextA(hwndDlg, ID_TEXT, buffer, MAX_STRING);
            CopyStringToChilpboard(hwndDlg, buffer);
            return TRUE;
        }
        break;

    case WM_HOTKEY:
        if (wParam == HK_CTRL_C &&
            HIWORD(lParam) == 0x43 &&
            LOWORD(lParam) == MOD_CONTROL)
        {
            GetDlgItemTextA(hwndDlg, ID_TEXT, buffer, MAX_STRING);
            CopyStringToChilpboard(hwndDlg, buffer);
            return TRUE;
        }
        break;
    }
    return FALSE;
}

LPWORD DefineStaticText(LPWORD lpw, WORD id, LPCSTR msg, short x, short y, short width, short height)
{
    LPDLGITEMTEMPLATE lpdit;

    lpw = lpwAlign(lpw);  /* align DLGITEMTEMPLATE on DWORD boundary */
    lpdit = (LPDLGITEMTEMPLATE) lpw;
    lpdit->x  = x;
    lpdit->y  = y;
    lpdit->cx = width;
    lpdit->cy = height;
    lpdit->id = id;      /* text identifier */
    lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0082;     /* static class */

    LPWSTR lpwsz = (LPWSTR)lpw;
    int nchar = MultiByteToWideChar(CP_ACP, 0, msg, -1, lpwsz, strlen(msg) + 1);
    lpw += nchar;
    *lpw++ = 0;            /* no creation data */
    return lpw;
}

LPWORD DefineEdit(LPWORD lpw, WORD id, LPCSTR caption, short x, short y, short width, short height, DWORD styles)
{
    LPDLGITEMTEMPLATE lpdit;

    lpw = lpwAlign(lpw);   /* align DLGITEMTEMPLATE on DWORD boundary */
    lpdit = (LPDLGITEMTEMPLATE) lpw;
    lpdit->x  = x;
    lpdit->y  = y;
    lpdit->cx = width;
    lpdit->cy = height;
    lpdit->id = id;        /* text identifier */
    lpdit->style = ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE | styles ;

    lpw = (LPWORD) (lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0081;       /* edit class */

    LPWSTR lpwsz = (LPWSTR)lpw;
    int nchar = MultiByteToWideChar(CP_ACP, 0, caption, -1, lpwsz, 50);
    lpw += nchar;
    *lpw++ = 0;            /* no creation data */
    return lpw;
}

LPWORD DefineButton(LPWORD lpw, WORD id, LPCSTR caption, short x, short y, short width, short height)
{
    LPDLGITEMTEMPLATE lpdit;

    lpw = lpwAlign(lpw);   /* align DLGITEMTEMPLATE on DWORD boundary */
    lpdit = (LPDLGITEMTEMPLATE)lpw;
    lpdit->x  = x;
    lpdit->y  = y;
    lpdit->cx = width;
    lpdit->cy = height;
    lpdit->id = id;
    lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;

    lpw = (LPWORD)(lpdit + 1);
    *lpw++ = 0xFFFF;
    *lpw++ = 0x0080;      /* button class */

    LPWSTR lpwsz = (LPWSTR)lpw;
    int nchar = MultiByteToWideChar(CP_ACP, 0, caption, -1, lpwsz, 50);
    lpw += nchar;
    *lpw++ = 0;            /* no creation data */
    return lpw;
}

/*
*  Define a dialog box.
*/
LPWORD DefineDialog(LPWORD lpw, WORD cdit, LPCSTR caption, short x, short y, short width, short height)
{
    LPDLGTEMPLATE lpdt;

    lpw = lpwAlign(lpw);      /* align DLGITEMTEMPLATE on DWORD boundary */
    lpdt = (LPDLGTEMPLATE)lpw;
    lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
    lpdt->x     = x;
    lpdt->y     = y;
    lpdt->cx    = width;
    lpdt->cy    = height;
    lpdt->cdit  = cdit;      /* number of controls */

    lpw = (LPWORD)(lpdt + 1);
    *lpw++ = 0;   /* no menu */
    *lpw++ = 0;   /* predefined dialog box class (by default) */

    LPWSTR lpwsz = (LPWSTR)lpw;
    int nchar = MultiByteToWideChar(CP_ACP, 0, caption, -1, lpwsz, 50);
    lpw += nchar;
    return lpw;
}

int CALLBACK WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpCmdLine, int nShowCmd)
{
    HGLOBAL hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
    int ret = -1;

    if (hgbl == NULL)
        return -1;

    LPWORD lpw = (LPWORD)GlobalLock(hgbl);

    if (lpw)
    {
        lpw = DefineDialog(lpw, 4, "Chilpboard Sample", 10, 10, 200, 190);
        lpw = DefineButton(lpw, ID_PASTE, "Paste", 110, 160, 80, 15);
        lpw = DefineButton(lpw, ID_COPY, "Copy", 10, 160, 80, 15);
        lpw = DefineEdit(lpw, ID_TEXT, "Source", 10, 10, 90, 140, ES_MULTILINE);
        lpw = DefineEdit(lpw, ID_VIEW, "Destination", 100, 10, 90, 140, ES_MULTILINE|ES_READONLY);

        GlobalUnlock(hgbl);
        ret = DialogBoxIndirect(hInst, (LPDLGTEMPLATE)hgbl, NULL, (DLGPROC)DialogProc);

        if (ret == -1)
        {
            char error[50] = {0};
            sprintf(error, "Create dialog failed. (%d)\n", GetLastError());
            OutputDebugStringA(error);
        }
        GlobalFree(hgbl);
    }
    return ret;
}

没有评论:

发表评论