-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathWinUtil.cpp
More file actions
98 lines (78 loc) · 2.75 KB
/
WinUtil.cpp
File metadata and controls
98 lines (78 loc) · 2.75 KB
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
//////////////////////////////////////////////////////////////////////////////////////////
// File: WinUtil.h
//////////////////////////////////////////////////////////////////////////////////////////
// Description: WinUtil class
// Project: GUI Library
// Author(s): Jason Boettcher
// jackal@shplorb.com
// www.shplorb.com/~jackal
//////////////////////////////////////////////////////////////////////////////////////////
// Inclusions of header files
#include <assert.h>
#include <windows.h>
#include "WinUtil.h"
using namespace std;
using namespace RTE;
//////////////////////////////////////////////////////////////////////////////////////////
// Method: GetClipboardText
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Gets the text from the clipboard.
bool WinUtil::GetClipboardText(string *Text)
{
HANDLE CBDataHandle; // handle to the clipboard data
LPSTR CBDataPtr; // pointer to data to send
// Check the pointer
assert(Text);
// Does the clipboard contain text?
if (IsClipboardFormatAvailable(CF_TEXT))
{
// Open the clipboard
if (OpenClipboard(0))
{
CBDataHandle = GetClipboardData(CF_TEXT);
if (CBDataHandle)
{
CBDataPtr = (LPSTR)GlobalLock(CBDataHandle);
int TextSize = strlen(CBDataPtr);
// Insert the text
Text->erase();
Text->insert(0, CBDataPtr);
CloseClipboard();
GlobalUnlock(CBDataHandle);
return true;
}
CloseClipboard();
}
}
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////
// Method: SetClipboardText
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Sets the text in the clipboard.
bool WinUtil::SetClipboardText(string Text)
{
// Open the clipboard
if (OpenClipboard(0))
{
// Allocate global memory for the text
HGLOBAL hMemory = GlobalAlloc(GMEM_MOVEABLE, Text.size()+1);
if (hMemory == 0)
{
CloseClipboard();
return false;
}
// Empty the clipboard
EmptyClipboard();
// Copy the text into memory
char *CText = (char *)GlobalLock(hMemory);
memcpy(CText, Text.c_str(), Text.size());
CText[Text.size()] = '\0';
GlobalUnlock(hMemory);
// Set the data
SetClipboardData(CF_TEXT, hMemory);
CloseClipboard();
return true;
}
return false;
}