black_layered.cpp
7.53 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#include <time.h>
HWND hwnd;
HINSTANCE hInst;
# define LWA_COLORKEY 1
# define LWA_ALPHA 2
# define WS_EX_LAYERED 0x80000
int wd=0;
int ht=0;
HBITMAP
DoGetBkGndBitmap2(
IN CONST UINT uBmpResId
)
{
static HBITMAP hbmBkGnd = NULL;
if (NULL == hbmBkGnd)
{
hbmBkGnd = (HBITMAP)LoadImage(NULL, "background.bmp", IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE);
BITMAPINFOHEADER h2;
h2.biSize=sizeof(h2);
h2.biBitCount=0;
// h2.biWidth=11; h2.biHeight=22; h2.biPlanes=1;
HDC hxdc=CreateDC("DISPLAY",NULL,NULL,NULL);
GetDIBits(hxdc, hbmBkGnd, 0, 0, NULL, (BITMAPINFO*)&h2, DIB_RGB_COLORS);
wd=h2.biWidth; ht=h2.biHeight;
DeleteDC(hxdc);
// hbmBkGnd = (HBITMAP)LoadImage(
// GetModuleHandle(NULL), MAKEINTRESOURCE(uBmpResId),
// IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if (NULL == hbmBkGnd)
hbmBkGnd = (HBITMAP)-1;
}
return (hbmBkGnd == (HBITMAP)-1)
? NULL : hbmBkGnd;
}
BOOL
DoSDKEraseBkGnd2(
IN CONST HDC hDC,
IN CONST COLORREF crBkGndFill
)
{
HBITMAP hbmBkGnd = DoGetBkGndBitmap2(0);
if (hDC && hbmBkGnd)
{
RECT rc;
if ((ERROR != GetClipBox(hDC, &rc)) && !IsRectEmpty(&rc))
{
HDC hdcMem = CreateCompatibleDC(hDC);
if (hdcMem)
{
HBRUSH hbrBkGnd = CreateSolidBrush(crBkGndFill);
if (hbrBkGnd)
{
HGDIOBJ hbrOld = SelectObject(hDC, hbrBkGnd);
if (hbrOld)
{
SIZE size = {
(rc.right-rc.left), (rc.bottom-rc.top)
};
if (PatBlt(hDC, rc.left, rc.top, size.cx, size.cy, PATCOPY))
{
HGDIOBJ hbmOld = SelectObject(hdcMem, hbmBkGnd);
if (hbmOld)
{
StretchBlt(hDC,
0,
0,
size.cx,
size.cy,
hdcMem,
0,
0,
wd,
ht,
SRCCOPY);
/* BitBlt(hDC, rc.left, rc.top, size.cx, size.cy,
hdcMem, rc.left, rc.top, SRCCOPY);*/
SelectObject(hdcMem, hbmOld);
}
}
SelectObject(hDC, hbrOld);
}
DeleteObject(hbrBkGnd);
}
DeleteDC(hdcMem);
}
}
}
return TRUE;
}
static LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam) // second message parameter
{
switch (uMsg)
{
case WM_CREATE:
// SetTimer(hwnd,10,30000,NULL);
SetTimer(hwnd,100,2000,NULL);
break;
case WM_TIMER:
if (wParam==100) SetWindowPos(hwnd,HWND_TOPMOST,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN), NULL);
// if (wParam==10) DestroyWindow(hwnd);
case WM_ERASEBKGND:
{
DoSDKEraseBkGnd2((HDC)wParam, RGB(0,0,0));
return true;
}
case WM_CTLCOLORSTATIC:
{
SetBkMode((HDC) wParam, TRANSPARENT);
return (DWORD) GetStockObject(NULL_BRUSH);
}
case WM_DESTROY:
KillTimer(hwnd,100);
PostQuitMessage (0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
bool
create_window(void)
{
WNDCLASSEX wndClass;
ZeroMemory (&wndClass, sizeof (wndClass));
wndClass.cbSize = sizeof (wndClass);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInst;
wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndClass.hIconSm = NULL;
wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "blackscreen";
if (!RegisterClassEx(&wndClass)) {
// return false;
}
RECT clientRect;
clientRect.left = 0;
clientRect.top = 0;
clientRect.right = 640;
clientRect.bottom = 480;
AdjustWindowRect (&clientRect, WS_CAPTION, FALSE);
hwnd = CreateWindowEx (0,
"blackscreen",
"blackscreen",
WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN|WS_BORDER,
CW_USEDEFAULT,
CW_USEDEFAULT,
clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top,
NULL,
NULL,
hInst,
NULL);
typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);
PSLWA pSetLayeredWindowAttributes;
/*
* Code that follows allows the program to run in
* environment other than windows 2000
* without crashing only difference being that
* there will be no transparency as
* the SetLayeredAttributes function is available only in
* windows 2000
*/
HMODULE hDLL = LoadLibrary ("user32");
pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");
/*
* Make the windows a layered window
*/
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_DLGFRAME | WS_THICKFRAME);
SetWindowLong(hwnd, GWL_STYLE, style);
if (pSetLayeredWindowAttributes != NULL) {
SetWindowLong (hwnd, GWL_EXSTYLE, GetWindowLong
(hwnd, GWL_EXSTYLE) |WS_EX_LAYERED|WS_EX_TRANSPARENT );
ShowWindow (hwnd, SW_SHOWNORMAL);
}
if (pSetLayeredWindowAttributes != NULL) {
/**
* Second parameter RGB(255,255,255) sets the colorkey to white
* LWA_COLORKEY flag indicates that color key is valid
* LWA_ALPHA indicates that ALphablend parameter (factor)
* is valid
*/
pSetLayeredWindowAttributes (hwnd, RGB(255,255,255), 255, LWA_ALPHA);
}
SetWindowPos(hwnd,HWND_TOPMOST,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED|SWP_NOACTIVATE);
//SM_CXVIRTUALSCREEN
return true;
}
DWORD WINAPI BlackWindow(LPVOID lpParam)
{
// TODO: Place code here.
create_window();
MSG msg;
while (GetMessage(&msg,0,0,0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}