PHP Code:
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <time.h>
//Window Handles
HWND hWnd;
int screenWidth = 0;
int screenHeight = 0;
//Center Window
VOID CenterWindow(HWND hwnd, HWND hwndParent, int Width, int Height)
{
/* Variables */
RECT rc;
/* If Parent Window Is Set As Null, Get The Desktop Window */
if(hwndParent == NULL)
{
hwndParent = GetDesktopWindow();
}
/* Get Parent Client Area Measurements */
GetClientRect(hwndParent, &rc);
/* Center The Window */
MoveWindow(hwnd, (rc.right - rc.left - Width) / 2, (rc.bottom - rc.top - Height) / 2, Width, Height, TRUE);
return;
}
void GetDesktopResolution(int& width, int& height)
{
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
width = desktop.right;
height = desktop.bottom;
}
bool isRunning()
{
//This checks if the process is already running so you don't have multiple instances running.
HANDLE handle = CreateMutex(NULL, true, "desktopOverlay");
if(GetLastError() != ERROR_SUCCESS)
{
MessageBox(0, "Process is already running", "Warning", MB_ICONWARNING);
return false;
}
else
return true;
}
RECT initRecArea(int left, int top, int width, int height)
{
RECT rec = {left, top, width, height};
return rec;
}
int randColor(float* r, float* g, float* b)
{
COLORREF colRef1 = RGB( rand() % 255, rand() % 255, rand() % 255 );
HBRUSH color1 = CreateSolidBrush( colRef1 );
//This get the individual RGB colors from our brush above
//Then it takes the difference between the seeded color
//and the randomized color and divides by 255 (could be any number)
*r += (((GetRValue( colRef1 )) - *r) / 255);
*g += (((GetGValue( colRef1 )) - *g) / 255);
*b += (((GetBValue( colRef1 )) - *b) / 255);
DeleteObject((HBRUSH)color1);
return (int)CreateSolidBrush( RGB( *r, *g, *b ) );
}
void DrawStuff(HWND hWnd, HDC hDC, RECT& rc)
{
static float r, g, b = (float)(rand() % 255);
// HWND desktop = GetDesktopWindow();
// HDC hdc = GetWindowDC(hWnd);
// HDC memDC = CreateCompatibleDC(hdc);
HDC memDC = CreateCompatibleDC(GetWindowDC(hWnd));
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, screenWidth, screenHeight);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
srand((int)time(NULL)); //Needed to randomize the color correctly
RECT rec = initRecArea(rc.left, rc.top, screenWidth, screenHeight);
GetClientRect(hWnd, &rec);
screenWidth = (rec.right - rec.left);
screenHeight = (rec.bottom - rec.top);
// Get the device context for the screen
if (hDC == NULL)
{
MessageBoxA(0, "Device context can't be found!", "Error", MB_OK | MB_ICONERROR);
}
//Color the background to myColor
//HBRUSH hBrush = CreateSolidBrush(RGB(0,0,255));
int hBrush = (int)randColor(&r,&g,&b);
FillRect(memDC, &rec, (HBRUSH)hBrush);
DeleteObject((HBRUSH)hBrush);
// Set WS_EX_LAYERED on this window
SetWindowLong(hWnd, GWL_EXSTYLE,
GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
// Make this window 70% alpha
SetLayeredWindowAttributes(hWnd, 0, (255 * 50) / 100, LWA_ALPHA); //50% transparency
// Ask the window and its children to repaint
RedrawWindow(hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
// Draw Buffer into screen
BitBlt(hDC, 0, 0, screenWidth, screenHeight, memDC, 0, 0, SRCCOPY);
// Finally set the animation timer
SetTimer(hWnd, NULL, 25, NULL);
//Free Resources
SelectObject(memDC, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(memDC);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc = {0};
switch (message)
{
case WM_ERASEBKGND:
{
return TRUE;
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
DrawStuff(hWnd, hdc, rc);
ReleaseDC(hWnd, hdc);
EndPaint(hWnd, &ps);
}
break;
case WM_CREATE:
{
SetTimer( hWnd, NULL, USER_TIMER_MINIMUM, NULL );
}
break;
case WM_TIMER:
{
GetClientRect( hWnd, &rc );
InvalidateRect( hWnd, &rc, TRUE );
}
break;
case WM_KEYDOWN:
{
if(wParam == VK_ESCAPE)
{
int ret = MessageBox(NULL, TEXT("Are you sure to quit?"), TEXT("Message"), MB_OKCANCEL);
if(ret == IDOK)
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
int width = 0;
int height = 0;
//Check if this process is running
if(isRunning() == false)
return 0;
char szWindowClass[] = "desktopOverlay";
MSG msg;
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszClassName = szWindowClass;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx( &wcex );
GetDesktopResolution(width, height);
//Main Application Window
//hWnd = CreateWindowEx(0, szWindowClass, "", WS_POPUPWINDOW | WS_CAPTION | WS_MAXIMIZEBOX, 0, 0, 400, 200, NULL, NULL, hInstance, NULL);
hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, szWindowClass, "", WS_POPUP | WS_VISIBLE, 0, 0, width, height, NULL, NULL, hInstance, NULL);
// Do stuff here
//End
if(!hWnd)
{
return FALSE;
}
//Activate our Main Window
CenterWindow(hWnd, NULL, width, height);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int) msg.wParam;
}