制作取色器的方法
作者:文墨网    转贴自:本站原创    点击数:2078

在VC6.0中新建 工程 选择Win32 application,一直下一步确定。
然后再新建C++ Source file
内容:
#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPSTR lpCmdLine,
       int nShowCmd)
{
 static TCHAR szAppName[]=RGB;

 WNDCLASS cwnd;
 cwnd.cbClsExtra=0;
 cwnd.cbWndExtra=0;
 cwnd.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 cwnd.hCursor=LoadCursor(NULL,IDC_ARROW);
 cwnd.hIcon=LoadIcon(hInstance,szAppName);
 cwnd.hInstance=hInstance;
 cwnd.lpfnWndProc=WndProc;
 cwnd.lpszClassName=szAppName;
 cwnd.lpszMenuName=NULL;
 cwnd.style=CS_HREDRAW | CS_VREDRAW;

 if(! RegisterClass(&cwnd))
 {
  MessageBox(NULL,Fail to Register the Window class!,szAppName,MB_IC&#111nERROR);
  return 0;
 }

 HWND hwnd;

 hwnd=CreateWindow(szAppName,szAppName,
  WS_OVERLAPPED | WS_MINIMIZEBOX | WS_EX_TOPMOST | WS_SYSMENU,
  CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  NULL,NULL,hInstance,NULL);

 ShowWindow(hwnd,nShowCmd);
 UpdateWindow(hwnd);

 MSG msg;

 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 static RECT rect,color_rect,point_rect,rgb_rect,d_rect;
 const int height=100,width=300;
 
 static POINT mspt;
 static HWND handle;
 static COLORREF color;
 static bool flag;
 static HBRUSH hBrush;

 char szbuf[100];

 switch(msg)
 {
 case WM_CREATE:
  SetTimer(hwnd,0,100,NULL);
  MoveWindow(hwnd,(GetSystemMetrics(SM_CXSCREEN)-width)/2,(GetSystemMetrics(SM_CYSCREEN)-height)/2,width,height,true);
  return 0;
 case WM_SIZE:
  GetClientRect(hwnd,&rect);
  color_rect.left=rect.left+2,color_rect.top=rect.top=2,color_rect.bottom=rect.bottom-2,color_rect.right=color_rect.left+100;
  point_rect.left=110,point_rect.top=rect.top,point_rect.right=rect.right,point_rect.bottom=rect.bottom/2;
  rgb_rect.left=110,rgb_rect.top=point_rect.bottom,rgb_rect.right=rect.right,rgb_rect.bottom=rect.bottom;
  return 0;
 case WM_TIMER:
  hdc=GetDC(hwnd);
  GetCursorPos(&mspt);
  handle=WindowFromPoint(mspt);
  GetWindowRect(handle,&d_rect);
  color=GetPixel(GetWindowDC(handle),mspt.x-d_rect.left,mspt.y-d_rect.top);
  DrawText(hdc,szbuf,wsprintf(szbuf,X:%04d Y:%04d  ,mspt.x,mspt.y),&point_rect,DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
  DrawText(hdc,szbuf,wsprintf(szbuf,R:%03d G:%03d B:%03d  ,GetR&#118alue(color),GetG&#118alue(color),GetB&#118alue(color)),&rgb_rect,DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
  hBrush=CreateSolidBrush(color);
  SelectObject(hdc,hBrush);
  Rectangle(hdc,color_rect.left,color_rect.top,color_rect.right,color_rect.bottom);
  DeleteObject(hBrush);
  ReleaseDC(hwnd,hdc);
  return 0;
 case WM_DESTROY:
  KillTimer(hwnd,0);
  PostQuitMessage(0);
  return 0;
 }
 return DefWindowProc(hwnd,msg,wParam,lParam);
}