📄 colorbtn.cpp
字号:
CColorBtnDlg::used[0]= 1;
CColorBtnDlg::used[1]= 3;
CColorBtnDlg::used[2]= 5;
CColorBtnDlg::used[3]= 7;
CColorBtnDlg::used[4]= 9;
CColorBtnDlg::used[5]= 11;
CColorBtnDlg::used[6]= 13;
CColorBtnDlg::used[7]= 15;
CColorBtnDlg::used[8]= 17;
CColorBtnDlg::used[9]= 19;
CColorBtnDlg::used[10]= 20;
CColorBtnDlg::used[11]= 18;
CColorBtnDlg::used[12]= 16;
CColorBtnDlg::used[13]= 14;
CColorBtnDlg::used[14]= 12;
CColorBtnDlg::used[15]= 10;
CColorBtnDlg::used[16]= 8;
CColorBtnDlg::used[17]= 6;
CColorBtnDlg::used[18]= 4;
CColorBtnDlg::used[19]= 2;
}
void CColorBtn::Serialize( CArchive& ar )
{
if (ar.IsStoring())
{
ar.Write((void *)CColorBtnDlg::colors,sizeof(COLORREF)*20);
ar.Write((void *)CColorBtnDlg::used,sizeof(BYTE)*20);
}
else
{
ar.Read((void *)CColorBtnDlg::colors,sizeof(COLORREF)*20);
ar.Read((void *)CColorBtnDlg::used,sizeof(BYTE)*20);
}
}
/////////////////////////////////////////////////////////////////////////////
// CColorBtnDlg dialog
CColorBtnDlg::CColorBtnDlg(CWnd* pParent /*=NULL*/)
: CDialog(CColorBtnDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CColorBtnDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
colorindex = -1;
}
void CColorBtnDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CColorBtnDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CColorBtnDlg, CDialog)
//{{AFX_MSG_MAP(CColorBtnDlg)
ON_BN_CLICKED(IDC_OTHER, OnOther)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_DRAWITEM()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(IDC_COLOR1,IDC_COLOR20,OnColor)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorBtnDlg message handlers
BOOL CColorBtnDlg::OnInitDialog()
{
CDialog::OnInitDialog();
RECT r,r2;
parent->GetWindowRect(&r);
// Move the dialog to be below the button
SetWindowPos(NULL,r.left,r.bottom,0,0,SWP_NOSIZE|SWP_NOZORDER);
GetWindowRect(&r2);
// Check to see if the dialog has a portion outside the
// screen, if so, adjust.
if (r2.bottom > GetSystemMetrics(SM_CYSCREEN))
{
r2.top = r.top-(r2.bottom-r2.top);
}
if (r2.right > GetSystemMetrics(SM_CXSCREEN))
{
r2.left = GetSystemMetrics(SM_CXSCREEN) - (r2.right-r2.left);
}
SetWindowPos(NULL,r2.left,r2.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
// Capture the mouse, this allows the dialog to close when
// the user clicks outside.
// Remember that the dialog has no "close" button.
SetCapture();
if (colorindex != -1)
{
CWnd *pCtl = GetDlgItem(IDC_COLOR1 + colorindex);
if (pCtl)
{
pCtl->SetFocus();
return FALSE;
}
}
return TRUE;
}
void CColorBtnDlg::EndDialog( int nResult ){
ReleaseCapture();
CDialog::EndDialog(nResult);}
void CColorBtnDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
RECT r;
POINT p;
p.x = point.x;
p.y = point.y;
ClientToScreen(&p);
GetWindowRect(&r);
// The user clicked...
if (!PtInRect(&r,p))
{
// ...outside the dialog, close.
EndDialog(IDCANCEL);
}
else
{
// ...inside the dialog. Since this window
// has the mouse captured, its children
// get no messages. So, check to see
// if the click was in one of its children
// and tell him.
// If the user clicks inside the dialog
// but not on any of the controls,
// ChildWindowFromPoint returns a
// pointer to the dialog. In this
// case we do not resend the message
// (obviously) because it would cause
// a stack overflow.
CWnd *child = ChildWindowFromPoint(point);
if (child && child != this)
child->SendMessage(WM_LBUTTONDOWN,0,0l);
}
CDialog::OnLButtonDown(nFlags, point);
}
void CColorBtnDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpd)
{
CDC dc;
CPen nullpen;
CBrush brush;
CPen *oldpen;
CBrush *oldbrush;
// Draw the wells using the current color table
nullpen.CreateStockObject(NULL_PEN);
brush.CreateSolidBrush(colors[nIDCtl-IDC_COLOR1]);
dc.Attach(lpd->hDC);
oldpen = dc.SelectObject(&nullpen);
oldbrush = dc.SelectObject(&brush);
lpd->rcItem.right++;
lpd->rcItem.bottom++;
dc.Rectangle(&lpd->rcItem);
if (lpd->itemState & ODS_FOCUS)
{
CRect rcFocus(lpd->rcItem);
rcFocus.DeflateRect(1, 1);
dc.DrawFocusRect(rcFocus);
}
dc.SelectObject(oldpen);
dc.SelectObject(oldbrush);
dc.Detach();
CDialog::OnDrawItem(nIDCtl, lpd);
}
void CColorBtnDlg::OnColor(UINT id){
// A well has been clicked, set the color index
// and close.
colorindex = id-IDC_COLOR1;
int i;
// This color is now the MRU
for (i=0;i<20;i++)
{
if (used[colorindex] > used[i])
{
used[i]++;
}
}
used[colorindex] = 1;
EndDialog(IDOK);}
void CColorBtnDlg::OnOther()
{
int i;
COLORREF newcolor;
// The "Other" button.
ReleaseCapture();
CColorDialog dlg;
dlg.m_cc.Flags |= CC_FULLOPEN;
if (dlg.DoModal() == IDOK)
{
// The user clicked OK.
// set the color and close
newcolor = dlg.GetColor();
// Check to see if the selected color is
// already in the table.
colorindex = -1;
for (i=0;i<20;i++)
{
if (colors[i] == newcolor)
{
colorindex = i;
break;
}
}
// If the color was not found,
// replace the LRU with this color
if (colorindex == -1)
{
for (i=0;i<20;i++)
{
if (used[i] == 20)
{
colors[i] = newcolor;
colorindex = i;
break;
}
}
}
// This is the new MRU
for (i=0;i<20;i++)
{
if (used[colorindex] > used[i])
{
used[i]++;
}
}
used[colorindex] = 1;
EndDialog(IDOK);
return;
}
// If the user clicked "Cancel" reclaim the mouse capture.
SetCapture();
}
void CColorBtnDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// See notes for OnLButtonDown.
CWnd *child = ChildWindowFromPoint(point,CWP_ALL);
if (child && child != this)
child->SendMessage(WM_LBUTTONDOWN,0,0l);
CDialog::OnLButtonUp(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -