Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818433
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:13:53


作者为:

By PAUL Grégory


原本44K,被俺精简成了9K,

#define _WIN32_WINNT 0x0500 // Used for DC brushes and pens
#define _WIN32_IE 0x0600 // Used for systray balloon tips

#include "resource.h"
#include
#include

// Constants
#define GTC_MAXSTR  128
#define GTC_TIMEOUT  100
#define GTC_TIMER_ID 6363
#define GTC_SYSTRAY  403

#define GTC_APP_WIDTH 140
#define GTC_APP_HEIGHT 56
#define GTC_SQUARE_LEN 16

// Constant arrays
char   hex[] = "0123456789ABCDEF";

// Global vars
HINSTANCE  hInst;       // Instance handle
TCHAR   szTitle[GTC_MAXSTR];   // Title bar caption
TCHAR   szWindowClass[GTC_MAXSTR];  // Main window class
COLORREF  cursorColor, lastColor;   // RGB color under cursor
COLORREF  copyColor;      // RGB color in clipboard
HWND   hWnd;       // Window handle
UINT_PTR  hTimer;       // Timer handle
NOTIFYICONDATA tnid;       // Systray stucture
HGLOBAL   hClip;       // Global allocation for clipboard
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void inline RegisterSystrayMode()
{
 tnid.cbSize    = NOTIFYICONDATA_V2_SIZE;
 tnid.hWnd    = hWnd;
 tnid.uID    = 1;
 tnid.uFlags    = NIF_MESSAGE | NIF_ICON | NIF_INFO | NIF_TIP;
 tnid.uCallbackMessage = GTC_SYSTRAY;
 tnid.hIcon    = LoadIcon(hInst, MAKEINTRESOURCE(IDI_GETTHECOLOR));
 tnid.uTimeout   = 5000;
 tnid.dwInfoFlags  = NIIF_INFO;

 lstrcpyn(tnid.szInfoTitle, "GetTheColor已启动\000", 64);
 lstrcpyn(tnid.szInfo, "系统快捷键:\n- Windows + S : 显示/隐藏窗口\n- Windows + C : 将颜色拷贝到剪贴板\000", 256);
 lstrcpyn(tnid.szTip, "GetTheColor\000", 128);

 Shell_NotifyIcon(NIM_ADD, &tnid);
}
// Function declaration
ATOM inline MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;
 wcex.cbSize   = sizeof(WNDCLASSEX);
 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_GETTHECOLOR);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = NULL;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = NULL;
 return RegisterClassEx(&wcex);
}
BOOL inline InitInstance(HINSTANCE hInstance)
{
 hInst = hInstance;

 // Creates window
 hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
  szWindowClass, szTitle,
  WS_OVERLAPPED | WS_SYSMENU,
  CW_USEDEFAULT, 0,
  GTC_APP_WIDTH, GTC_APP_HEIGHT,
  NULL, NULL,
  hInstance, NULL);

 if (!hWnd)
 {
  return FALSE;
 }

 // Creates timer
 hTimer = SetTimer(hWnd, GTC_TIMER_ID, GTC_TIMEOUT, NULL);

 if (!hTimer)
 {
  return FALSE;
 }

 // Register systray and hotkeys
 RegisterSystrayMode();
 RegisterHotKey(hWnd, 1, MOD_WIN, (int)'C');
 RegisterHotKey(hWnd, 2, MOD_WIN, (int)'S');

 UpdateWindow(hWnd);

 return TRUE;
}

void inline FormatHTMLColor(char *str, COLORREF color, BOOL bSharp)
{
 WORD r, g, b;
 int i = 0;

 r = GetRValue(color);
 g = GetGValue(color);
 b = GetBValue(color);


 if (bSharp)
 {
  str[i++] = '#';
 }

 str[i++] = hex[r >> 4];
 str[i++] = hex[r & 0x0F];
 str[i++] = hex[g >> 4];
 str[i++] = hex[g & 0x0F];
 str[i++] = hex[b >> 4];
 str[i++] = hex[b & 0x0F];
 str[i++] = 0;
}

void inline FormatRGBColor(char *str, COLORREF color)
{
 WORD r, g, b;
 r = GetRValue(color);
 g = GetGValue(color);
 b = GetBValue(color);
 wsprintf(str,"R:%3.3d G:%3.3d B:%3.3d",r,g,b);
}

void inline ChangeWindowVisibility()
{
 if (IsIconic(hWnd) > 0 || IsWindowVisible(hWnd) == 0)
 {
  ShowWindow(hWnd, SW_SHOWNORMAL);
  SetForegroundWindow(hWnd);
 }
 else
 {
  ShowWindow(hWnd, SW_HIDE);
 }
}

/**
 * Application entry point
 *
 * @param hInstance The instance handle given by Windows
 * @param hPrevInstance The previous instance handle
 * @param lpCmdLine The command args
 * @param nCmdShow The show flags
 */
//int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
extern "C" void WinMainCRTStartup()
{
 MSG msg;
 HINSTANCE hInstance=GetModuleHandle(NULL);
 lstrcpy(szWindowClass, "GTCClass");
 lstrcpy(szTitle, "屏幕取色");

 MyRegisterClass(hInstance);

 // Inits application and window
 if (!InitInstance(hInstance))
 {
  return;
 }

 // Boucle de messages principale?
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return;
}

/**
 * Adds an icon in Windows taskbar notification, and enables the ballon tooltip
 */

 

/**
 * Show/hides window according to its current status
 */


/**
 * Window callback function
 *
 * @param hWnd The window handle
 * @param message The message identifier
 * @param wParam Param in WORD format
 * @param lParam Param in long format
 */
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC   hdc, hdcScreen;
 POINT  pt;
 char  sColor1[8], sColor2[32];
 LPTSTR  lptstrCopy;

 switch (message)
 {
  /* Timer handling */
  case WM_TIMER:
   if (GetCursorPos(&pt))
   {
    hdcScreen = GetDC(NULL);
    cursorColor = GetPixel(hdcScreen, pt.x, pt.y);
    ReleaseDC(NULL, hdcScreen);
   }
   if (lastColor != cursorColor)
   {
    lastColor = cursorColor;
    RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
   }
   break;

  /* Application surface refresh */
  case WM_PAINT:
   // Formats colors to HTML text
   FormatHTMLColor(sColor1, cursorColor, TRUE);
   FormatRGBColor(sColor2, cursorColor);
   hdc = BeginPaint(hWnd, &ps);
   // Clean the app surface
   SelectObject(ps.hdc, GetStockObject(DC_BRUSH));
   SetDCBrushColor(ps.hdc, RGB(0, 0, 0));
   Rectangle(ps.hdc, 0, 0, GTC_APP_WIDTH, GTC_APP_HEIGHT);
   SetBkColor(ps.hdc, RGB(0, 0, 0));
   SetTextColor(ps.hdc, RGB(255, 255, 255));
   TextOut(ps.hdc, 2, 0, sColor1, 7);
   TextOut(ps.hdc, 2, GTC_SQUARE_LEN, sColor2, lstrlen(sColor2));
   EndPaint(hWnd, &ps);
   break;

  /* Windows hotkeys */
  case WM_HOTKEY:
   if (LOWORD(lParam) == MOD_WIN)
   {
    switch (HIWORD(lParam))
    {
     case (int)'S':
      ChangeWindowVisibility();
      break;

     case (int)'C':
      // Allocates shared memory for clipboard
      hClip = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 7 * sizeof(char));

      if (!hClip)
      {
       return FALSE;
      }

      // Locking the memory for write into
      lptstrCopy = (char *)GlobalLock(hClip);
      
      if (!lptstrCopy)
      {
       return FALSE;
      }

      FormatHTMLColor(lptstrCopy, cursorColor, FALSE);
      GlobalUnlock(hClip);

      if (OpenClipboard(hWnd))
      {
       EmptyClipboard();
       SetClipboardData(CF_TEXT, hClip);
       CloseClipboard();
      }

      copyColor = cursorColor;
      RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
      break;

     default:
      break;
    }
   }

   break;

  /* Systray icon click */
  case GTC_SYSTRAY:
   switch((UINT)lParam)
   {
    case WM_LBUTTONDOWN:
     ChangeWindowVisibility();
     break;
    case WM_RBUTTONDOWN:
     Shell_NotifyIcon(NIM_MODIFY, &tnid);
    default:
     break;
   }

   break;

  /* Program dispose */
  case WM_DESTROY:
   if (hClip)
   {
    GlobalFree(hClip);
   }

   UnregisterHotKey(hWnd, 1);
   UnregisterHotKey(hWnd, 2);
   Shell_NotifyIcon(NIM_DELETE, &tnid);

   PostQuitMessage(0);
   break;

  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
 }

 return 0;
}


 


 

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by getthecolor.rc
//
#define IDI_GETTHECOLOR                 101
#define IDS_INFO_TITLE                  103
#define IDS_INFO_TEXT                   104
#define IDS_TOOLTIP                     105

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        104
#define _APS_NEXT_COMMAND_VALUE         40002
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif
或者直接下载:http://blog.vckbase.com/files/bastet/getthecolor.zip


--------------------next---------------------

阅读(527) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~