PHP Code:
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.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;
}
bool isRunning()
{
//This checks if the process is already running so you don't have multiple instances running.
HANDLE handle = CreateMutex(NULL, true, "myColor");
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;
}
COLORREF getColor(void)
{
POINT p;
COLORREF color;
HDC hDC = GetDC(NULL);
BOOL b;
// Get the device context for the screen
if (hDC == NULL)
{
MessageBoxA(0, "Device context can't be found!", "Error", MB_OK | MB_ICONERROR);
return 0;
}
// Get the current cursor position
b = GetCursorPos(&p);
if (!b)
{
MessageBoxA(0, "Can't get the cursor position!", "Error", MB_OK | MB_ICONERROR);
return 0;
}
// Retrieve the color at that position
color = GetPixel(hDC, p.x, p.y);
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
return color;
}
void DrawStuff(HWND hWnd, HDC hDC, RECT& rc)
{
//Get the rectangle of our screen & calculate the width & height
RECT rec = initRecArea(rc.left, rc.top, screenWidth, screenHeight);
GetClientRect(hWnd, &rec);
screenWidth = (rec.right - rec.left);
screenHeight = (rec.bottom - rec.top);
HDC memDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, screenWidth, screenHeight);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
POINT p;
BOOL b1 = GetCursorPos(&p);
COLORREF myColor;
char szBuffer[512];
// Get the device context for the screen
if (hDC == NULL)
{
MessageBoxA(0, "Device context can't be found!", "Error", MB_OK | MB_ICONERROR);
}
myColor = getColor();
//Color the background to myColor
HBRUSH hBrush = CreateSolidBrush(myColor);
FillRect(memDC, &rec, (HBRUSH)hBrush);
DeleteObject((HBRUSH)hBrush);
// Draw Buffer into screen
BitBlt(hDC, 0, 0, screenWidth, screenHeight, memDC, 0, 0, SRCCOPY);
//Draw the text onto screen
sprintf_s( szBuffer, sizeof( szBuffer ), "My Color is: \nHex: #%X \nDecimal(RGB): %i %i %i", myColor, GetRValue(myColor), GetGValue(myColor), GetBValue(myColor));
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB((255 - GetRValue(myColor)), (255 - GetGValue(myColor)), (255 - GetBValue(myColor))));
SetRect(&rec, rec.left, rec.top/2, rec.right, rec.bottom/2);
DrawText(hDC, szBuffer, (strlen(szBuffer)), &rec, DT_CENTER | DT_VCENTER | DT_NOCLIP );
//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 )
{
//Check if this process is running
if(isRunning() == false)
return 0;
char szWindowClass[] = "myColor";
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 );
//Main Application Window
hWnd = CreateWindowEx(0, szWindowClass, "", WS_POPUPWINDOW | WS_CAPTION | WS_MAXIMIZEBOX, 0, 0, 400, 200, NULL, NULL, hInstance, NULL);
// Do stuff here
//End
if(!hWnd)
{
return FALSE;
}
//Activate our Main Window
CenterWindow(hWnd, NULL, 400, 200);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int) msg.wParam;
}