zoukankan      html  css  js  c++  java
  • 取色工具源码

        自己写的一款取色工具,源码如下:

      1 // -----------------------------------------------------
      2 // 函数名  : CCatchColorDlg::OnBnClickedButtonCatchcolor
      3 // 函数功能: 取色事件
      4 // 输入参数: 
      5 // 输出参数: 
      6 // 返回值  : 
      7 // -----------------------------------------------------
      8 void CCatchColorDlg::OnBnClickedButtonCatchcolor()
      9 {
     10     // 鼠标状态为左键按下
     11     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
     12 
     13     // 设置鼠标的形状
     14     m_hPoint =  AfxGetApp()->LoadCursor(IDC_CURSOR1);
     15     ::SetCursor(m_hPoint);
     16     InitAll(FALSE);
     17     
     18 }
     19 
     20 void CCatchColorDlg::OnEnChangeEditRgb()
     21 {
     22     UpdateData();
     23 
     24     // 如果是系统取色
     25     if (CatchColorMode_System == m_iCurrentMode) 
     26     {
     27         if (m_strRgb.IsEmpty()) 
     28         {
     29             m_strHex.Empty();
     30         } 
     31         else 
     32         {
     33             int nIndex = m_strRgb.Find(",");
     34             int nIndexTmp = 0;
     35             CString strRed;
     36             CString strGreen;
     37             CString strBlue;
     38             strRed.Empty();
     39             strGreen.Empty();
     40             strRed.Empty();
     41             while (-1 != nIndex && 0 != nIndex) 
     42             {
     43                 if(strRed.IsEmpty()) 
     44                 {
     45                     strRed = m_strRgb.Left(nIndex);
     46                 } 
     47                 else if (strGreen.IsEmpty()) 
     48                 {
     49                     strGreen = m_strRgb.Mid(nIndexTmp + 1, nIndex - nIndexTmp - 1);
     50                     strBlue = m_strRgb.Right(m_strRgb.GetLength() - nIndex - 1);
     51                 } 
     52                 else 
     53                 {
     54                     m_strHex.Empty();
     55                     break;
     56                 }
     57                 nIndexTmp = nIndex;
     58                 nIndex = m_strRgb.Find(_T(","), (nIndex + 1));
     59             }
     60 
     61             // RGB编辑框是否不为空
     62             BOOL bRgbNotEmpty = (!strRed.IsEmpty()) 
     63                                 && (!strBlue.IsEmpty())
     64                                 && (!strGreen.IsEmpty());
     65             if (bRgbNotEmpty) 
     66             {
     67                 // 是否是不合法的RGB值
     68                 BOOL bNotValidRGB = (!CheckRGB(atoi(strRed)))
     69                                     || (!CheckRGB(atoi(strGreen)))
     70                                     || (!CheckRGB(atoi(strBlue)));
     71                 if (bNotValidRGB) 
     72                 {
     73                         m_strHex.Empty();
     74                 } 
     75                 else 
     76                 {
     77                     m_strHex.Format(_T("#%02X%02X%02X"), atoi(strRed), atoi(strGreen), atoi(strBlue));
     78                     m_Color = RGB(atoi(strRed), atoi(strGreen), atoi(strBlue));
     79                 }
     80             }
     81             GetDlgItem(IDC_STATIC_COLOR)->RedrawWindow();
     82             UpdateData(FALSE);
     83         }
     84     }
     85     UpdateData(FALSE);
     86     GetDlgItem(IDC_STATIC_COLOR)->RedrawWindow();
     87 }
     88 
     89 void CCatchColorDlg::OnEnChangeEditHex()
     90 {
     91     UpdateData();
     92 
     93     if (CatchColorMode_System == m_iCurrentMode) 
     94     {
     95         if (m_strHex.IsEmpty() 
     96             || MaxHexWidth != m_strHex.GetLength()
     97             || HEX_FLAG != m_strHex.Left(strlen(HEX_FLAG))) 
     98         {
     99             return;
    100         }
    101         else 
    102         {
    103             int iHex = 0;
    104             int iLength = m_strHex.GetLength();
    105 
    106             char* pstrHex = NULL;
    107             pstrHex = (char*)malloc(sizeof(char)*(iLength + 1));
    108             memset(pstrHex, 0, (iLength + 1));
    109             memcpy(pstrHex, (LPCTSTR)m_strHex, iLength);
    110 
    111             char* pHex = NULL;
    112             pHex = &pstrHex[1];
    113             iHex = strtol(pHex, NULL, 16);
    114 
    115             int iRed = 0, iGreen = 0, iBule = 0;
    116             ChangeHexToRGB(iHex, iRed, iGreen, iBule);
    117             m_strRgb.Format(_T("%d, %d, %d"),iRed, iGreen, iBule);
    118 
    119             free(pstrHex);
    120             pstrHex = NULL;
    121 
    122             m_Color = RGB(iRed, iGreen, iBule);
    123             GetDlgItem(IDC_STATIC_COLOR)->RedrawWindow();
    124             UpdateData(FALSE);
    125         }
    126     }
    127 }
    128 
    129 BOOL CCatchColorDlg::CheckRGB(int color)
    130 {
    131     return ((MinRGBValue <= color) && (MaxRGBValue >= color));
    132 }
    133 
    134 
    135 void CCatchColorDlg::GetCurrentColor()
    136 {
    137     UpdateData();
    138 
    139     // 如果是屏幕取色
    140     if (CatchColorMode_Screen == m_iCurrentMode) 
    141     {
    142         HDC hDC = ::GetDC(NULL);
    143         CPoint pt; 
    144         GetCursorPos(&pt); 
    145         COLORREF clr = ::GetPixel(hDC,pt.x,pt.y);
    146 
    147         CString ClrText; 
    148         m_strRgb.Format( _T("(%d,%d,%d)"), GetRValue(clr), GetGValue(clr), GetBValue(clr));
    149         m_strHex.Format(_T("#%02X%02X%02X"), GetRValue(clr), GetGValue(clr), GetBValue(clr));
    150 
    151         ::ReleaseDC(NULL, hDC);
    152         m_Color = clr;
    153         GetDlgItem(IDC_STATIC_COLOR)->RedrawWindow();
    154 
    155         UpdateData(FALSE);
    156     }
    157     
    158 }
    159 
    160 void CCatchColorDlg::SetCurrentColor()
    161 {
    162     ReleaseCapture();
    163     m_iCurrentMode = CatchColorMode_System;
    164     SetClipboard(m_strHex);
    165     CString strMeg;
    166     strMeg.Empty();
    167     strMeg.Format(_T("%s已经被保存到剪贴板上了."), m_strHex);
    168     MessageBox(strMeg, _T("取色"), MB_OK|MB_ICONINFORMATION);
    169     InitAll(TRUE);
    170 }
    171 
    172 BOOL CCatchColorDlg::SetClipboard(CString strText)
    173 {
    174     if (!OpenClipboard())
    175     {
    176         return FALSE;
    177     }
    178 
    179     EmptyClipboard();
    180     int iLength = strText.GetLength();
    181     HGLOBAL hglbCopy;
    182     LPTSTR  lptstrCopy; 
    183     hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
    184             (iLength + 1) * sizeof(char)); 
    185 
    186     if (NULL == hglbCopy) 
    187     { 
    188         CloseClipboard(); 
    189         return FALSE; 
    190     }
    191 
    192     lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
    193     memcpy(lptstrCopy, (LPCTSTR)strText, iLength);
    194     GlobalUnlock(hglbCopy); 
    195     SetClipboardData(CF_TEXT, hglbCopy);
    196     CloseClipboard();
    197     return TRUE; 
    198 }
    199 
    200 void CCatchColorDlg::ChangeHexToRGB(int iHex, int& iRed, int& iGreen, int& iBlue)
    201 {
    202     iRed = ( 0xff <<16 & iHex ) >> 16;
    203     iGreen = ( 0xff <<8 & iHex ) >> 8;
    204     iBlue =0xff & iHex;
    205 
    206 }
    207 void CCatchColorDlg::OnBnClickedButtonCatchsyscolor()
    208 {
    209     m_iCurrentMode = CatchColorMode_System;
    210 
    211     CColorDialog colorDlg;
    212     if (IDOK == colorDlg.DoModal()) 
    213     {
    214         COLORREF clr = colorDlg.GetColor();
    215         CString ClrText; 
    216         m_strRgb.Format(_T("(%d,%d,%d)"), GetRValue(clr), GetGValue(clr) ,GetBValue(clr));
    217         m_strHex.Format(_T("#%02X%02X%02X"), GetRValue(clr), GetGValue(clr), GetBValue(clr));
    218         m_Color = clr;
    219 
    220         UpdateData(FALSE);
    221 
    222         CString strMeg;
    223         strMeg.Empty();
    224 
    225         if (SetClipboard(m_strHex))
    226         {
    227             strMeg.Format(_T("%s已被成功拷贝到剪贴板上了"),m_strHex);
    228         }
    229         else
    230         {
    231             strMeg.Format(_T("%s无法拷贝到剪贴板上"));
    232         }
    233         MessageBox(strMeg, _T("成功"), MB_OK | MB_ICONINFORMATION);
    234         GetDlgItem(IDC_STATIC_COLOR)->RedrawWindow();
    235     }
    236     else 
    237     {
    238         return;
    239     }
    240 }
    241 
    242 
    243 HBRUSH CCatchColorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    244 {
    245     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    246 
    247     // 如果是颜色静态文本框
    248     if(IDC_STATIC_COLOR == pWnd->GetDlgCtrlID())
    249     {
    250         pDC->SetBkMode(TRANSPARENT);
    251         m_brush.Detach();
    252         m_brush.CreateSolidBrush(m_Color);
    253         pDC->SelectObject(&m_brush);
    254         hbr = m_brush;
    255     } 
    256     else if (CTLCOLOR_STATIC == nCtlColor 
    257              && IDC_STATIC_COLOR != pWnd->GetDlgCtrlID()) 
    258     {
    259         pDC->SetBkMode(TRANSPARENT);
    260         pDC->SetTextColor(RGB(0, 0, 0));
    261         pDC->SetBkColor(DEFAULT_COLOR);
    262         hbr = m_TextBrush;
    263     }
    264     return hbr;
    265 }
    266 
    267 void CCatchColorDlg::OnLButtonDown(UINT nFlags, CPoint point)
    268 {
    269     
    270     UpdateData();
    271 
    272     // 如果是屏幕取色模式,鼠标左键按下,则取色
    273     if (CatchColorMode_Screen == m_iCurrentMode)
    274     {
    275         GetCurrentColor();
    276     }
    277     // 如果不是屏幕取色模式,鼠标左键按下,则移动对话框
    278     else
    279     {
    280         PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y)); 
    281     }
    282 
    283     CDialog::OnLButtonDown(nFlags, point);
    284     
    285 }
    286 
    287 void CCatchColorDlg::InitAll(BOOL bEnable)
    288 {
    289     GetDlgItem(IDC_BUTTON_CATCHSYSCOLOR)->EnableWindow(bEnable);
    290     GetDlgItem(IDC_BUTTON_GETVERSION)->EnableWindow(bEnable);
    291 
    292     if (!bEnable) 
    293     {
    294         m_iCurrentMode = CatchColorMode_Screen;
    295         SetCapture();
    296     }
    297 }
    298 
    299 void CCatchColorDlg::OnLButtonUp(UINT nFlags, CPoint point)
    300 {
    301     // 如果当前模式是屏幕取色模式,鼠标左键松开,则取色,并显示
    302     if (CatchColorMode_Screen == m_iCurrentMode) 
    303     {
    304         GetCurrentColor();
    305         SetCurrentColor();
    306     }
    307 
    308     CDialog::OnLButtonUp(nFlags, point);
    309 }
    310 
    311 void CCatchColorDlg::OnMouseMove(UINT nFlags, CPoint point)
    312 {
    313     // 鼠标移动,取色
    314     GetCurrentColor();
    315 
    316     CDialog::OnMouseMove(nFlags, point);
    317 }
    318 
    319 BOOL CCatchColorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
    320 {
    321     if (HTNOWHERE == nHitTest) 
    322     {
    323         // 如果是屏幕取色模式,设置鼠标的形状为取色工具图标
    324         if (CatchColorMode_Screen == m_iCurrentMode) 
    325         {
    326             ::SetCursor(m_hPoint);
    327             return TRUE;
    328         }
    329     }
    330 
    331     return CDialog::OnSetCursor(pWnd, nHitTest, message);
    332 }
    高山流水,海纳百川!
  • 相关阅读:
    Mysql数据库的安装及配置
    java调用ws服务
    linux下安装mysql
    实现离线地图行政区域划分
    linux远程方式,以及基础命令
    javascript对文件的读写
    jsoup对 HTML 文档的解析和操作
    Linux netstat命令详解
    服务器开发工具
    github基本命令
  • 原文地址:https://www.cnblogs.com/ahcc08/p/3692980.html
Copyright © 2011-2022 走看看