ColorSpy.cpp

 #include "stdafx.h"
#include "windows.h"
#include "resource.h"
#include "stdlib.h"

HANDLE hAccel;
int SubZero=0;
HBRUSH TheBrush=0;
BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), 0,DlgProc);
return 0;
}

BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
{
if(LOWORD(wParam) == ID_INFO_ABOUTTHISPROGRAM)
{
MessageBox(hwnd, "ColorSpy v2.0\n\nColorSpy will capture the color beneath the mouse cursor and report its RGB, Decimal and Hexadecimal values.\n\nThis program was written in 100% pure C++\n\nCopyright XeoTechnology 2001", "About", MB_OK|MB_ICONINFORMATION);
}
/* if(LOWORD(wParam) == ACCEL_HEX)
{
char Hexbuffer[100]="";
GetDlgItemText(hwnd, IDC_HEX, Hexbuffer, 100);
HANDLE hMem=Hexbuffer;
SetClipboardData(CF_DSPTEXT , hMem);
CloseHandle(hMem);
}
if(LOWORD(wParam) == ACCEL_RGB)
{
char RGBbuffer[100]="";
GetDlgItemText(hwnd, IDC_RGB, RGBbuffer, 100);
HANDLE hMem=RGBbuffer;
SetClipboardData(CF_DSPTEXT , hMem);
CloseHandle(hMem);
}
if(LOWORD(wParam) == ACCEL_STOP)
{
DebugBreak();
if(SubZero==0)
{
KillTimer(hwnd, 1);
SubZero++;
}
else
{
SetTimer(hwnd, 0, 10, NULL);
SubZero--;
}
}*/
break;
}
case WM_CTLCOLORSTATIC:
{
if (GetDlgCtrlID((HWND)lParam)==IDC_COLORVIEW)
{
if (TheBrush!=0){DeleteObject(TheBrush);}
POINT CursorPos;
GetCursorPos(&CursorPos);
int mouseX=CursorPos.x;
int mouseY=CursorPos.y;
char charX[10]="";
char charY[10]="";
itoa(mouseX, charX, 10);
itoa(mouseY, charY, 10);
SetDlgItemText(hwnd, IDC_X, charX);
SetDlgItemText(hwnd, IDC_Y, charY);
HDC rDC;
COLORREF rPixel;
rDC=GetDC(0);
rPixel=GetPixel(rDC, mouseX, mouseY);
char DecChar[20]="";
itoa(rPixel, DecChar, 10);
SetDlgItemText(hwnd, IDC_DEC, DecChar);
char HexChar[20]="";
itoa(rPixel, HexChar, 16);
SetDlgItemText(hwnd, IDC_HEX, HexChar);
char RGBChar[20]="";
char iR[3]="";
char iG[3]="";
char iB[3]="";
iR[strlen(iR)]=HexChar[0];
iR[strlen(iR)]=HexChar[1];
iR[strlen(iR)]=0;
iG[strlen(iG)]=HexChar[2];
iG[strlen(iG)]=HexChar[3];
iG[strlen(iG)]=0;
iB[strlen(iB)]=HexChar[4];
iB[strlen(iB)]=HexChar[5];
iB[strlen(iB)]=0;
if(HexChar=="0")
{
SetDlgItemText(hwnd, IDC_RGB, "000000");
}
else
{
strcpy(RGBChar, iB);
strcat(RGBChar, iG);
strcat(RGBChar, iR);
SetDlgItemText(hwnd, IDC_RGB, RGBChar);
}
int BVal = rPixel / 65536;
char BChar[20]="";
itoa(BVal, BChar, 10);
SetDlgItemText(hwnd, IDC_B, BChar);

int GVal = (rPixel - (65536 * BVal)) / 256;
char GChar[20]="";
itoa(GVal,GChar, 10);
SetDlgItemText(hwnd, IDC_G, GChar);

int RVal = rPixel - (65536 * BVal + 256 * GVal);
char RChar[20]="";
itoa(RVal,RChar, 10);
SetDlgItemText(hwnd, IDC_R, RChar);

ReleaseDC(0, rDC);
HDC ImageDC;
ImageDC=(HDC)wParam;
TheBrush=CreateSolidBrush(rPixel);
return (int)TheBrush;
}
return FALSE;
break;
}
case WM_TIMER:
{
RECT rect;
GetClientRect(GetDlgItem(hwnd, IDC_COLORVIEW), &rect);
InvalidateRect(GetDlgItem(hwnd, IDC_COLORVIEW), &rect, TRUE);
break;
}
case WM_INITDIALOG:
{
double ScreenX=0;
double ScreenY=0;
double WinX=0;
double WinY=0;
RECT xeo;
GetWindowRect(hwnd, &xeo);
WinX = xeo.right - xeo.left;
WinY = xeo.bottom - xeo.top;
ScreenX = GetSystemMetrics(SM_CXSCREEN);
ScreenY = GetSystemMetrics(SM_CYSCREEN);
ScreenX = ((ScreenX * .95) / 2) - ((int)WinX / 2);
ScreenY = (ScreenY / 2) - ((int)WinY / 2);
SetWindowPos(hwnd,HWND_TOPMOST, (int)ScreenX ,(int)ScreenY,(int)WinX,(int)WinY,NULL);
SetTimer(hwnd, 1, 100, NULL);
SetFocus(GetDlgItem(hwnd, IDC_EDIT9));
return FALSE;
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
break;
}
}
return FALSE;
}

Project Homepage: