-
Notifications
You must be signed in to change notification settings - Fork 1
/
Clipboard.cpp
99 lines (87 loc) · 2.21 KB
/
Clipboard.cpp
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
// Copyleft 2004 Chris Korda
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or any later version.
/*
chris korda
revision history:
rev date comments
00 21nov03 initial version
01 10jan05 add ownership
generic read/write interface for windows clipboard
*/
#include "stdafx.h"
#include "Clipboard.h"
const DWORD CClipboard::HDRSIZE = sizeof(DWORD) + sizeof(GUID);
CClipboard::CClipboard(HWND hWnd, LPCSTR Format) :
m_hWnd(hWnd)
{
m_hWnd = NULL;
SetFormat(Format);
memset(&m_Owner, 0, sizeof(GUID));
}
CClipboard::~CClipboard()
{
}
void CClipboard::SetFormat(LPCSTR Format)
{
m_Format = (Format != NULL ? RegisterClipboardFormat(Format) : 0);
}
void CClipboard::SetOwner(LPGUID Owner)
{
memcpy(&m_Owner, Owner, sizeof(GUID));
}
bool CClipboard::Write(const LPVOID Data, DWORD Length) const
{
bool retc = FALSE;
if (m_Format) {
if (OpenClipboard(m_hWnd)) {
if (EmptyClipboard()) {
HGLOBAL gmem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
HDRSIZE + Length);
if (gmem != NULL) {
BYTE *p = (BYTE *)GlobalLock(gmem);
if (p != NULL) {
*((DWORD *)p) = Length;
CopyMemory(p + sizeof(DWORD), &m_Owner, sizeof(GUID));
CopyMemory(p + HDRSIZE, Data, Length);
GlobalUnlock(p);
if (SetClipboardData(m_Format, gmem) != NULL)
retc = TRUE;
} else
GlobalFree(gmem);
}
}
}
}
CloseClipboard();
return(retc);
}
LPVOID CClipboard::Read(DWORD& Length, LPGUID Owner) const
{
const int HDRSIZE = sizeof(DWORD) + sizeof(GUID);
LPVOID Data = NULL;
Length = 0;
if (m_Format) {
if (OpenClipboard(m_hWnd)) {
HANDLE gmem = GetClipboardData(m_Format);
if (gmem != NULL) {
BYTE *p = (BYTE *)GlobalLock(gmem);
if (p != NULL) {
Length = *((DWORD *)p);
Data = new BYTE[Length];
if (Owner != NULL)
CopyMemory(Owner, p + sizeof(DWORD), sizeof(GUID));
CopyMemory(Data, p + HDRSIZE, Length);
GlobalUnlock(p);
}
}
}
}
CloseClipboard();
return(Data);
}
bool CClipboard::HasData() const
{
return(IsClipboardFormatAvailable(m_Format) != 0);
}