| 1 |
// Unicode support by Jim Park -- 08/22/2007 |
| 2 |
// For layered windows |
| 3 |
#define _WIN32_WINNT 0x0500 |
| 4 |
|
| 5 |
#include <windows.h> |
| 6 |
#include <windowsx.h> |
| 7 |
#include <nsis/pluginapi.h> // nsis plugin |
| 8 |
|
| 9 |
#ifndef LWA_COLORKEY |
| 10 |
# define LWA_COLORKEY 1 |
| 11 |
# define LWA_ALPHA 2 |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
#define RESOLUTION 32 // 30 fps ;) (32? I like SHR more than iDIV ;) |
| 16 |
|
| 17 |
HINSTANCE g_hInstance; |
| 18 |
HBITMAP g_hbm; |
| 19 |
BITMAP bm; |
| 20 |
int g_rv; |
| 21 |
int resolution; |
| 22 |
int sleep_val, fadein_val, fadeout_val, state, timeleft, keycolor, alphaparam; |
| 23 |
const TCHAR classname[4] = _T("_sp"); |
| 24 |
|
| 25 |
typedef BOOL(WINAPI*SetLayeredWindowAttributes_T)(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags); |
| 26 |
SetLayeredWindowAttributes_T SetLayeredWindowAttributesProc; |
| 27 |
#define IsLayeredWnd() ( SetLayeredWindowAttributesProc != NULL ) |
| 28 |
|
| 29 |
static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 30 |
{ |
| 31 |
PAINTSTRUCT ps; |
| 32 |
RECT r; |
| 33 |
HDC curdc = NULL; |
| 34 |
HDC hdc; |
| 35 |
HBITMAP oldbm; |
| 36 |
|
| 37 |
switch (uMsg) { |
| 38 |
case WM_CREATE: |
| 39 |
SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); |
| 40 |
SetWindowLongPtr(hwnd, GWL_STYLE, 0); |
| 41 |
SetWindowPos(hwnd, NULL, |
| 42 |
r.left + (r.right - r.left - bm.bmWidth) / 2, |
| 43 |
r.top + (r.bottom - r.top - bm.bmHeight) / 2, |
| 44 |
bm.bmWidth, bm.bmHeight, SWP_NOZORDER | SWP_SHOWWINDOW); |
| 45 |
return 0; |
| 46 |
|
| 47 |
case WM_PAINT: |
| 48 |
curdc = BeginPaint(hwnd, &ps); |
| 49 |
hdc = CreateCompatibleDC(curdc); |
| 50 |
GetClientRect(hwnd, &r); |
| 51 |
|
| 52 |
oldbm = SelectObject(hdc, g_hbm); |
| 53 |
BitBlt(curdc, r.left, r.top, r.right - r.left, r.bottom - r.top, hdc, |
| 54 |
0, 0, SRCCOPY); |
| 55 |
|
| 56 |
SelectObject(hdc, oldbm); |
| 57 |
DeleteDC(hdc); |
| 58 |
EndPaint(hwnd, &ps); |
| 59 |
|
| 60 |
case WM_CLOSE: |
| 61 |
return 0; |
| 62 |
|
| 63 |
case WM_TIMER: |
| 64 |
case WM_LBUTTONDOWN: |
| 65 |
g_rv = (uMsg == WM_LBUTTONDOWN); |
| 66 |
DestroyWindow(hwnd); |
| 67 |
break; |
| 68 |
} |
| 69 |
return DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 70 |
} |
| 71 |
|
| 72 |
void SetTransparentRegion(HWND myWnd) |
| 73 |
{ |
| 74 |
HDC dc; |
| 75 |
int x, y; |
| 76 |
HRGN region, cutrgn; |
| 77 |
BITMAPINFO bmi; |
| 78 |
int size = bm.bmWidth * bm.bmHeight * 4; |
| 79 |
int *bmp = GlobalAlloc(GPTR, size); |
| 80 |
int *bmp_orig = bmp; |
| 81 |
bmi.bmiHeader.biBitCount = 32; |
| 82 |
bmi.bmiHeader.biCompression = BI_RGB; |
| 83 |
bmi.bmiHeader.biHeight = bm.bmHeight; |
| 84 |
bmi.bmiHeader.biPlanes = 1; |
| 85 |
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 86 |
bmi.bmiHeader.biWidth = bm.bmWidth; |
| 87 |
bmi.bmiHeader.biClrUsed = 0; |
| 88 |
bmi.bmiHeader.biClrImportant = 0; |
| 89 |
|
| 90 |
dc = CreateCompatibleDC(NULL); |
| 91 |
SelectObject(dc, g_hbm); |
| 92 |
|
| 93 |
x = GetDIBits(dc, g_hbm, 0, bm.bmHeight, bmp, &bmi, DIB_RGB_COLORS); |
| 94 |
|
| 95 |
region = CreateRectRgn(0, 0, bm.bmWidth, bm.bmHeight); |
| 96 |
|
| 97 |
// Search for transparent pixels |
| 98 |
for (y = bm.bmHeight - 1; y >= 0; y--) { |
| 99 |
for (x = 0; x < bm.bmWidth;) { |
| 100 |
if ((*bmp & 0xFFFFFF) == keycolor) { |
| 101 |
int j = x; |
| 102 |
while ((x < bm.bmWidth) && ((*bmp & 0xFFFFFF) == keycolor)) { |
| 103 |
bmp++, x++; |
| 104 |
} |
| 105 |
|
| 106 |
// Cut transparent pixels from the original region |
| 107 |
cutrgn = CreateRectRgn(j, y, x, y + 1); |
| 108 |
CombineRgn(region, region, cutrgn, RGN_XOR); |
| 109 |
DeleteObject(cutrgn); |
| 110 |
} else { |
| 111 |
bmp++, x++; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Set resulting region. |
| 117 |
SetWindowRgn(myWnd, region, TRUE); |
| 118 |
DeleteObject(region); |
| 119 |
DeleteObject(dc); |
| 120 |
GlobalFree(bmp_orig); |
| 121 |
} |
| 122 |
|
| 123 |
BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) |
| 124 |
{ |
| 125 |
g_hInstance = hInst; |
| 126 |
return TRUE; |
| 127 |
} |
| 128 |
|
| 129 |
void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 130 |
{ |
| 131 |
int call = -1; |
| 132 |
switch (state) { |
| 133 |
// FadeIN |
| 134 |
case 0: |
| 135 |
if (timeleft == 0) { |
| 136 |
timeleft = sleep_val; |
| 137 |
state++; |
| 138 |
if (IsLayeredWnd()) |
| 139 |
call = 255; |
| 140 |
} else { |
| 141 |
call = ((fadein_val - timeleft) * 255) / fadein_val; |
| 142 |
break; |
| 143 |
} |
| 144 |
// Sleep |
| 145 |
case 1: |
| 146 |
if (timeleft == 0) { |
| 147 |
timeleft = fadeout_val; |
| 148 |
state++; |
| 149 |
// fadeout |
| 150 |
} else |
| 151 |
break; |
| 152 |
// FadeOUT |
| 153 |
case 2: |
| 154 |
if (timeleft == 0) { |
| 155 |
PostMessage((HWND) dwUser, WM_TIMER, 0, 0); |
| 156 |
return; |
| 157 |
} else { |
| 158 |
call = ((timeleft) * 255) / fadeout_val; |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
// Transparency value acquired, and could be set... |
| 163 |
if ((call >= 0) && IsLayeredWnd()) |
| 164 |
{ |
| 165 |
SetLayeredWindowAttributesProc((HWND) dwUser, keycolor, (BYTE) call, alphaparam); |
| 166 |
} |
| 167 |
|
| 168 |
// Time is running out... |
| 169 |
timeleft--; |
| 170 |
} |
| 171 |
|
| 172 |
void __declspec(dllexport) show(HWND hwndParent, int string_size, |
| 173 |
TCHAR *variables, stack_t ** stacktop) |
| 174 |
{ |
| 175 |
DEVMODE dm; |
| 176 |
TCHAR fn[MAX_PATH], uselayerwnd; |
| 177 |
TCHAR temp[64]; |
| 178 |
FARPROC slwa; |
| 179 |
|
| 180 |
g_rv = -1; |
| 181 |
resolution = RESOLUTION; |
| 182 |
|
| 183 |
EXDLL_INIT(); |
| 184 |
|
| 185 |
popstring(temp); |
| 186 |
sleep_val = myatoi(temp); |
| 187 |
popstring(temp); |
| 188 |
fadein_val = myatoi(temp); |
| 189 |
popstring(temp); |
| 190 |
fadeout_val = myatoi(temp); |
| 191 |
popstring(temp); |
| 192 |
keycolor = myatoi(temp); |
| 193 |
popstring(fn); |
| 194 |
|
| 195 |
dm.dmSize = sizeof(DEVMODE); |
| 196 |
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm); |
| 197 |
#ifdef _WIN64 |
| 198 |
slwa = (FARPROC) SetLayeredWindowAttributes; |
| 199 |
#else |
| 200 |
slwa = GetProcAddress(LoadLibraryA("USER32"), "SetLayeredWindowAttributes"); |
| 201 |
#endif |
| 202 |
|
| 203 |
// Check for winXP/2k at 32 bpp transparency |
| 204 |
uselayerwnd = slwa && dm.dmBitsPerPel >= 32 && keycolor == -1; |
| 205 |
|
| 206 |
if (!uselayerwnd) { |
| 207 |
// Fading+transparency is unsupported at old windows versions... |
| 208 |
resolution = sleep_val + fadein_val + fadeout_val; |
| 209 |
fadeout_val = fadein_val = 0; |
| 210 |
sleep_val = 1; |
| 211 |
state = 1; // skip fade in |
| 212 |
} else { |
| 213 |
// div them by resolution |
| 214 |
sleep_val >>= 5; |
| 215 |
fadein_val >>= 5; |
| 216 |
fadeout_val >>= 5; |
| 217 |
state = 0; |
| 218 |
|
| 219 |
alphaparam = LWA_ALPHA | ((keycolor == -1) ? (0) : (LWA_COLORKEY)); |
| 220 |
keycolor = |
| 221 |
((keycolor & 0xFF) << 16) + (keycolor & 0xFF00) + |
| 222 |
((keycolor & 0xFF0000) >> 16); |
| 223 |
} |
| 224 |
|
| 225 |
if (fn[0] && ((sleep_val + fadein_val + fadeout_val) > 0)) { |
| 226 |
MSG msg; |
| 227 |
static WNDCLASS wc; |
| 228 |
wc.lpfnWndProc = WndProc; |
| 229 |
wc.hInstance = g_hInstance; |
| 230 |
wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 231 |
wc.lpszClassName = classname; |
| 232 |
if (RegisterClass(&wc)) { |
| 233 |
TCHAR fn2[MAX_PATH]; |
| 234 |
|
| 235 |
fn2[0] = _T('\0'), lstrcat(fn2, fn); |
| 236 |
lstrcat(fn2, _T(".wav")); |
| 237 |
lstrcat(fn, _T(".bmp")); |
| 238 |
|
| 239 |
g_hbm = LoadImage(NULL, fn, IMAGE_BITMAP, 0, 0, |
| 240 |
LR_CREATEDIBSECTION | LR_LOADFROMFILE); |
| 241 |
if (g_hbm) { |
| 242 |
HWND myWnd; |
| 243 |
UINT timerEvent; |
| 244 |
|
| 245 |
// Get Bitmap Information |
| 246 |
GetObject(g_hbm, sizeof(bm), & bm); |
| 247 |
|
| 248 |
myWnd = CreateWindowEx(WS_EX_TOOLWINDOW | |
| 249 |
(uselayerwnd ? (WS_EX_LAYERED) : (0)), classname, |
| 250 |
classname, 0, 0, 0, 0, 0, (HWND) hwndParent, |
| 251 |
NULL, g_hInstance, NULL); |
| 252 |
|
| 253 |
// Set transparency / key color |
| 254 |
if (uselayerwnd) { |
| 255 |
SetLayeredWindowAttributesProc = (SetLayeredWindowAttributes_T) slwa; |
| 256 |
|
| 257 |
// Use win2k method |
| 258 |
SetLayeredWindowAttributesProc(myWnd, keycolor, |
| 259 |
(BYTE) ((fadein_val > 0) ? (0) : (255)), |
| 260 |
alphaparam); |
| 261 |
} else if (keycolor != -1) { |
| 262 |
// transparency mode |
| 263 |
SetTransparentRegion(myWnd); |
| 264 |
} |
| 265 |
|
| 266 |
PlaySound(fn2, NULL, SND_ASYNC | SND_FILENAME | SND_NODEFAULT); |
| 267 |
|
| 268 |
// Start up timer... |
| 269 |
timeleft = fadein_val; |
| 270 |
timerEvent = |
| 271 |
timeSetEvent(resolution, RESOLUTION / 4, TimeProc, |
| 272 |
(DWORD_PTR) myWnd, TIME_PERIODIC); |
| 273 |
|
| 274 |
while (IsWindow(myWnd) && GetMessage(&msg, myWnd, 0, 0)) { |
| 275 |
DispatchMessage(&msg); |
| 276 |
} |
| 277 |
|
| 278 |
// Kill the timer... |
| 279 |
timeKillEvent(timerEvent); |
| 280 |
|
| 281 |
// Stop currently playing wave, we want to exit |
| 282 |
PlaySound(0, 0, 0); |
| 283 |
|
| 284 |
DeleteObject(g_hbm); |
| 285 |
} |
| 286 |
// We should UnRegister class, since Windows NT series never does this by itself |
| 287 |
UnregisterClass(wc.lpszClassName, wc.hInstance); |
| 288 |
} |
| 289 |
} |
| 290 |
wsprintf(temp, _T("%d"), g_rv); |
| 291 |
pushstring(temp); |
| 292 |
} |