-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClipWatch.cpp
More file actions
181 lines (164 loc) · 4.81 KB
/
ClipWatch.cpp
File metadata and controls
181 lines (164 loc) · 4.81 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <windows.h>
#include <Shlobj.h>
bool DisplayClipboard(const UINT format)
{
bool found = false;
HGLOBAL hClip = GetClipboardData(format);
if (hClip != NULL)
{
found = true;
switch (format)
{
default:
found = false;
break;
case CF_OEMTEXT:
case CF_TEXT:
{
PCSTR const buffer = (PCSTR) GlobalLock(hClip);
puts(buffer);
GlobalUnlock(hClip);
}
break;
case CF_UNICODETEXT:
{
PCWSTR const buffer = (PCWSTR) GlobalLock(hClip);
_putws(buffer);
GlobalUnlock(hClip);
}
break;
case CF_BITMAP:
{
HBITMAP hBitmap = (HBITMAP) hClip;
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
wprintf(L"Bitmap: %dx%d bpp:%d\n", bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel);
GlobalUnlock(hClip);
}
break;
case CF_DIB:
{
BITMAPINFO* pBitmapInfo = (BITMAPINFO*) GlobalLock(hClip);
wprintf(L"DIB: %dx%d bitcount:%d\n", pBitmapInfo->bmiHeader.biWidth, pBitmapInfo->bmiHeader.biHeight, pBitmapInfo->bmiHeader.biBitCount);
GlobalUnlock(hClip);
}
break;
case CF_HDROP:
{
DROPFILES* pData = (DROPFILES*) GlobalLock(hClip);
if (pData->fWide)
{
wchar_t *pchData = (wchar_t*) ((char*) pData + pData->pFiles);
while (*pchData != L'\0')
{
wprintf(pchData);
wprintf(L"\n");
pchData += wcslen(pchData) + 1;
}
}
else
{
char *pchData = (char*) ((char*) pData + pData->pFiles);
while (*pchData != L'\0')
{
printf(pchData);
printf("\n");
pchData += strlen(pchData) + 1;
}
}
GlobalUnlock(hClip);
}
break;
}
}
return found;
}
BOOL OpenClipboardRetry(HWND hWndNewOwner)
{
BOOL r;
while ((r = OpenClipboard(hWndNewOwner)) == FALSE)
{
if (GetLastError() != ERROR_ACCESS_DENIED)
break;
Sleep(100);
}
return r;
}
LRESULT CALLBACK ClipWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND hClipOwner;
switch (uMsg)
{
case WM_CREATE:
AddClipboardFormatListener(hWnd);
return TRUE;
case WM_DESTROY:
RemoveClipboardFormatListener(hWnd);
return TRUE;
case WM_CLIPBOARDUPDATE:
hClipOwner = GetClipboardOwner();
printf("hClipOwner: 0x%08x\n", (UINT) (INT_PTR) hClipOwner);
if (OpenClipboardRetry(hWnd))
{
hClipOwner = GetClipboardOwner();
printf("hClipOwner: 0x%08x\n", (UINT) (INT_PTR) hClipOwner);
bool found = false;
int index = 0;
UINT format;
while (!found && (format = EnumClipboardFormats(index)) > 0)
{
//wprintf(L"Format: %#.4x\n", format);
found = DisplayClipboard(format);
++index;
}
if (!found)
{
UINT f[] = { CF_UNICODETEXT, CF_BITMAP, CF_HDROP };
format = GetPriorityClipboardFormat(f, ARRAYSIZE(f));
if (format > 0)
{
//wprintf(L"Priority Format: %#.4x\n", format);
DisplayClipboard(format);
}
}
_putws(L"---");
CloseClipboard();
}
else
printf("Error OpenClipboard: %d\n", GetLastError());
return !0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
};
}
ATOM Register(HINSTANCE hInst)
{
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = ClipWndProc;
wx.hInstance = hInst;
wx.lpszClassName = TEXT("RADCLIPWATCH");
return RegisterClassEx(&wx);
}
void main()
{
HINSTANCE hInst = GetModuleHandle(NULL);
ATOM a = Register(hInst);
/*HWND hWnd =*/ CreateWindow((LPCTSTR) a, nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInst, nullptr);
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
fwprintf(stderr, L"Error: GetMessage %d\n", GetLastError());
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}