#include
<
windows.h
>
#define
ID_TIMER 1
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int
WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,
int
iCmdShow)
{
static TCHAR szAppName[]=TEXT("GetColor ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadIcon(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpfnWndProc=WndProc;
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("The Program require windows NT"),szAppName,MB_ICONERROR);
return 0;
}
MessageBox(NULL,TEXT("程序由BearOcean编写,用于捕捉鼠标位置的RGB值和色彩"),TEXT("Infomation"),MB_OK);
hwnd=CreateWindow(szAppName,TEXT("GetColor--Writen By BearSea"),
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_BORDER,
CW_USEDEFAULT,CW_USEDEFAULT,
300,100,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static HDC hdcScreen;
PAINTSTRUCT ps;
POINT pt;
RECT rc;
static COLORREF cr=RGB(0,0,0),LastCr=RGB(1,0,0);//用于存放颜色信息
HDC hdc;
TCHAR szBuffer1[17];
TCHAR szBuffer2[17];
TCHAR szBuffer[10];
RECT ColorRect={150,0,300,100};
HBRUSH hBrush;
int iLength1;
int iLength2;
HGLOBAL hGlobal;
PTSTR pGlobal ;
switch(message)
{
case WM_CREATE:
GetClientRect(hwnd,&rc);
hdcScreen=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);//获取屏幕句柄
SetTimer(hwnd,ID_TIMER,100,NULL);
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_TIMER:
GetCursorPos(&pt);//Get Cursor pos
cr=GetPixel(hdcScreen,pt.x,pt.y);
if(cr!=LastCr)
{
InvalidateRect(hwnd,NULL,TRUE);
LastCr=cr;
}
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rc);
hBrush=CreateSolidBrush(cr);
FillRect(hdc,&ColorRect,hBrush);
iLength1=wsprintf(szBuffer1,TEXT("16:#%02x%02x%02x"),GetRValue(cr),GetGValue(cr),GetBValue(cr));
iLength2=wsprintf(szBuffer2,TEXT("rgb: %4d%4d%4d"),GetRValue(cr),GetGValue(cr),GetBValue(cr));
TextOut(hdc,5,10,szBuffer1,iLength1);
TextOut(hdc,5,30,szBuffer2,iLength2);
EndPaint(hwnd,&ps);
DeleteObject(hBrush);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_SPACE:
GetCursorPos(&pt);//Get Cursor pos
cr=GetPixel(hdcScreen,pt.x,pt.y);
wsprintf(szBuffer,TEXT("#%02x%02x%02x"),GetRValue(cr),GetGValue(cr),GetBValue(cr));
//------------------------------------
hGlobal = GlobalAlloc (GHND | GMEM_SHARE, (lstrlen (szBuffer) + 1) * sizeof (TCHAR)) ;
pGlobal = GlobalLock (hGlobal) ;
lstrcpy (pGlobal, szBuffer) ;
GlobalUnlock (hGlobal) ;
OpenClipboard (hwnd) ;
EmptyClipboard () ;
SetClipboardData (CF_TEXT, hGlobal) ;
CloseClipboard () ;
MessageBox (NULL, TEXT ("颜色信息已经写入剪贴板 :)"), TEXT ("Action"), 0);
//------------------------------------
//MessageBox (NULL, TEXT ("颜色信息已存入剪贴板"), TEXT ("HelloMsg"), 0);
break;
case VK_F1:
MessageBox(NULL, TEXT ("使用方法:\n鼠标指向要选取的颜色\n按下空格键,颜色值将被写入剪贴板.\n-------------------------------------------------\n写这个是为了方便自己写网页的时候\n获取颜色值。\n-------------------------------------------------\n By BearOcean"), TEXT ("Help"), 0);
break;
}
return 0;
case WM_DESTROY:
DeleteDC(hdcScreen);
KillTimer(hwnd,ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}