MixColorDlg.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // MixColorDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MixColor.h"
  5. #include "MixColorDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMixColorDlg dialog
  50. CMixColorDlg::CMixColorDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CMixColorDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CMixColorDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. //初始化变量值
  59. m_clrBackGround = GetSysColor(COLOR_MENU);
  60. m_clrTxt = GetSysColor(COLOR_MENUTEXT);
  61. }
  62. void CMixColorDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CMixColorDlg)
  66. DDX_Control(pDX, IDC_TXT, m_stcTxt);
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CMixColorDlg, CDialog)
  70. //{{AFX_MSG_MAP(CMixColorDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. ON_WM_HSCROLL()
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CMixColorDlg message handlers
  79. BOOL CMixColorDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // Add "About..." menu item to system menu.
  83. // IDM_ABOUTBOX must be in the system command range.
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. CString strAboutMenu;
  90. strAboutMenu.LoadString(IDS_ABOUTBOX);
  91. if (!strAboutMenu.IsEmpty())
  92. {
  93. pSysMenu->AppendMenu(MF_SEPARATOR);
  94. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  95. }
  96. }
  97. // Set the icon for this dialog.  The framework does this automatically
  98. //  when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101. // TODO: Add extra initialization here
  102. //获得背景色的rgb值
  103. int nBackBlue = (m_clrBackGround>>16);
  104. int nBackGreen = (m_clrBackGround>>8)&0x0000FF;
  105. int nBackRed = m_clrBackGround&0x000000FF;
  106. //获得文本颜色的rgb值
  107. int nTxtBlue = (m_clrTxt>>16);
  108. int nTxtGreen = (m_clrTxt>>8)&0x0000FF;
  109. int nTxtRed = m_clrTxt&0x000000FF;
  110. //获得滚动条窗口的指针
  111. CSliderCtrl* pTxtRed = (CSliderCtrl*)GetDlgItem(IDC_TXT_RED);
  112. CSliderCtrl* pTxtGreen = (CSliderCtrl*)GetDlgItem(IDC_TXT_GREEN);
  113. CSliderCtrl* pTxtBlue = (CSliderCtrl*)GetDlgItem(IDC_TXT_BLUE);
  114. CSliderCtrl* pBackRed = (CSliderCtrl*)GetDlgItem(IDC_BACK_RED);
  115. CSliderCtrl* pBackGreen = (CSliderCtrl*)GetDlgItem(IDC_BACK_GREEN);
  116. CSliderCtrl* pBackBlue = (CSliderCtrl*)GetDlgItem(IDC_BACK_BLUE);
  117. //设定滚动条的滚动范围和初始位置
  118. pTxtRed->SetRange(0,255);
  119. pTxtGreen->SetRange(0,255);
  120. pTxtBlue->SetRange(0,255);
  121. pTxtRed->SetPos(nTxtRed);
  122. pTxtGreen->SetPos(nTxtGreen);
  123. pTxtBlue->SetPos(nTxtBlue);
  124. pBackRed->SetRange(0,255);
  125. pBackGreen->SetRange(0,255);
  126. pBackBlue->SetRange(0,255);
  127. pBackRed->SetPos(nBackRed);
  128. pBackGreen->SetPos(nBackGreen);
  129. pBackBlue->SetPos(nBackBlue);
  130. return TRUE;  // return TRUE  unless you set the focus to a control
  131. }
  132. void CMixColorDlg::OnSysCommand(UINT nID, LPARAM lParam)
  133. {
  134. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  135. {
  136. CAboutDlg dlgAbout;
  137. dlgAbout.DoModal();
  138. }
  139. else
  140. {
  141. CDialog::OnSysCommand(nID, lParam);
  142. }
  143. }
  144. // If you add a minimize button to your dialog, you will need the code below
  145. //  to draw the icon.  For MFC applications using the document/view model,
  146. //  this is automatically done for you by the framework.
  147. void CMixColorDlg::OnPaint() 
  148. {
  149. CPaintDC dc(this); // device context for painting
  150. if (IsIconic())
  151. {
  152. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  153. // Center icon in client rectangle
  154. int cxIcon = GetSystemMetrics(SM_CXICON);
  155. int cyIcon = GetSystemMetrics(SM_CYICON);
  156. CRect rect;
  157. GetClientRect(&rect);
  158. int x = (rect.Width() - cxIcon + 1) / 2;
  159. int y = (rect.Height() - cyIcon + 1) / 2;
  160. // Draw the icon
  161. dc.DrawIcon(x, y, m_hIcon);
  162. }
  163. else
  164. {
  165. CDialog::OnPaint();
  166. }
  167. //获得静态文本框的窗口指针和其设备场景
  168. CStatic* pTxt = (CStatic*)GetDlgItem(IDC_TXT);
  169. CDC* pDC = pTxt->GetDC();
  170. //获得静态文本框的客户区域
  171. CRect rc;
  172. pTxt->GetClientRect(&rc);
  173. //创建画背景的画刷,并用其填充背景
  174. CBrush brush;
  175. brush.CreateSolidBrush(m_clrBackGround);
  176. pDC->FillRect(&rc,&brush);
  177. //设定文字的色彩,并输出文字
  178. pDC->SetBkColor(m_clrBackGround);
  179. pDC->SetTextColor(m_clrTxt);
  180. pDC->DrawText("调色程序",&rc,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  181. pTxt->ReleaseDC(pDC);
  182. }
  183. // The system calls this to obtain the cursor to display while the user drags
  184. //  the minimized window.
  185. HCURSOR CMixColorDlg::OnQueryDragIcon()
  186. {
  187. return (HCURSOR) m_hIcon;
  188. }
  189. void CMixColorDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  190. {
  191. CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  192. //不是拖动的消息不处理
  193. if( nSBCode != SB_THUMBTRACK)
  194. return;
  195. //获得静态文本框的客户区域
  196. CStatic* pTxt = (CStatic*)GetDlgItem(IDC_TXT);
  197. CRect rc;
  198. pTxt->GetClientRect(&rc);
  199. //判断拖动的是哪个滚动条,然后根据滚动条的当前位置改变文本框的前景色和背景色
  200. switch(pScrollBar->GetDlgCtrlID())
  201. {
  202. case IDC_TXT_BLUE:
  203. m_clrTxt = (m_clrTxt&0x0000FFFF)+(nPos<<16);
  204. break;
  205. case IDC_TXT_GREEN:
  206. m_clrTxt = (m_clrTxt&0x00FF00FF)+(nPos<<8);
  207. break;
  208. case IDC_TXT_RED:
  209. m_clrTxt = (m_clrTxt&0x00FFFF00)+nPos;
  210. break;
  211. case IDC_BACK_BLUE:
  212. m_clrBackGround = (m_clrBackGround&0x0000FFFF)+(nPos<<16);
  213. break;
  214. case IDC_BACK_GREEN:
  215. m_clrBackGround = (m_clrBackGround&0x00FF00FF)+(nPos<<8);
  216. break;
  217. case IDC_BACK_RED:
  218. m_clrBackGround = (m_clrBackGround&0x00FFFF00)+nPos;
  219. break;
  220. }
  221. //重画文本框窗口区域
  222. InvalidateRect(&rc);
  223. }