ColorSetDlg.cpp
资源名称:44757463.rar [点击查看]
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:12k
源码类别:
绘图程序
开发平台:
Visual C++
- // ColorSetDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "GraphSoft.h"
- #include "ColorSetDlg.h"
- #include "GraphSoftView.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CColorSetDlg dialog
- CColorSetDlg::CColorSetDlg(int nType,CWnd* pParent /*=NULL*/)
- : CDialog(CColorSetDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CColorSetDlg)
- m_nColor_B = 0;
- m_nColor_G = 0;
- m_nColor_R = 0;
- //}}AFX_DATA_INIT
- m_nColorType=nType;
- m_nNumPatterns = 40 ;
- m_nColorOther = 1000 ;
- m_nColorAuto = 1001;
- m_OtherColorRect.top = 118 ;
- m_OtherColorRect.left = 0 ;
- m_OtherColorRect.bottom = 140 ;
- m_OtherColorRect.right = 146 ;
- m_AutoSetRect.top=0;
- m_AutoSetRect.left=0;
- m_AutoSetRect.bottom=22;
- m_AutoSetRect.right=146;
- m_AutoColorRect.left=0;
- m_AutoColorRect.top=0;
- m_AutoColorRect.bottom=22;
- m_AutoColorRect.right=22;
- m_nBoxSize = 18 ;
- m_nNumColumns = 8 ;
- m_nMargin = 1 ;
- m_nCurrent = -1 ;
- m_nSelect = -1 ;
- m_clrArray[0]=RGB(0,0,0);
- m_clrArray[1]=RGB(153,51,0);
- m_clrArray[2]=RGB(51,51,0);
- m_clrArray[3]=RGB(0,51,0);
- m_clrArray[4]=RGB(0,51,102);
- m_clrArray[5]=RGB(0,0,128);
- m_clrArray[6]=RGB(51,51,153);
- m_clrArray[7]=RGB(51,51,51);
- m_clrArray[8]=RGB(128,0,0);
- m_clrArray[9]=RGB(255,102,0);
- m_clrArray[10]=RGB(128,128,0);
- m_clrArray[11]=RGB(0,1128,0);
- m_clrArray[12]=RGB(0,128,128);
- m_clrArray[13]=RGB(0,0,255);
- m_clrArray[14]=RGB(102,102,153);
- m_clrArray[15]=RGB(128,128,128);
- m_clrArray[16]=RGB(255,0,0);
- m_clrArray[17]=RGB(255,153,0);
- m_clrArray[18]=RGB(153,204,0);
- m_clrArray[19]=RGB(51,153,102);
- m_clrArray[20]=RGB(51,204,204);
- m_clrArray[21]=RGB(51,102,255);
- m_clrArray[22]=RGB(128,0,128);
- m_clrArray[23]=RGB(153,153,153);
- m_clrArray[24]=RGB(255,0,255);
- m_clrArray[25]=RGB(255,204,0);
- m_clrArray[26]=RGB(255,255,0);
- m_clrArray[27]=RGB(0,255,0);
- m_clrArray[28]=RGB(0,255,255);
- m_clrArray[29]=RGB(0,204,255);
- m_clrArray[30]=RGB(153,51,102);
- m_clrArray[31]=RGB(192,192,192);
- m_clrArray[32]=RGB(234,255,204);
- m_clrArray[33]=RGB(255,204,53);
- m_clrArray[34]=RGB(255,255,153);
- m_clrArray[35]=RGB(204,255,204);
- m_clrArray[36]=RGB(204,255,255);
- m_clrArray[37]=RGB(53,204,255);
- m_clrArray[38]=RGB(204,153,255);
- m_clrArray[39]=RGB(255,255,255);
- m_pWndParent=pParent;
- }
- void CColorSetDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CColorSetDlg)
- DDX_Text(pDX, IDC_COLOR_B, m_nColor_B);
- DDV_MinMaxInt(pDX, m_nColor_B, 0, 255);
- DDX_Text(pDX, IDC_COLOR_G, m_nColor_G);
- DDV_MinMaxInt(pDX, m_nColor_G, 0, 255);
- DDX_Text(pDX, IDC_COLOR_R, m_nColor_R);
- DDV_MinMaxInt(pDX, m_nColor_R, 0, 255);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CColorSetDlg, CDialog)
- //{{AFX_MSG_MAP(CColorSetDlg)
- ON_WM_PAINT()
- ON_WM_LBUTTONDOWN()
- ON_WM_KILLFOCUS()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CColorSetDlg message handlers
- void CColorSetDlg::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: Add your message handler code here
- int i ;
- draw_cell(&dc,m_nColorAuto);
- for(i=0;i<m_nNumPatterns;i++) {
- draw_cell(&dc,i) ;
- }
- draw_cell(&dc,m_nColorOther) ;
- // Do not call CDialog::OnPaint() for painting messages
- }
- BOOL CColorSetDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CColorSetDlg::Init()
- {
- m_nSelect=-1;
- }
- void CColorSetDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- int i ;
- CRect rect ;
- if(m_OtherColorRect.PtInRect(point)) {
- CColorDialog colordlg(GetInitColor(), CC_FULLOPEN) ;
- if(colordlg.DoModal()==IDOK) {
- SetColor(colordlg.GetColor());
- this->PostMessage(WM_COMMAND,IDCANCEL) ;
- }
- }else if(m_AutoSetRect.PtInRect(point)) {
- SetColor(RGB(0,0,0));
- this->PostMessage(WM_COMMAND,IDCANCEL) ;
- } else {
- for(i=0;i<m_nNumPatterns;i++) {
- if(GetCellRect(i,&rect)) {
- if(rect.PtInRect(point)) {
- m_nCurrent = i ;
- SetColor(GetColor(i));
- Invalidate() ;
- this->PostMessage(WM_COMMAND,IDCANCEL) ;
- }
- }
- }
- }
- CDialog::OnLButtonDown(nFlags, point);
- }
- void CColorSetDlg::OnKillFocus(CWnd* pNewWnd)
- {
- CDialog::OnKillFocus(pNewWnd);
- // TODO: Add your message handler code here
- this->PostMessage(WM_COMMAND,IDCANCEL);
- }
- void CColorSetDlg::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- BOOL flag = FALSE ;
- int i ;
- CRect rect ;
- if(m_AutoSetRect.PtInRect(point)) {
- if(this->m_nSelect!=m_nColorAuto) {
- CClientDC dc(this);
- int old = m_nSelect ;
- m_nSelect = -1 ;
- if(old>=0) draw_cell(&dc,old) ;
- m_nSelect = m_nColorAuto ;
- draw_cell(&dc,m_nColorAuto) ;
- }
- flag = TRUE ;
- }
- if(!flag){
- GetCellRect(m_nColorOther,&rect) ;
- if(rect.PtInRect(point)) {
- if(m_nSelect!=m_nColorOther) {
- CClientDC dc(this);
- int old = this->m_nSelect ;
- m_nSelect = -1 ;
- if(old>=0) draw_cell(&dc,old) ;
- m_nSelect = m_nColorOther ;
- draw_cell(&dc,m_nColorOther) ;
- }
- flag = TRUE ;
- }
- }
- if(!flag) {
- for(i=0;i<m_nNumPatterns;i++) {
- if(GetCellRect(i,&rect)) {
- if(rect.PtInRect(point)) {
- if(this->m_nSelect!=i) {
- CClientDC dc(this);
- int old = this->m_nSelect ;
- this->m_nSelect = -1 ;
- if(old>=0) draw_cell(&dc,old) ;
- this->m_nSelect = i ;
- draw_cell(&dc,i) ;
- }
- flag = TRUE ;
- break ;
- }
- }
- } }
- if(!flag) {
- CClientDC dc(this);
- int old = this->m_nSelect ;
- this->m_nSelect = -1 ;
- if(old>=0) draw_cell(&dc,old) ;
- }
- CDialog::OnMouseMove(nFlags, point);
- }
- void CColorSetDlg::draw_cell(CDC *pDC, int i)
- {
- if(i==m_nColorOther) {
- CRect rect = m_OtherColorRect ;
- rect.top += 2*m_nMargin;
- // Fill background
- pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
- // Draw horizontal line
- pDC->FillSolidRect(m_OtherColorRect.left+2*m_nMargin, m_OtherColorRect.top,
- m_OtherColorRect.Width()-4*m_nMargin, 1, ::GetSysColor(COLOR_3DSHADOW));
- pDC->FillSolidRect(m_OtherColorRect.left+2*m_nMargin, m_OtherColorRect.top+1,
- m_OtherColorRect.Width()-4*m_nMargin, 1, ::GetSysColor(COLOR_3DHILIGHT));
- rect.DeflateRect(1,1);
- // Draw button
- if (i==this->m_nSelect) pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT);
- else if (i==this->m_nCurrent) pDC->DrawEdge(rect, BDR_SUNKENOUTER, BF_RECT);
- NONCLIENTMETRICS ncm;
- ncm.cbSize = sizeof(NONCLIENTMETRICS);
- VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
- CFont font ;
- font.CreateFontIndirect(&(ncm.lfMessageFont)) ;
- CFont* pOldFont = (CFont*)pDC->SelectObject(&font) ;
- pDC->SetBkMode(TRANSPARENT);
- CString str ;
- #ifdef IDS_AUTO_COLOR
- str.LoadString(IDS_OTHER_COLOR) ;
- #else
- str="其它颜色";
- #endif
- pDC->DrawText(str, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- pDC->SelectObject(pOldFont) ;
- }else if(i==m_nColorAuto) {
- CRect rect = m_AutoSetRect ;
- rect.bottom += 2*m_nMargin;
- // Fill background
- pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
- pDC->FillSolidRect(m_AutoSetRect.left+m_nMargin, m_AutoSetRect.bottom-1,
- m_AutoSetRect.Width()-2*m_nMargin, 1, ::GetSysColor(COLOR_3DHILIGHT));
- rect.DeflateRect(m_nMargin,m_nMargin);
- // Draw button
- if (i==this->m_nSelect) pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT);
- else if (i==this->m_nCurrent) pDC->DrawEdge(rect, BDR_SUNKENOUTER, BF_RECT);
- // Draw Out Rect
- pDC->MoveTo(m_AutoSetRect.left+5*m_nMargin,m_AutoSetRect.top+3*m_nMargin);
- pDC->LineTo(m_AutoSetRect.right-5*m_nMargin,m_AutoSetRect.top+3*m_nMargin);
- pDC->LineTo(m_AutoSetRect.right-5*m_nMargin,m_AutoSetRect.bottom-3*m_nMargin);
- pDC->LineTo(m_AutoSetRect.left+5*m_nMargin,m_AutoSetRect.bottom-3*m_nMargin);
- pDC->LineTo(m_AutoSetRect.left+5*m_nMargin,m_AutoSetRect.top+3*m_nMargin);
- // Draw the AutoColorRect
- CPen pen;
- pen.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
- CBrush brush ;
- brush.CreateSolidBrush(RGB(0,0,0)) ;
- CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);
- CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
- rect=m_AutoColorRect;
- rect.DeflateRect(0,4*m_nMargin+1);
- rect.left+=7*m_nMargin+1;
- pDC->Rectangle(rect);
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- // Text
- NONCLIENTMETRICS ncm;
- ncm.cbSize = sizeof(NONCLIENTMETRICS);
- VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
- CFont font ;
- font.CreateFontIndirect(&(ncm.lfMessageFont)) ;
- CFont* pOldFont = (CFont*)pDC->SelectObject(&font) ;
- pDC->SetBkMode(TRANSPARENT);
- CString str ;
- #ifdef IDS_AUTO_COLOR
- str.LoadString(IDS_AUTO_COLOR) ;
- #else
- str="自动";
- #endif
- pDC->DrawText(str, m_AutoSetRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- pDC->SelectObject(pOldFont) ;
- }else {
- CRect rect;
- if (!GetCellRect(i,&rect)) return;
- pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
- if(i==this->m_nSelect) pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT);
- else if(i==this->m_nCurrent) pDC->DrawEdge(rect, BDR_SUNKENOUTER, BF_RECT);
- CPen pen;
- pen.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
- CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);
- CBrush brush ;
- CBrush* pOldBrush ;
- brush.CreateSolidBrush(GetColor(i)) ;
- pOldBrush = (CBrush*) pDC->SelectObject(&brush);
- // Draw the cell colour
- rect.DeflateRect(m_nMargin+1, m_nMargin+1);
- pDC->Rectangle(rect);
- // restore DC and cleanup
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- }
- }
- int CColorSetDlg::GetRow(int i)
- {
- return i / m_nNumColumns;
- }
- int CColorSetDlg::GetColumn(int i)
- {
- return i % m_nNumColumns;
- }
- BOOL CColorSetDlg::GetCellRect(int i,CRect* pRect)
- {
- if(i==this->m_nColorOther) {
- *pRect = this->m_OtherColorRect ;
- }
- else {
- if (i < 0 || i >= m_nNumPatterns)
- return FALSE;
- pRect->left = GetColumn(i) * m_nBoxSize + m_nMargin;
- pRect->top = GetRow(i) * m_nBoxSize + m_nMargin + m_AutoSetRect.Height()+2*m_nMargin;
- pRect->right = pRect->left + m_nBoxSize;
- pRect->bottom = pRect->top + m_nBoxSize;
- }
- return TRUE;
- }
- COLORREF CColorSetDlg::GetColor(int i)
- {
- return m_clrArray[i];
- }
- COLORREF CColorSetDlg::GetColor()
- {
- return RGB(m_nColor_R,m_nColor_G,m_nColor_B);
- }
- void CColorSetDlg::InitColor(COLORREF color)
- {
- m_nColor_R=(int)GetRValue(color);
- m_nColor_G=(int)GetGValue(color);
- m_nColor_B=(int)GetBValue(color);
- for(int i=0;i<m_nNumPatterns;i++){
- if(color==m_clrArray[i]){
- m_nCurrent=i;
- break;
- }
- }
- }
- void CColorSetDlg::SetColor(COLORREF color)
- {
- m_nColor_R=(int)GetRValue(color);
- m_nColor_G=(int)GetGValue(color);
- m_nColor_B=(int)GetBValue(color);
- if(m_nColorType==0&&m_pWndParent!=NULL){
- m_pWndParent->PostMessage(WM_SETCOLOR,WPARAM(color));
- }else if(m_nColorType==1&&m_pWndParent!=NULL){
- m_pWndParent->PostMessage(WM_SETFILLCOLOR,WPARAM(color));
- }
- }
- COLORREF CColorSetDlg::GetInitColor()
- {
- return RGB(m_nColor_R,m_nColor_G,m_nColor_B);
- }
- BOOL CColorSetDlg::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CDialog::PreCreateWindow(cs);
- }