📄 colourpopup.cpp
字号:
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -