www.gusucode.com > VC++屏幕取色程序源码升级版源码程序 > VC++屏幕取色程序源码升级版源码程序\code\Page1.cpp
// Page1.cpp : implementation file // download by http://www.NewXing.com #include "stdafx.h" #include "Page.h" #include "Page1.h" #include"PageDlg.h" #include"mmsystem.h" #include<stdlib.h> #pragma comment(lib,"winmm.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPage1 property page IMPLEMENT_DYNCREATE(CPage1, CPropertyPage) CPage1::CPage1() : CPropertyPage(CPage1::IDD) { //{{AFX_DATA_INIT(CPage1) m_strXYPos = _T(""); m_strHwnd = _T(""); m_strPassword = _T(""); m_strRGB = _T(""); m_hex = _T(""); m_strTitle = _T(""); m_strClass = _T(""); m_rr = 0; m_gg = 0; m_bb = 0; m_nTemp=0; //}}AFX_DATA_INIT m_bIsCapturing = FALSE; m_color=RGB(0,128,64); m_bLeft=true; m_x=::GetSystemMetrics(SM_CXSCREEN); m_y=::GetSystemMetrics(SM_CYSCREEN); } CPage1::~CPage1() { } void CPage1::DoDataExchange(CDataExchange* pDX) { CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPage1) DDX_Control(pDX, IDC_G, m_editG); DDX_Control(pDX, IDC_B, m_editB); DDX_Control(pDX, IDC_R, m_editR); DDX_Control(pDX, IDC_COMBO1, m_cc); DDX_Control(pDX, IDC_SLIDER3, m_sldB); DDX_Control(pDX, IDC_SLIDER2, m_sldG); DDX_Control(pDX, IDC_SLIDER1, m_sldR); DDX_Control(pDX, IDC_RECT_START, m_start); DDX_Control(pDX, IDC_EDIT_RGB, m_rgb); DDX_Text(pDX, IDC_EDIT_POS, m_strXYPos); DDX_Text(pDX, IDC_EDIT_HWND, m_strHwnd); DDX_Text(pDX, IDC_RGB, m_strRGB); DDX_Text(pDX, IDC_EDIT_HEX, m_hex); DDV_MaxChars(pDX, m_hex, 6); DDX_Text(pDX, IDC_EDIT_TITLE, m_strTitle); DDX_Text(pDX, IDC_EDIT_CLASS, m_strClass); DDX_Text(pDX, IDC_R, m_rr); DDX_Text(pDX, IDC_G, m_gg); DDX_Text(pDX, IDC_B, m_bb); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPage1, CPropertyPage) //{{AFX_MSG_MAP(CPage1) ON_WM_LBUTTONDOWN() ON_WM_CTLCOLOR() ON_WM_LBUTTONDBLCLK() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_PAINT() ON_WM_HSCROLL() ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1) ON_BN_CLICKED(IDC_CONVERT, OnConvert) ON_EN_CHANGE(IDC_R, OnChangeR) ON_EN_CHANGE(IDC_G, OnChangeG) ON_EN_CHANGE(IDC_B, OnChangeB) ON_WM_TIMER() ON_BN_CLICKED(IDC_COPY, OnCopy) ON_BN_CLICKED(IDC_ADD, OnAdd) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPage1 message handlers void CPage1::OnLButtonDown(UINT nFlags, CPoint point) { if (::ChildWindowFromPoint(m_hWnd,point) == m_start.m_hWnd) { SetCapture(); SetCursor(AfxGetApp()->LoadCursor(IDC_MAG_GLASS)); m_start.SetIcon(AfxGetApp()->LoadIcon(IDI_MAG_GLASS_BLANK)); m_bIsCapturing = true; if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bHide) { AfxGetMainWnd()->ShowWindow(SW_SHOWMINIMIZED); m_psd=new CSmallDlg; m_psd->Create(IDD_SMALL); new CFaderWnd(m_psd); m_psd->ShowWindow(SW_SHOW); } } AfxGetMainWnd()->SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,0); CPropertyPage::OnLButtonDown(nFlags, point); } HBRUSH CPage1::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(pWnd->GetDlgCtrlID()==IDC_RGB || pWnd->GetDlgCtrlID()==IDC_EDIT_HEX ) pDC->SetTextColor(RGB(0,128,192)); if(pWnd->GetDlgCtrlID()==IDC_STATE) pDC->SetTextColor(RGB(0,0,255)); return hbr; } void CPage1::OnLButtonDblClk(UINT nFlags, CPoint point) { if(m_rect.PtInRect(point)) { CColorDialog colordlg; colordlg.m_cc.Flags |= CC_FULLOPEN; //颜色对话框显示全部 if(colordlg.DoModal()==IDOK) m_color=colordlg.GetColor(); InvalidateRect(m_rect); m_strRGB.Format("RGB(%d,%d,%d)",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.Format("%02x%02x%02x",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.MakeUpper(); this->SetColorValue(); UpdateData(false); } CPropertyPage::OnLButtonDblClk(nFlags, point); } void CPage1::OnLButtonUp(UINT nFlags, CPoint point) { if (m_bIsCapturing) { ReleaseCapture(); m_bIsCapturing = FALSE; SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW)); m_start.SetIcon(AfxGetApp()->LoadIcon(IDI_MAG_GLASS)); if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bAutoCopy) //是否自动复制到剪贴板上 { GLOBALHANDLE hGMem=::GlobalAlloc(GHND,m_strRGB.GetLength()+1); LPSTR lpGMem=(LPSTR)::GlobalLock(hGMem); strcpy(lpGMem,(LPTSTR)(LPCTSTR)m_strRGB); ::GlobalUnlock(hGMem); if(::OpenClipboard(NULL)) { ::EmptyClipboard(); ::SetClipboardData(CF_TEXT,hGMem); ::CloseClipboard(); } static int tt=0; CString str; str.Format("%d",tt); m_strTemp=" 颜色值已复制到剪贴板上___"+ str; tt++; ((CPageDlg*)AfxGetMainWnd())->SetStateText(m_strTemp); SetTimer(2,100,NULL); PlaySound("CopySound",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT); } if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bHide) //显示主窗口 { AfxGetMainWnd()->ShowWindow(SW_SHOWNORMAL); m_psd->ShowWindow(SW_HIDE); new CFaderWnd(m_psd); m_psd->DestroyWindow(); delete m_psd; } int tmp=m_cc.AddColor(m_color,m_strRGB); if(tmp !=-1) m_cc.SetCurSel(tmp); if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bSaveColor) { CFileFind fd; if(!fd.FindFile("Recent_Color.txt")) { CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeRead|CFile::modeCreate); sf.Close(); } fd.Close(); CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeReadWrite); sf.SeekToEnd(); CString str; str.Format("%ld\n",m_color); sf.WriteString(str); sf.Close(); } } CPropertyPage::OnLButtonUp(nFlags, point); } BOOL CPage1::OnInitDialog() { CPropertyPage::OnInitDialog(); GetDlgItem(IDC_RECT22)->GetWindowRect(m_rect); m_rect.left+=3; m_rect.top +=3; m_rect.right -=3; m_rect.bottom -=3; ScreenToClient(&m_rect); m_font.CreateFont(18, 0,0,0,FW_BOLD, 0,0,0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Fixdays"); GetDlgItem(IDC_RGB)->SetFont(&m_font); GetDlgItem(IDC_EDIT_HEX)->SetFont(&m_font); /*--------------------------------------------------------*/ m_sldR.SetRange(0,255,true); m_sldG.SetRange(0,255,true); m_sldB.SetRange(0,255,true); this->SetColorValue(); /*--------------------------------------------------------*/ { m_tooltip.Create(this); m_tooltip.SetMaxTipWidth(200); m_tooltip.SetDelayTime(100); m_tooltip.Activate(TRUE); m_tooltip.AddTool(GetDlgItem(IDC_COMBO1),"取色后的颜色将会添加到该组合框内"); m_tooltip.AddTool(GetDlgItem(IDC_SLIDER1),"拖动此滑块进行红色调节"); m_tooltip.AddTool(GetDlgItem(IDC_SLIDER2),"拖动此滑块进行绿色调节"); m_tooltip.AddTool(GetDlgItem(IDC_SLIDER3),"拖动此滑块进行蓝色调节"); m_tooltip.AddTool(GetDlgItem(IDC_EDIT_HEX),"输入16进制颜色后单击“转换”按扭,可以转换成RGB格式和查看该颜色"); } m_cc.SetCurSel(m_cc.AddColor(m_color,"默认颜色")); if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bSaveColor) { CFileFind fd; if(!fd.FindFile("Recent_Color.txt")) { CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeRead|CFile::modeCreate); sf.Close(); } fd.Close(); CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeRead); CString str; while(sf.ReadString(str)) { m_cc.AddColor((COLORREF)atol(str),str); } } return TRUE; } void CPage1::OnMouseMove(UINT nFlags, CPoint point) { if (m_bIsCapturing) { ClientToScreen(&point); m_strXYPos.Format("X=%ld, Y=%ld", point.x, point.y); m_color=::GetPixel(::GetDC(0),point.x,point.y); InvalidateRect(m_rect,true); m_strRGB.Format("RGB(%d,%d,%d)",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.Format("%02X%02X%02X",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bHide) { m_psd->m_color=m_color; m_psd->InvalidateRect(m_psd->m_rect); m_psd->SetDlgItemText(IDC_RGB,m_strRGB); CRect rcLeft(0,0,189,129),rcRight(m_x-189,0,m_x,129); if(m_bLeft && rcLeft.PtInRect(point)) { m_psd->MoveWindow(rcRight); m_bLeft=false; } if(!m_bLeft && rcRight.PtInRect(point)) { m_psd->MoveWindow(rcLeft); m_bLeft=true; } } this->SetColorValue(); HWND hWndCur= ::WindowFromPoint(point); m_strHwnd.Format("%ld", hWndCur); //窗口句柄 char szClassName[255]; ::GetClassName(hWndCur, szClassName, 255); this->m_strClass=szClassName; char szWndName[255]; ::GetWindowText(hWndCur,szWndName,255); m_strTitle=szWndName; UpdateData(FALSE); } CPropertyPage::OnMouseMove(nFlags, point); } void CPage1::OnPaint() { CPaintDC dc(this); CBrush br(m_color); dc.FillRect(m_rect, &br); } void CPage1::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { m_rr=m_sldR.GetPos(); m_gg=m_sldG.GetPos(); m_bb=m_sldB.GetPos(); m_color=RGB(m_rr,m_gg,m_bb); InvalidateRect(m_rect,true); m_strRGB.Format("RGB(%d,%d,%d)",m_rr,m_gg,m_bb); m_hex.Format("%02X%02X%02X",m_rr,m_gg,m_bb); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); UpdateData(false); CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar); } void CPage1::SetColorValue() //根据m_color的值来设置滑动块的位置和编辑框的内容 { m_rr=GetRValue(m_color); m_gg=GetGValue(m_color); m_bb=GetBValue(m_color); m_sldR.SetPos(m_rr); m_sldG.SetPos(m_gg); m_sldB.SetPos(m_bb); UpdateData(false); } void CPage1::OnSelchangeCombo1() { m_color=m_cc.GetSelColor(); InvalidateRect(m_rect,true); this->SetColorValue(); m_strRGB.Format("RGB(%d,%d,%d)",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.Format("%02X%02X%02X",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); UpdateData(false); if(((CPageDlg*)AfxGetMainWnd())->m_page2.m_bAutoCopy) //是否自动复制到剪贴板上 { GLOBALHANDLE hGMem=::GlobalAlloc(GHND,m_strRGB.GetLength()+1); LPSTR lpGMem=(LPSTR)::GlobalLock(hGMem); strcpy(lpGMem,(LPTSTR)(LPCTSTR)m_strRGB); ::GlobalUnlock(hGMem); if(::OpenClipboard(NULL)) { ::EmptyClipboard(); ::SetClipboardData(CF_TEXT,hGMem); ::CloseClipboard(); } static int nn=0; CString str; str.Format("%d",nn); m_strTemp=" 颜色值已复制到剪贴板上___"+ str; nn++; ((CPageDlg*)AfxGetMainWnd())->SetStateText(m_strTemp); SetTimer(2,100,NULL); PlaySound("CopySound",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT); } } BOOL CPage1::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); return CPropertyPage::PreTranslateMessage(pMsg); } void CPage1::Reset() { m_cc.ResetContent(); } /*=====================将16进制颜色转换成RGB表示的格式===================*/ #include <math.h> int strHexToInt(char* strSource) { int nTemp=0; CString strTemp; strTemp=strSource; for(char cc='G',dd='g'; cc<='Z',dd<='z'; cc++,dd++) //判断输入的字符串是否合法 { if(strTemp.Find(cc,0) !=-1 || strTemp.Find(dd,0) !=-1) { ::MessageBox(NULL,"请输入正确的16进制字符串!","输入错误",MB_ICONEXCLAMATION); return -1; } } for(int i = 0; i<(int)::strlen(strSource); i++) { int nDecNum; switch(strSource[i]) { case 'a': case 'A': nDecNum = 10; break; case 'b': case 'B': nDecNum = 11; break; case 'c': case 'C': nDecNum = 12; break; case 'd': case 'D': nDecNum = 13; break; case 'e': case 'E': nDecNum = 14; break; case 'f': case 'F': nDecNum = 15; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': nDecNum = strSource[i] - '0'; break; default: return 0; } nTemp += nDecNum * (int)::pow(16,::strlen(strSource)-i -1); } return nTemp; } void CPage1::OnConvert() { UpdateData(); /* if(m_hex.IsEmpty()) { MessageBox("请现在上面的编辑框中输入16进制颜色!","屏幕取色",MB_ICONINFORMATION); return; } if(m_hex.GetLength() !=6) { MessageBox("请输入6个字符长的16进制颜色!","屏幕取色",MB_ICONINFORMATION); return; } */ if(strHexToInt((LPSTR)(LPCTSTR)m_hex) !=-1) { int rr=strHexToInt((LPSTR)(LPCTSTR)m_hex.Left(2)); int gg=strHexToInt((LPSTR)(LPCTSTR)m_hex.Mid(2).Left(2)); int bb=strHexToInt((LPSTR)(LPCTSTR)m_hex.Right(2)); m_color=RGB(rr,gg,bb); InvalidateRect(m_rect); m_strRGB.Format("RGB(%d,%d,%d)",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.Format("%02x%02x%02x",GetRValue(m_color),GetGValue(m_color),GetBValue(m_color)); m_hex.MakeUpper(); this->SetColorValue(); UpdateData(false); } } void CPage1::OnChangeR() { m_editR.SetLimitText(3); UpdateData(); if(m_rr >255) m_rr=255; m_color=RGB(m_rr,m_gg,m_bb); InvalidateRect(m_rect); m_sldR.SetPos(m_rr); m_sldG.SetPos(m_gg); m_sldB.SetPos(m_bb); m_strRGB.Format("RGB(%d,%d,%d)",m_rr,m_gg,m_bb); m_hex.Format("%02X%02X%02X",m_rr,m_gg,m_bb); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); UpdateData(false); } void CPage1::OnChangeG() { m_editG.SetLimitText(3); UpdateData(); if(m_gg >255) m_gg=255; m_color=RGB(m_rr,m_gg,m_bb); InvalidateRect(m_rect); m_sldR.SetPos(m_rr); m_sldG.SetPos(m_gg); m_sldB.SetPos(m_bb); m_strRGB.Format("RGB(%d,%d,%d)",m_rr,m_gg,m_bb); m_hex.Format("%02X%02X%02X",m_rr,m_gg,m_bb); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); UpdateData(false); } void CPage1::OnChangeB() { m_editB.SetLimitText(3); UpdateData(); if(m_bb >255) m_bb=255; m_color=RGB(m_rr,m_gg,m_bb); InvalidateRect(m_rect); m_sldR.SetPos(m_rr); m_sldG.SetPos(m_gg); m_sldB.SetPos(m_bb); m_strRGB.Format("RGB(%d,%d,%d)",m_rr,m_gg,m_bb); m_hex.Format("%02X%02X%02X",m_rr,m_gg,m_bb); m_hex.MakeUpper(); AfxGetMainWnd()->SetWindowText(m_strRGB); UpdateData(false); } void CPage1::OnTimer(UINT nIDEvent) { static BOOL bb=false; ((CPageDlg*)AfxGetMainWnd())->SetStateText(bb? "":m_strTemp); bb=!bb; m_nTemp++; if(m_nTemp >8) { m_nTemp=0; KillTimer(2); ((CPageDlg*)AfxGetMainWnd())->SetStateText(m_strTemp); } CPropertyPage::OnTimer(nIDEvent); } void CPage1::OnCopy() { GLOBALHANDLE hGMem=::GlobalAlloc(GHND,m_strRGB.GetLength()+1); LPSTR lpGMem=(LPSTR)::GlobalLock(hGMem); strcpy(lpGMem,(LPTSTR)(LPCTSTR)m_strRGB); ::GlobalUnlock(hGMem); if(::OpenClipboard(NULL)) { ::EmptyClipboard(); ::SetClipboardData(CF_TEXT,hGMem); ::CloseClipboard(); } m_strTemp=" 颜色值已复制到剪贴板上"; ((CPageDlg*)AfxGetMainWnd())->SetStateText(m_strTemp); SetTimer(2,100,NULL); PlaySound("CopySound",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT); } void CPage1::OnAdd() { int tmp=m_cc.AddColor(m_color,m_strRGB); if(tmp ==-1) ((CPageDlg*)AfxGetMainWnd())->SetStateText(" 该颜色已经存在了!"); else { ((CPageDlg*)AfxGetMainWnd())->SetStateText(" 添加成功"); m_cc.SetCurSel(tmp); } CFileFind fd; if(!fd.FindFile("Recent_Color.txt")) { CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeRead|CFile::modeCreate); sf.Close(); } fd.Close(); CStdioFile sf; sf.Open("Recent_Color.txt",CFile::modeReadWrite); sf.SeekToEnd(); CString str; str.Format("%ld\n",m_color); sf.WriteString(str); sf.Close(); PlaySound("CopySound",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT); }