December 11, 2024, 09:38:36 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CClipboard Class

Started by Mike Stefanik, March 04, 2006, 05:59:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mike Stefanik

I needed to do some clipboard manipulation, so I created a CClipboard class that I thought others might find useful. The simplest way to use it to set and get text off the clipboard is:


CClipboard clipboard;
string strValue;

clipboard.Open();
clipboard.SetText("This is some text I'm putting on the clipboard");
strValue = clipboard.GetText();
clipboard.Close();


Clipboard.inc

// CClipboard Class Definition

#ifndef _INC_CLASS_CCLIPBOARD
#define _INC_CLASS_CCLIPBOARD

#define CF_TEXT             1
#define CF_BITMAP           2
#define CF_METAFILEPICT     3
#define CF_SYLK             4
#define CF_DIF              5
#define CF_TIFF             6
#define CF_OEMTEXT          7
#define CF_DIB              8
#define CF_PALETTE          9
#define CF_PENDATA          10
#define CF_RIFF             11
#define CF_WAVE             12
#define CF_UNICODETEXT      13
#define CF_ENHMETAFILE      14

#define GMEM_FIXED          0x0000
#define GMEM_MOVEABLE       0x0002
#define GMEM_NOCOMPACT      0x0010
#define GMEM_NODISCARD      0x0020
#define GMEM_ZEROINIT       0x0040
#define GMEM_MODIFY         0x0080
#define GMEM_DISCARDABLE    0x0100
#define GMEM_NOT_BANKED     0x1000
#define GMEM_SHARE          0x2000
#define GMEM_DDESHARE       0x2000
#define GMEM_NOTIFY         0x4000
#define GMEM_LOWER          GMEM_NOT_BANKED
#define GMEM_VALID_FLAGS    0x7F72
#define GMEM_INVALID_HANDLE 0x8000

#define GHND                (GMEM_MOVEABLE | GMEM_ZEROINIT)
#define GPTR                (GMEM_FIXED | GMEM_ZEROINIT)

declare import,OpenClipboard(unsigned int hWndOwner), int;
declare import,CloseClipboard(), int;
declare import,CountClipboardFormats(), int;
declare import,EmptyClipboard(), int;
declare import,EnumClipboardFormats(unsigned int nFormat), unsigned int;
declare import,GetClipboardData(unsigned int nFormat), unsigned int;
declare import,GetClipboardFormatNameA(unsigned int nFormat, pointer lpFormatName, int nMaxLength), int;
declare import,GetClipboardOwner(), unsigned int;
declare import,GetOpenClipboardWindow(), unsigned int;
declare import,IsClipboardFormatAvailable(unsigned int nFormat), int;
declare import,RegisterClipboardFormatA(string strFormat), unsigned int;
declare import,SetClipboardData(unsigned int nFormat, unsigned int hMem), unsigned int;
declare import,GlobalAlloc(unsigned int uFlags, unsigned int dwBytes), unsigned int;
declare import,GlobalLock(unsigned int hMem), pointer;
declare import,GlobalUnlock(unsigned int hMem), int;
declare import,GlobalFree(unsigned int hMem), unsigned int;
declare import,GlobalSize(unsigned int hMem), unsigned int;
declare import,RtlMoveMemory(pointer lpDestination, pointer lpSource, unsigned int nLength);
declare import,RtlZeroMemory(pointer lpBuffer, unsigned int nLength);
declare import,lstrlenA(pointer lpString), int;

class CClipboard
{
    declare CClipboard();
    declare _CClipboard();

    declare Open(opt unsigned int hWndOwner = 0);
    declare Close(), int;
    declare Empty(), int;
    declare GetData(unsigned int nFormat, unsigned int *phMem), int;
    declare GetFirstFormat(), unsigned int;
    declare GetFormatCount(), int;
    declare GetFormatName(unsigned int nFormat, string strFormat), int;
    declare GetNextFormat(), unsigned int;
    declare GetOpenWindow(), unsigned int;
    declare GetOwner(), unsigned int;
    declare GetText(), heap;
    declare IsFormatAvailable(unsigned int nFormat), int;
    declare RegisterFormat(string strFormat), unsigned int;
    declare SetData(unsigned int nFormat, unsigned int hMem), int;
    declare SetText(pointer lpText, opt int nLength = -1), int;

    int m_bOpened;
    unsigned int m_hClipboard;
    unsigned int m_nFormat;
}
#endif


Clipboard.src

// CClipboard Class Implementation
#include "clipboard.inc"

CClipboard::CClipboard()
{
    m_bOpened = false;
    m_hClipboard = 0;
    m_nFormat = 0;
    return;
}

CClipboard::_CClipboard()
{
    if (m_bOpened)
    {
        CloseClipboard();
    }
    return;
}

CClipboard::Open(unsigned int hWndOwner)
{
    if (OpenClipboard(hWndOwner) != 0)
    {
        m_bOpened = true;
        return true;
    }

    return false;
}

CClipboard::Close(), int
{
    if (m_bOpened)
    {
        CloseClipboard();
        m_bOpened = false;
        return true;
    }

    return false;
}

CClipboard::Empty(), int
{
    if (m_bOpened)
    {
        EmptyClipboard();
        return true;
    }

    return false;
}

CClipboard::GetData(unsigned int nFormat, unsigned int *phMem), int
{
    if (phMem = null)
        return false;

    if (m_bOpened)
    {
        *(unsigned int)phMem = GetClipboardData(nFormat);
        if (*(unsigned int)phMem != 0)
        {
            return true;
        }
    }

    return false;
}

CClipboard::GetFirstFormat()
{
    if (m_bOpened)
    {
        m_nFormat = EnumClipboardFormats(0);
        return m_nFormat;
    }

    return 0;
}

CClipboard::GetFormatCount(), int
{
    return CountClipboardFormats();
}

CClipboard::GetFormatName(int nFormat, string strFormat), int
{
    if (m_bOpened)
    {
        int nLength;
        nLength = GetClipboardFormatNameA(nFormat, strFormat, 255);

        if (nLength > 0)
            return true;
    }

    return false;
}

CClipboard::GetNextFormat(), unsigned int
{
    if (m_bOpened)
    {
        m_nFormat = EnumClipboardFormats(m_nFormat);
        return m_nFormat;
    }

    return 0;
}

CClipboard::GetOpenWindow(), unsigned int
{
    return GetOpenClipboardWindow();
}

CClipboard::GetOwner(), unsigned int
{
    return GetClipboardOwner();
}

CClipboard::GetText(), heap
{
    pointer lpText = null;
    pointer lpData = null;
    unsigned int hMem = 0;
    int nLength = 0;
    int nIndex;

    if (m_bOpened)
    {
        hMem = GetClipboardData(CF_TEXT);
        if (hMem != 0)
        {
            lpData = GlobalLock(hMem);
            if (lpData != null)
                nLength = lstrlenA(lpData);
        }
    }

    lpText = AllocHeap(nLength + 1);
    RtlZeroMemory(lpText, nLength + 1);

    if (lpData != null and nLength > 0)
    {
        RtlMoveMemory(lpText, lpData, nLength);
    }

    if (hMem != 0)
    {
        GlobalUnlock(hMem);
    }

    return *(string)lpText;
}

CClipboard::IsFormatAvailable(unsigned int nFormat), int
{
    if (IsClipboardFormatAvailable(nFormat) != 0)
        return true;

    return false;
}

CClipboard::RegisterFormat(string strFormat), unsigned int
{
    return RegisterClipboardFormatA(strFormat);
}

CClipboard::SetData(int nFormat, unsigned int hMem), int
{
    if (m_bOpened)
    {
        m_hClipboard = SetClipboardData(nFormat, hMem);
        if (m_hClipboard != 0)
        {
            return true;
        }
    }

    return false;
}

CClipboard::SetText(pointer lpText, int nLength), int
{
    unsigned int hMem = 0;
    pointer lpMem = null;

    if (m_bOpened)
    {
        if (nLength = -1)
            nLength = lstrlenA(lpText);

        EmptyClipboard();

        if (nLength = 0)
            return true;

        hMem = GlobalAlloc(GHND, nLength + 1);
        if (hMem = 0)
            return false;

        lpMem = GlobalLock(hMem);
        if (lpMem = 0)
            return false;

        RtlMoveMemory(lpMem, lpText, nLength + 1);
        GlobalUnlock(hMem);
        SetClipboardData(CF_TEXT, hMem);
        return true;
    }

    return false;
}
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation