ColourPopup.cpp
上传用户:cn05999
上传日期:2020-06-29
资源大小:84k
文件大小:15k
- // ColourPopup.cpp : implementation file
- //
- #include "stdafx.h"
- #include "CProgressCtrl.h"
- #include "ColourPopup.h"
- //#include "ColourPicker.h"
- #include "math.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define TEXT_BOX_VALUE -2 //定义用户文本显示的按钮坐标
- #define MAX_COLOURS 100 //定义颜色板中最多的颜色数
- /////////////////////////////////////////////////////////////////////////////
- // CColourPopup
- ColourTableEntry CColourPopup::m_crColours[]=
- {
- {RGB(0x00,0x00,0x00), _T("黑色") },
- {RGB(0xA5,0x2A,0x00), _T("棕色") },
- {RGB(0x00,0x40,0x40), _T("橄榄绿色") },
- {RGB(0x00,0x55,0x00), _T("暗绿色") },
- {RGB(0x00,0x00,0x5E), _T("深青色") },
- {RGB(0x00,0x00,0x8B), _T("深蓝色") },
- {RGB(0x4B,0x00,0x82), _T("深紫蓝色") },
- {RGB(0x28,0x28,0x28), _T("深灰色") },
- {RGB(0x8B,0x00,0x00), _T("深红色") },
- {RGB(0xFF,0x68,0x20), _T("桔红色") },
- {RGB(0x8B,0x8B,0x00), _T("深黄色") },
- {RGB(0x00,0x93,0x00), _T("绿色") },
- {RGB(0x38,0x8E,0x8E), _T("青色") },
- {RGB(0x00,0x00,0xFF), _T("蓝色") },
- {RGB(0x7B,0x7B,0xC0), _T("绿灰色") },
- {RGB(0x66,0x66,0x66), _T("灰色") },
- {RGB(0xFF,0x00,0x00), _T("红色") },
- {RGB(0xFF,0xAD,0x5B), _T("淡桔红色") },
- {RGB(0x32,0xCD,0x32), _T("浅绿色") },
- {RGB(0x3C,0xB3,0x71), _T("海蓝色") },
- {RGB(0x7F,0xFF,0xD4), _T("浅绿色") },
- {RGB(0x7D,0x9E,0xC0), _T("淡蓝色") },
- {RGB(0x80,0x00,0x80), _T("紫罗兰色") },
- {RGB(0x7F,0x7F,0x7F), _T("灰色") },
- {RGB(0xFF,0xC0,0xCB), _T("粉红色") },
- {RGB(0xFF,0xD7,0x00), _T("金色") },
- {RGB(0xFF,0xFF,0x00), _T("黄色") },
- {RGB(0x00,0xFF,0x00), _T("亮绿色") },
- {RGB(0x40,0xE0,0xD0), _T("绿松色") },
- {RGB(0xC0,0xFF,0xFF), _T("天蓝色") },
- {RGB(0x48,0x00,0x48), _T("梅红") },
- {RGB(0xC0,0xC0,0xC0), _T("淡灰色") },
- {RGB(0xFF,0xE4,0xE1), _T("玫瑰色") },
- {RGB(0xD2,0xB4,0x8C), _T("棕褐色") },
- {RGB(0xFF,0xFF,0xE0), _T("淡黄色") },
- {RGB(0x98,0xFB,0x98), _T("浅绿色") },
- {RGB(0xAF,0xEE,0xEE), _T("浅绿宝石色")},
- {RGB(0x68,0x83,0x8B), _T("浅蓝色") },
- {RGB(0xE6,0xE6,0xFA), _T("淡紫色") },
- {RGB(0xFF,0xFF,0xFF), _T("白色") },
- };
- CColourPopup::CColourPopup()
- {
- Initialize();
- }
- CColourPopup::~CColourPopup()
- {
- m_Font.DeleteObject();
- m_Palette.DeleteObject();
- }
- BEGIN_MESSAGE_MAP(CColourPopup, CWnd)
- //{{AFX_MSG_MAP(CColourPopup)
- ON_WM_PAINT()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_KEYDOWN()
- ON_WM_PALETTECHANGED()
- ON_WM_QUERYNEWPALETTE()
- ON_WM_NCDESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CColourPopup message handlers
- void CColourPopup::Initialize()
- {
- m_nNumColours =sizeof(m_crColours)/sizeof(ColourTableEntry);
- ASSERT(m_nNumColours<=MAX_COLOURS);
- if(m_nNumColours>MAX_COLOURS)
- m_nNumColours=MAX_COLOURS;
- m_nNumColumns=0;
- m_nNumRows=0;
- m_nBoxSize=18;
- m_nMargin=::GetSystemMetrics(SM_CXEDGE);
- m_nCurrentRow=-1;
- m_nCurrentCol=-1;
- m_nChosenColourRow=-1;
- m_nChosenColourCol=-1;;
- m_bShowCustom=TRUE;
- m_strCustomText=_T("其它颜色");
- m_crInitialColour=m_crColour=RGB(0,0,0);
- m_pParent=NULL;
- if(m_nBoxSize-2*m_nMargin-2<5) m_nBoxSize=5+2*m_nMargin+2;
- //在颜色板中使用非客户区的字体
- //以下代码创建NONCLIENTMETRICS结构,
- NONCLIENTMETRICS ncm;
- ncm.cbSize=sizeof(NONCLIENTMETRICS);
- VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,sizeof(NONCLIENTMETRICS),&ncm,0));
- m_Font.CreateFontIndirect(&(ncm.lfMessageFont));
- //创建 应用颜色板结构
- struct
- {
- //逻辑颜色板
- LOGPALETTE Logpalette;
- PALETTEENTRY PalEntry[MAX_COLOURS];
- }pal;
- //颜色板的入口数为定义的颜色数
- LOGPALETTE* pLogPalette=(LOGPALETTE*)&pal;
- pLogPalette->palNumEntries=m_nNumColours;
- for(int i=0;i<m_nNumColours;i++)
- {
- pLogPalette->palPalEntry[i].peRed=GetRValue(m_crColours[i].crColour);
- pLogPalette->palPalEntry[i].peGreen=GetGValue(m_crColours[i].crColour);
- pLogPalette->palPalEntry[i].peBlue=GetBValue(m_crColours[i].crColour);
- pLogPalette->palPalEntry[i].peFlags=0;
- }
- //
- //以创建的逻辑板为模板来生成MFC的CPalette类
- m_Palette.CreatePalette(pLogPalette);
- }
- CColourPopup::CColourPopup(CPoint p, COLORREF crColour, CWnd *pParentWnd, UINT nID, LPCTSTR szCustomText/*=NULL*/)
- {
- Initialize();
- m_crColour=m_crInitialColour=crColour;
- if(szCustomText!=NULL)
- {
- m_bShowCustom=TRUE;
- m_strCustomText=szCustomText;
- }
- else
- m_bShowCustom=FALSE;
- m_pParent=pParentWnd;
- CColourPopup::Create(p,crColour,pParentWnd,nID,szCustomText);
- }
- BOOL CColourPopup::Create(CPoint p, COLORREF crColour, CWnd *pParentWnd, UINT nID, LPCTSTR szCustomText)
- {
- ASSERT(pParentWnd&&::IsWindow(pParentWnd->GetSafeHwnd()));
- // ASSERT(pParentWnd->IsKindOf(RUNTIME_CLASS(CColourPicker)));
- m_pParent=pParentWnd;
- m_crColour=m_crInitialColour=crColour;
-
- //创建 窗体并注册类名
- CString szClassName=AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|CS_VREDRAW,
- 0,(HBRUSH)GetStockObject(LTGRAY_BRUSH),0);
- if(!CWnd::CreateEx(0,szClassName,_T(""),WS_VISIBLE|WS_POPUP,p.x,p.y,100,100,
- pParentWnd->GetSafeHwnd(),0,NULL))
- return FALSE;
- if(szCustomText!=NULL)
- m_strCustomText=szCustomText;
- //设定窗体的尺寸
- SetWindowSize();
- //创建ToolTips显示的文字
- CreateToolTips();
- FindCellFromColour(crColour);
- SetCapture();
- return TRUE;
- }
- void CColourPopup::SetWindowSize()
- {
- CSize TextSize;
- //若显示用户文本区,则定义字体和文本尺寸
- if(m_bShowCustom)
- {
- //得到用户文本尺寸
- CClientDC dc(this);
- CFont* pOldFont=(CFont*)dc.SelectObject(&m_Font);
- TextSize=dc.GetTextExtent(m_strCustomText)+CSize(2*m_nMargin,2*m_nMargin);
- dc.SelectObject(pOldFont);
- //添加足够的空间来绘制水平线
- TextSize.cy+=2*m_nMargin+2;
- }
- m_nNumColumns=8;
- m_nNumRows=m_nNumColours/m_nNumColumns;
- if(m_nNumColours%m_nNumColumns) m_nNumRows++;
- //得到当前窗体位置,并设定新的尺寸
- CRect rect;
- GetWindowRect(rect);
- m_WindowRect.SetRect(rect.left,rect.top,rect.left+m_nNumColumns*m_nBoxSize+2*m_nMargin,
- rect.top+m_nNumRows*m_nBoxSize+2*m_nMargin);
- if(m_bShowCustom)
- {
- m_WindowRect.bottom+=(m_nMargin+TextSize.cy);
- if(TextSize.cx>m_WindowRect.Width())
- m_WindowRect.right=m_WindowRect.left+TextSize.cx;
- TextSize.cx=m_WindowRect.Width()-2*m_nMargin;
- m_TextRect.SetRect(m_nMargin,m_nMargin,
- m_nMargin+TextSize.cx,m_nMargin+TextSize.cy);
- }
- //检验是否超出了屏幕的显示范围
- CSize ScreenSize(::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN));
- //太靠右
- if(m_WindowRect.right>ScreenSize.cx)
- m_WindowRect.OffsetRect(-(m_WindowRect.right-ScreenSize.cx),0);
- //太靠左
- if(m_WindowRect.left<0)
- m_WindowRect.OffsetRect(-m_WindowRect.left,0);
- //底超出了屏幕
- if(m_WindowRect.bottom>ScreenSize.cy)
- {
- CRect ParentRect;
- m_pParent->GetWindowRect(ParentRect);
- m_WindowRect.OffsetRect(0,-(ParentRect.Height()+m_WindowRect.Height()));
- }
- MoveWindow(m_WindowRect,TRUE);
- }
- void CColourPopup::FindCellFromColour(COLORREF crColour)
- {
- for(int row=0;row<m_nNumRows;row++)
- for(int col=0;col<m_nNumColumns;col++)
- {
- if(GetColour(row,col)==crColour)
- {
- m_nChosenColourRow=row;
- m_nChosenColourCol=col;
- return;
- }
- }
- m_nChosenColourRow=TEXT_BOX_VALUE;
- m_nChosenColourCol=TEXT_BOX_VALUE;
- }
- void CColourPopup::CreateToolTips()
- {
- if(!m_ToolTip.Create(this)) return;
- for(int row=0;row<m_nNumRows;row++)
- for(int col=0;col<m_nNumColumns;col++)
- {
- CRect rect;
- if(!GetCellRect(row,col,rect)) continue;
- m_ToolTip.AddTool(this,GetColourName(row,col),rect,1);
- }
- }
- BOOL CColourPopup::GetCellRect(int row, int col, const LPRECT &rect)
- {
- if(row<0||row>=m_nNumRows||col<0||col>=m_nNumColumns)
- return FALSE;
- rect->left=col*m_nBoxSize+m_nMargin;
- rect->top=row*m_nBoxSize+m_nMargin;
- //当显示用户的文本时,将颜色板按钮部分下移
- if(m_bShowCustom)
- rect->top+=(m_nMargin+m_TextRect.Height());
- rect->right=rect->left+m_nBoxSize;
- rect->bottom=rect->top+m_nBoxSize;
- return TRUE;
- }
- void CColourPopup::OnPaint()
- {
- //创建绘制的CDC派生类
- CPaintDC dc(this); // device context for painting
- //绘制颜色单元
- // TODO: Add your message handler code here
- for(int row=0;row<m_nNumRows;row++)
- for(int col=0;col<m_nNumColumns;col++)
- DrawCell(&dc,row,col);
- //写用户文本
- if(m_bShowCustom)
- DrawCell(&dc,TEXT_BOX_VALUE,TEXT_BOX_VALUE);
- //显示出凸出的按钮效果
- CRect rect;
- GetClientRect(rect);
- dc.DrawEdge(rect,EDGE_RAISED,BF_RECT);
- // Do not call CWnd::OnPaint() for painting messages
- }
- void CColourPopup::DrawCell(CDC *pDC,int row,int col)
- {
- if(m_bShowCustom&&row==TEXT_BOX_VALUE)
- {
- CRect TextButtonRect=m_TextRect;
- TextButtonRect.bottom-=(2*m_nMargin+2);
- //背景
- pDC->FillSolidRect(m_TextRect,::GetSysColor(COLOR_3DFACE));
- //按钮
- if(m_nCurrentRow==row&&m_nCurrentCol==col)
- pDC->DrawEdge(TextButtonRect,EDGE_RAISED,BF_RECT);
- //文本
- CFont *pOldFont=(CFont*)pDC->SelectObject(&m_Font);
- pDC->DrawText(m_strCustomText,TextButtonRect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- pDC->SelectObject(pOldFont);
- //文本下的水平线
- pDC->FillSolidRect(m_TextRect.left+2*m_nMargin,m_TextRect.bottom-m_nMargin-2,
- m_TextRect.Width()+4*m_nMargin,1,::GetSysColor(COLOR_3DSHADOW));
- pDC->FillSolidRect(m_TextRect.left+2*m_nMargin,m_TextRect.bottom-m_nMargin-1,
- m_TextRect.Width()+4*m_nMargin,1,::GetSysColor(COLOR_3DHILIGHT));
- return;
- }
- ASSERT (row>=0&&row<m_nNumRows);
- ASSERT (col>=0&&col<m_nNumColumns);
- //颜色板
- CPalette *pOldPalette;
- if(pDC->GetDeviceCaps(RASTERCAPS)&&RC_PALETTE)
- {
- pOldPalette=pDC->SelectPalette(&m_Palette,FALSE);
- pDC->RealizePalette();
- }
- CRect rect;
- if(!GetCellRect(row,col,rect)) return;
- //填充背景颜色
- if((m_nChosenColourRow==row&&m_nChosenColourCol==col)&&
- !(m_nCurrentRow==row&&m_nCurrentCol==col))
- pDC->FillSolidRect(rect,::GetSysColor(COLOR_3DHILIGHT));
- else
- pDC->FillSolidRect(rect,::GetSysColor(COLOR_3DFACE));
- //各个按钮
- if(m_nChosenColourRow==row&&m_nChosenColourCol==col)
- pDC->DrawEdge(rect,EDGE_SUNKEN,BF_RECT);
- else if(m_nCurrentRow==row&&m_nCurrentCol==col)
- pDC->DrawEdge(rect,EDGE_RAISED,BF_RECT);
- CBrush brush(PALETTERGB(GetRValue(GetColour(row,col)),
- GetGValue(GetColour(row,col)),
- GetBValue(GetColour(row,col))));
- CPen pen;
- pen.CreatePen(PS_SOLID,1,::GetSysColor(COLOR_3DSHADOW));
- CBrush* pOldBrush=(CBrush*)pDC->SelectObject(&brush);
- CPen* pOldPen= (CPen*)pDC->SelectObject(&pen);
- //绘各板单元的颜色
- rect.DeflateRect(m_nMargin+1,m_nMargin+1);
- pDC->Rectangle(rect);
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- brush.DeleteObject();
- pen.DeleteObject();
- if(pDC->GetDeviceCaps(RASTERCAPS)&RC_PALETTE)
- pDC->SelectPalette(pOldPalette,FALSE);
- }
- void CColourPopup::EndSelection(int nMessage)
- {
- ReleaseCapture();
- //如果用户文本被选中,拾色器
- if(nMessage!=CPN_SELENDCANCEL&&m_nCurrentCol==TEXT_BOX_VALUE&&m_nCurrentRow==TEXT_BOX_VALUE)
- {
- CColorDialog dlg(m_crInitialColour,CC_FULLOPEN|CC_ANYCOLOR,this);
- if(dlg.DoModal()==IDOK)
- m_crColour=dlg.GetColor();
- else
- m_crColour=m_crInitialColour;
- }
- if(nMessage==CPN_SELENDCANCEL)
- m_crColour=m_crInitialColour;
- m_pParent->SendMessage(nMessage,(WPARAM)m_crColour,0);
- DestroyWindow();
- }
- void CColourPopup::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
-
- CWnd::OnLButtonUp(nFlags, point);
- DWORD pos=GetMessagePos();
- point=CPoint(LOWORD(pos),HIWORD(pos));
- if(m_WindowRect.PtInRect(point))
- EndSelection(CPN_SELENDOK);
- else
- EndSelection(CPN_SELENDCANCEL);
- }
- void CColourPopup::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- int row,col;
- point.x-=m_nMargin;
- point.y-=m_nMargin;
- //检验鼠标是否在相应的区域
- if(m_bShowCustom&&m_TextRect.PtInRect(point))
- row=col=TEXT_BOX_VALUE;
- else
- {
- if(m_bShowCustom)
- point.y-=m_TextRect.Height();
- row=point.y/m_nBoxSize,
- col=point.x/m_nBoxSize;
- if(row<0||row>=m_nNumRows||col<0||col>=m_nNumColumns)
- {
- CWnd::OnMouseMove(nFlags,point);
- return;
- }
- }
- //得到相应行列,调用ChangeSelection
- if(row!=m_nCurrentRow||col!=m_nCurrentCol)
- ChangeSelection(row,col);
- CWnd::OnMouseMove(nFlags, point);
- }
- void CColourPopup::ChangeSelection(int row, int col)
- {
- CClientDC dc(this);
- if((m_nCurrentRow>=0&&m_nCurrentRow<m_nNumRows&&
- m_nCurrentCol>=0&&m_nCurrentCol<m_nNumColumns)||
- (m_nCurrentCol==TEXT_BOX_VALUE)) //TEXT_BOX_VALUE=-2
- {
- int OldRow=m_nCurrentRow;
- int Oldcol=m_nCurrentCol;
- m_nCurrentRow=m_nCurrentCol=-1;
- DrawCell(&dc,OldRow,Oldcol);
- }
- //选中的单元
- m_nCurrentRow=row;m_nCurrentCol=col;
- DrawCell(&dc,m_nCurrentRow,m_nCurrentCol);
- //选 中的单元颜色保存
- if(m_nCurrentRow==TEXT_BOX_VALUE&&m_nCurrentCol==TEXT_BOX_VALUE)
- m_pParent->SendMessage(CPN_SELCHANGE,(WPARAM)m_crInitialColour,0);
- else
- {
- m_crColour=GetColour(m_nCurrentRow,m_nCurrentCol);
- m_pParent->SendMessage(CPN_SELCHANGE,(WPARAM)m_crColour,0);
- }
- }
- void CColourPopup::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- int row=m_nCurrentRow;
- int col=m_nCurrentCol;
-
- if(nChar==VK_DOWN)
- {
- if(row<0){row=0;col=0;}
- else if(row<m_nNumRows-1)row++;
- else {row=TEXT_BOX_VALUE;col=TEXT_BOX_VALUE;}
- ChangeSelection(row,col);
- }
- if(nChar==VK_UP)
- {
- if(row<0){row=m_nNumRows-1;col=0;}
- else if(row>0)row--;
- else {row=TEXT_BOX_VALUE;col=TEXT_BOX_VALUE;}
- ChangeSelection(row,col);
- }
- if(nChar==VK_RIGHT)
- {
- if(col<0){row=0;col=0;}
- else if(col<m_nNumColumns-1)col++;
- else col=0;
- ChangeSelection(row,col);
- }
- if(nChar==VK_LEFT)
- {
- if(col<0){row=m_nNumRows-1;col=m_nNumColumns-1;}
- else if(col>0)col--;
- else col=m_nNumRows;
- ChangeSelection(row,col);
- }
- if(nChar==VK_ESCAPE)
- {
- m_crColour=m_crInitialColour;
- EndSelection(CPN_SELENDCANCEL);
- return;
- }
- if(nChar==VK_RETURN)
- {
- EndSelection(CPN_SELENDOK);
- return;
- }
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- BOOL CColourPopup::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
- m_ToolTip.RelayEvent(pMsg);
- return CWnd::PreTranslateMessage(pMsg);
- }
- void CColourPopup::OnPaletteChanged(CWnd* pFocusWnd)
- {
- CWnd::OnPaletteChanged(pFocusWnd);
-
- // TODO: Add your message handler code here
- if(pFocusWnd->GetSafeHwnd()!=GetSafeHwnd())
- Invalidate();
- }
- BOOL CColourPopup::OnQueryNewPalette()
- {
- // TODO: Add your message handler code here and/or call default
- Invalidate();
- return CWnd::OnQueryNewPalette();
- }
- void CColourPopup::OnNcDestroy()
- {
- CWnd::OnNcDestroy();
-
- // TODO: Add your message handler code here
- delete this;
- }