www.pudn.com > GetRGB.RAR.zip > ColorSnapDlg.cpp


///////////////////////////////////////////////////////////////////////////// 
// 类名:CColorSnapDlg 
// 功能:屏幕颜色拾取 
// 作者:徐景周(jingzhou_xu@163.net) 
// 组织:未来工作室(Future Studio) 
// 日期:2002.7.1 
//////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h" 
#include "ColorSnap.h" 
#include "ColorSnapDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	CWBButton	m_OK; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	virtual BOOL OnInitDialog(); 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	DDX_Control(pDX, IDOK, m_OK); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
//******************************************************************************** 
//* 名称:OnInitDialog 
//* 作者:徐景周(jingzhou_xu@163.net) 
//* 功能:初始化关于对话框 
//******************************************************************************** 
BOOL CAboutDlg::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	//设置位图按钮 
	m_OK.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 ); 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
///////////////////////////////////////////////////////////////////////////// 
// CColorSnapDlg dialog 
 
CColorSnapDlg::CColorSnapDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CColorSnapDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CColorSnapDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CColorSnapDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CColorSnapDlg) 
	DDX_Control(pDX, IDC_ColorPicker, m_ColorPicker); 
	DDX_Control(pDX, IDC_ABOUT, m_About); 
	DDX_Control(pDX, IDOK, m_OK); 
	DDX_Control(pDX, IDC_EDITRED, m_EditRed); 
	DDX_Control(pDX, IDC_EDITGREEN, m_EditGreen); 
	DDX_Control(pDX, IDC_EDITBLUE, m_EditBlue); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CColorSnapDlg, CDialog) 
	//{{AFX_MSG_MAP(CColorSnapDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_ABOUT, OnAbout) 
	ON_BN_CLICKED(IDC_ColorPicker, OnColorPicker) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorSnapDlg message handlers 
//******************************************************************************** 
//* 名称:OnInitDialog 
//* 作者:徐景周(jingzhou_xu@163.net) 
//* 功能:初始化对话框 
//******************************************************************************** 
BOOL CColorSnapDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	//设置位图按钮 
	m_About.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 ); 
	m_OK.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 ); 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CColorSnapDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CColorSnapDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CColorSnapDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
//******************************************************************************** 
//* 名称:OnAbout 
//* 作者:徐景周(jingzhou_xu@163.net) 
//* 功能:弹出关于对话框 
//******************************************************************************** 
void CColorSnapDlg::OnAbout()  
{ 
	CAboutDlg dlg; 
	dlg.DoModal(); 
} 
 
//******************************************************************************** 
//* 名称:OnColorPicker 
//* 作者:徐景周(jingzhou_xu@163.net) 
//* 功能:进行拾取操作时 
//******************************************************************************** 
void CColorSnapDlg::OnColorPicker()  
{ 
	 if (m_ColorPicker.GetCheck()) 
    { 
        SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 
        SetTimer(1, 80, NULL); 
    } 
    else 
    {  
        SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 
        KillTimer(1); 
    } 
} 
 
//******************************************************************************** 
//* 名称:OnTimer 
//* 作者:徐景周(jingzhou_xu@163.net) 
//* 功能:在此动态拾取屏幕颜色到编辑框中 
//******************************************************************************** 
void CColorSnapDlg::OnTimer(UINT nIDEvent)  
{ 
	if (nIDEvent != 1) 
        return; 
     
    CPoint pt; 
    GetCursorPos(&pt); 
 
    HDC hDC = ::GetDC(NULL); 
    COLORREF clr = ::GetPixel(hDC, pt.x, pt.y); 
     
	CString ClrText; 
    ClrText.Format("%d",GetRValue(clr)); 
	m_EditRed.SetWindowText(ClrText); 
	ClrText.Format("%d",GetGValue(clr)); 
	m_EditGreen.SetWindowText(ClrText); 
	ClrText.Format("%d",GetBValue(clr)); 
	m_EditBlue.SetWindowText(ClrText); 
    
    ::ReleaseDC(NULL, hDC); 
	 
	CDialog::OnTimer(nIDEvent); 
}