forked from keizi666/charu3
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
clipboard.cpp
97 lines (89 loc) · 2.53 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
/*----------------------------------------------------------
ClipBoardƒNƒ‰ƒX
2002/11/16 (c)Keizi
----------------------------------------------------------*/
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
#include "clipboard.h"
bool CClipBoard::SetClipboardText(const CString sData, int retryCount, int retryInterval)
{
size_t len = sData.GetLength() + 1;
TCHAR* newText = new TCHAR[len];
_tcscpy_s(newText, len, sData);
bool isRet = false;
UINT uFormat;
#ifdef _UNICODE
uFormat = CF_UNICODETEXT;
#else
uFormat = CF_TEXT;
#endif
do {
if (::OpenClipboard(m_hParentWnd)) {
StopListener();
HGLOBAL hMem = ::GlobalAlloc(GHND, len * sizeof(TCHAR));
if (hMem) {
LPVOID pPtr = ::GlobalLock(hMem);
if (pPtr) {
_tcscpy_s(static_cast<TCHAR*>(pPtr), len, newText);
::GlobalUnlock(hMem);
::EmptyClipboard();
HANDLE hRet = ::SetClipboardData(uFormat, hMem);
if (hRet) {
isRet = true;
}
}
else {
::GlobalFree(hMem);
}
}
::CloseClipboard();
StartListener();
isRet = true;
break;
}
if (retryCount < 1) {
break;
}
retryCount--;
Sleep(retryInterval);
} while (true);
delete[] newText;
return isRet;
}
bool CClipBoard::GetClipboardText(CString& sData, int retryCount, int retryInterval)
{
bool isRet = false;
UINT uFormat;
#ifdef _UNICODE
uFormat = CF_UNICODETEXT;
#else
uFormat = CF_TEXT;
#endif
if (::IsClipboardFormatAvailable(uFormat)) {
do {
if (::OpenClipboard(m_hParentWnd)) {
HGLOBAL hGet = ::GetClipboardData(uFormat);
if (hGet) {
LPVOID pPtr = ::GlobalLock(hGet);
if (pPtr) {
sData = CString(static_cast<TCHAR*>(pPtr));
isRet = true;
::GlobalUnlock(hGet);
}
}
::CloseClipboard();
break;
}
if (retryCount < 1) {
break;
}
retryCount--;
Sleep(retryInterval);
} while (true);
}
return isRet;
}