ColorSnapDlg.cpp
上传用户:boxulig
上传日期:2022-06-30
资源大小:3348k
文件大小:7k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // ColorSnapDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ColorSnap.h"
  5. #include "ColorSnapDlg.h"
  6. #include <afx.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CColorSnapDlg dialog
  51. CColorSnapDlg::CColorSnapDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CColorSnapDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CColorSnapDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CColorSnapDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CColorSnapDlg)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CColorSnapDlg, CDialog)
  68. //{{AFX_MSG_MAP(CColorSnapDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_WM_MOUSEMOVE()
  73. ON_WM_TIMER()
  74. ON_WM_LBUTTONDOWN()
  75. ON_WM_LBUTTONUP()
  76. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  77. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  78. ON_WM_DESTROY()
  79. //}}AFX_MSG_MAP
  80. END_MESSAGE_MAP()
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CColorSnapDlg message handlers
  83. BOOL CColorSnapDlg::OnInitDialog()
  84. {
  85. CDialog::OnInitDialog();
  86. // Add "About..." menu item to system menu.
  87. // IDM_ABOUTBOX must be in the system command range.
  88. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89. ASSERT(IDM_ABOUTBOX < 0xF000);
  90. CMenu* pSysMenu = GetSystemMenu(FALSE);
  91. if (pSysMenu != NULL)
  92. {
  93. CString strAboutMenu;
  94. strAboutMenu.LoadString(IDS_ABOUTBOX);
  95. if (!strAboutMenu.IsEmpty())
  96. {
  97. pSysMenu->AppendMenu(MF_SEPARATOR);
  98. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99. }
  100. }
  101. // Set the icon for this dialog.  The framework does this automatically
  102. //  when the application's main window is not a dialog
  103. SetIcon(m_hIcon, TRUE); // Set big icon
  104. SetIcon(m_hIcon, FALSE); // Set small icon
  105. // TODO: Add extra initialization here
  106. bCaptureWay = false; //默认是定时器方式捕获全屏鼠标
  107. SetTimer(1, 1, NULL);
  108. bTimerRunning = true;
  109. return TRUE;  // return TRUE  unless you set the focus to a control
  110. }
  111. void CColorSnapDlg::OnSysCommand(UINT nID, LPARAM lParam)
  112. {
  113. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  114. {
  115. CAboutDlg dlgAbout;
  116. dlgAbout.DoModal();
  117. }
  118. else
  119. {
  120. CDialog::OnSysCommand(nID, lParam);
  121. }
  122. }
  123. // If you add a minimize button to your dialog, you will need the code below
  124. //  to draw the icon.  For MFC applications using the document/view model,
  125. //  this is automatically done for you by the framework.
  126. void CColorSnapDlg::OnPaint() 
  127. {
  128. if (IsIconic())
  129. {
  130. CPaintDC dc(this); // device context for painting
  131. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  132. // Center icon in client rectangle
  133. int cxIcon = GetSystemMetrics(SM_CXICON);
  134. int cyIcon = GetSystemMetrics(SM_CYICON);
  135. CRect rect;
  136. GetClientRect(&rect);
  137. int x = (rect.Width() - cxIcon + 1) / 2;
  138. int y = (rect.Height() - cyIcon + 1) / 2;
  139. // Draw the icon
  140. dc.DrawIcon(x, y, m_hIcon);
  141. }
  142. else
  143. {
  144. CDialog::OnPaint();
  145. }
  146. }
  147. // The system calls this to obtain the cursor to display while the user drags
  148. //  the minimized window.
  149. HCURSOR CColorSnapDlg::OnQueryDragIcon()
  150. {
  151. return (HCURSOR) m_hIcon;
  152. }
  153. void CColorSnapDlg::OnMouseMove(UINT nFlags, CPoint point) 
  154. {
  155. // TODO: Add your message handler code here and/or call default
  156. //对于SetCapture方法捕获鼠标,采用处理WM_MOUSEMOVE的方法
  157. if(bCaptureWay){
  158. HDC hDC = ::GetDC(NULL);
  159. POINT pt;
  160. GetCursorPos(&pt);
  161. COLORREF clr = ::GetPixel(hDC, pt.x, pt.y);
  162. CString strRGB;
  163. strRGB.Format("R:%d, G:%d, B:%d", GetRValue(clr), GetGValue(clr), GetBValue(clr));
  164. SetDlgItemText(IDC_RGBBOX, strRGB);
  165. ::ReleaseDC(NULL, hDC);
  166. }
  167. CDialog::OnMouseMove(nFlags, point);
  168. }
  169. void CColorSnapDlg::OnTimer(UINT nIDEvent) 
  170. {
  171. // TODO: Add your message handler code here and/or call default
  172. //对于定时器捕获鼠标,采用处理WM_TIMER的方法
  173. if(!bCaptureWay){
  174. HDC hDC = ::GetDC(NULL);
  175. POINT pt;
  176. GetCursorPos(&pt);
  177. COLORREF clr = ::GetPixel(hDC, pt.x, pt.y);
  178. CString strRGB;
  179. strRGB.Format("R:%d, G:%d, B:%d", GetRValue(clr), GetGValue(clr), GetBValue(clr));
  180. SetDlgItemText(IDC_RGBBOX, strRGB);
  181. ::ReleaseDC(NULL, hDC);
  182. }
  183. CDialog::OnTimer(nIDEvent);
  184. }
  185. void CColorSnapDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  186. {
  187. // TODO: Add your message handler code here and/or call default
  188. if(bCaptureWay) //bCaptureWay==1表示SetCapture方法捕获全屏鼠标
  189. SetCapture();
  190. CDialog::OnLButtonDown(nFlags, point);
  191. }
  192. void CColorSnapDlg::OnLButtonUp(UINT nFlags, CPoint point) 
  193. {
  194. // TODO: Add your message handler code here and/or call default
  195. if(bCaptureWay)
  196. ReleaseCapture();
  197. //正常的鼠标在应用程序的窗口客户群外的地方抬起是不会响应WM_LBUTTONDOWN的,这里会响应是因为SetCapture将捕获该函数之后的所有鼠标动作,不管它是否在应用程序的窗口客户区范围内。
  198. CDialog::OnLButtonUp(nFlags, point);
  199. }
  200. void CColorSnapDlg::OnButton1() 
  201. {
  202. // TODO: Add your control notification handler code here
  203. //使用定时器方法捕获全屏鼠标
  204. bCaptureWay = false;
  205. if(bTimerRunning)
  206. KillTimer(1);
  207. SetTimer(1, 1, NULL); //1ms发一次WM_TIMER
  208. bTimerRunning = true;
  209. }
  210. void CColorSnapDlg::OnButton2() 
  211. {
  212. // TODO: Add your control notification handler code here
  213. //使用SetCapture方法捕获全屏鼠标
  214. bCaptureWay = true;
  215. if(bTimerRunning){
  216. KillTimer(1);
  217. bTimerRunning = false;
  218. }
  219. }
  220. void CColorSnapDlg::OnDestroy() 
  221. {
  222. CDialog::OnDestroy();
  223. // TODO: Add your message handler code here
  224. if(bTimerRunning)
  225. KillTimer(1);
  226. }