/*
看书看的无聊,,随便写了个桌面颜色抓取器
功能:抓取鼠标所在点的颜色,返回16进制颜色,并显示颜色
*/
#include
#include
int main()
{
typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
PROCGETCONSOLEWINDOW GetConsoleWindow;
HMODULE hKernel32 = GetModuleHandle("kernel32");
GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
HDC hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
HDC hdcConsole=GetDC(GetConsoleWindow());
char s[1024];
POINT cursorpos;
for(;;Sleep(10)){
GetCursorPos(&cursorpos);
COLORREF c= GetPixel(hdc,cursorpos.x,cursorpos.y);
sprintf(s,"X:%d,Y:%d R:%X G:%X B:%X RGB:%X HTML:#%02X%02X%02X "
,cursorpos.x,cursorpos.y, GetRValue(c),GetGValue(c),GetBValue(c),
c, GetRValue(c),GetGValue(c),GetBValue(c));
SetTextColor(hdcConsole,c);
TextOut(hdcConsole,0,40,TEXT("Www.Ternsoft.Com"),strlen("Www.Ternsoft.Com"));
printf("\r%s",s);
//break;
}
getchar();
return 0;
}
//再来一个桌面版的
#include <STDIO.H>
#include <windows.h>
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("MyTestWindow");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex = {0};
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon;
if (!RegisterClassEx (&wndclassex))
{
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
szAppName,
TEXT ("Ternsoft.com 桌面取色器"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
bool bGetColor=false;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
char strMsg[256];
POINT ptMousePos;
UINT nPosColor;
switch (message)
{
case WM_CREATE:
return (0);
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
TextOut (hdc, 0, 0, ("请在本窗口空白区按住鼠标左键,移动鼠标到需要取色的位置后释放左键"), 64);
EndPaint (hwnd, &ps);
return (0);
case WM_MOUSEMOVE:
if(bGetColor){
hdc=GetDC(hwnd);
GetCursorPos(&ptMousePos);
SetTextColor(hdc,nPosColor=GetPixel(CreateDC("DISPLAY",0,0,0),ptMousePos.x,ptMousePos.y));
TextOut(hdc,30,30,strMsg,sprintf(strMsg,"x:%d y:%d RGB:%02X %02X %02X ",
ptMousePos.x,ptMousePos.y,GetRValue(nPosColor),GetGValue(nPosColor),GetBValue(nPosColor)));
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_LBUTTONDOWN:
bGetColor=true;
SetCursor(LoadCursor(NULL,IDC_CROSS));
SetCapture(hwnd);
return 0;
case WM_LBUTTONUP:
bGetColor=false;
ReleaseCapture();
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return (0);
}
return DefWindowProc (hwnd, message, wParam, lParam);
}