📄 colorpicker.cpp
字号:
HDC hDC;
hDC = ::GetWindowDC(hwndPoint);
::GetWindowRect(hwndPoint,&rect);
crtTMPColor = GetPixel(hDC,point.x-rect.left,point.y-rect.top);
::ReleaseDC(hwndPoint,hDC);
RECT redrawRect;
redrawRect.left = 0;
redrawRect.right = 150;
redrawRect.top = 120;
redrawRect.bottom = 145;
::GetWindowRect(hWnd,&rect);
if(!rect.PtInRect(point))//不在本窗口
{
crtColor = crtTMPColor;
::InvalidateRect(hWnd,&redrawRect,TRUE);
}
else
{
rect.bottom = rect.top + 120;
int iCurrentActiveRect;
if(rect.PtInRect(point))///判断鼠标是否在当前窗口的色彩区域
{
////计算位于哪个方框内////
point.x -= rect.left;
point.y -= rect.top;
if(point.x%10 != 0 && point.y%10 != 0)
iCurrentActiveRect = point.y/10*19 + point.x/10;
else
iCurrentActiveRect = -1;
}
else
{
iCurrentActiveRect = -1;
crtColor = oldColor;
if(hwndColorBtn == hwndPoint)
::ReleaseCapture();
else
bInCtrlArea = TRUE;
}
if(iCurrentActiveRect != iActiveRect)
{
///清除原焦点
CRect ActiveRect;
ActiveRect.left = iActiveRect%19*10;
ActiveRect.top = iActiveRect/19*10;
ActiveRect.right = ActiveRect.left + 10;
ActiveRect.bottom = ActiveRect.top + 10;
::InvalidateRect(hWnd,&ActiveRect,TRUE);
///新焦点
if(iCurrentActiveRect != -1)
{
crtColor = crtTMPColor;
ActiveRect.left = iCurrentActiveRect%19*10;
ActiveRect.top = iCurrentActiveRect/19*10;
ActiveRect.right = ActiveRect.left + 10;
ActiveRect.bottom = ActiveRect.top + 10;
::InvalidateRect(hWnd,&ActiveRect,TRUE);
}
::InvalidateRect(hWnd,&redrawRect,TRUE);
iActiveRect = iCurrentActiveRect;
}
}
}
}
if(bInCtrlArea)
::PostMessage(hwndParent,WM_SETCURSOR,NULL,MAKELPARAM(1,0));
else
::PostMessage(hwndParent,WM_SETCURSOR,NULL,NULL);
}
break;
case WM_ACTIVATE:
if(LOWORD(wParam) != WA_INACTIVE)
break;
case WM_LBUTTONUP:
point.x = LPARAM_X(lParam);
point.y = LPARAM_Y(lParam);
if(bMouseMoved || ( point.x == oldPoint.x && point.y == oldPoint.y ))
::PostMessage(hwndParent,WM_LBUTTONUP,(UINT)crtColor,lParam);
break;
case WM_LBUTTONDOWN:
oldPoint.x = LPARAM_X(lParam);
oldPoint.y = LPARAM_Y(lParam);
bMouseMoved = FALSE;
::InvalidateRect(hWnd,&crtColorRect,TRUE);
break;
case WM_ERASEBKGND:
{
hDC = (HDC)wParam;
HPEN oldPen = (HPEN)::SelectObject(hDC,_penB);
for(int i=0;i<18;i++)
{
::MoveToEx(hDC,9+i*10,0,NULL);
::LineTo(hDC,9+i*10,120);
}
for(i=0;i<12;i++)
{
::MoveToEx(hDC,0,9+i*10,NULL);
::LineTo(hDC,190,9+i*10);
}
CRect btmRect(0,120,191,150);
::FillRect(hDC,btmRect,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
::Rectangle(hDC,crtColorRect.left-1,crtColorRect.top-1,crtColorRect.right + 1,crtColorRect.bottom + 1);
::SelectObject(hDC,oldPen);
}
break;
case WM_PAINT:
{
hDC = ::BeginPaint(hWnd,&ps);
CDC dc;
dc.Attach(hDC);
CRect rect;
int x1,y1,i,j,k;
UCHAR R=255,G=255,B=255;
for(i=0;i<12;i++)
{
y1 = i*10;
rect.SetRect(0,y1,9,y1+9);
if(i == 10)
R=G=B=0x17;
dc.FillSolidRect(rect,RGB(R,G,B));
R -= 0x17;
G -= 0x17;
B -= 0x17;
}
R = G = B = 0;
for(k = 0;k < 2;k++)
for(j = 0;j < 18;j++)
for(i = 0;i < 6;i++)
{
x1 = 10+j*10;
y1 = k*60+i*10;
rect.SetRect(x1,y1,x1+9,y1+9);
dc.FillSolidRect(rect,RGB(R,G,B));
if(B == 0xff)
{
B = 0x00;
if(G == 0xff)
{
G = 0x00;
R += 0x33;
}
else
G += 0x33;
}
else
B += 0x33;
}
if(iActiveRect != -1)
{
CRect ActiveRect;
ActiveRect.left = iActiveRect%19*10;
ActiveRect.top = iActiveRect/19*10;
ActiveRect.right = ActiveRect.left + 10;
ActiveRect.bottom = ActiveRect.top + 10;
dc.DrawFocusRect(ActiveRect);
}
rect.SetRect(0,y1+130,60,y1+140);
dc.FillSolidRect(crtColorRect,crtColor);
char strColor[8]="#";
sprintf(strColor+1,"%02X%02X%02X",GetRValue(crtColor),GetGValue(crtColor),GetBValue(crtColor));
HFONT hOldFont = (HFONT)dc.SelectObject(hFont);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(80,127,strColor,7);
dc.SelectObject(hOldFont);
dc.Detach();
::EndPaint(hWnd,&ps);
}
break;
default:
return(::DefWindowProc(hWnd,nMsg,wParam,lParam));
}
return NULL;
}
BOOL CColorPicker::CreatePaletteWindow()
{
if(!m_bPaletteWndActive)
{
// 创建调色板子窗口
WNDCLASS wndcls;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = PaletteWndProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = ::AfxGetInstanceHandle();
wndcls.hIcon = NULL;
wndcls.hCursor = NULL;
wndcls.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = "ColorPalette";
if (!::RegisterClass(&wndcls))
AfxThrowResourceException();
HDC hDC = ::GetDC(m_hWnd);
int scrWidth = ::GetDeviceCaps(hDC,HORZRES);
int scrHeight = ::GetDeviceCaps(hDC,VERTRES);
::ReleaseDC(m_hWnd,hDC);
CRect rect;
GetWindowRect(rect);
rect.top = rect.bottom - 1;
if(rect.left > scrWidth-191)
rect.left = scrWidth-191;
else if(rect.left < 0)
rect.left = 0;
if(rect.top > scrHeight-150)
rect.top = rect.top-150;
rect.bottom = rect.top + 150;
rect.right = rect.left + 191;
if(!(m_hPaletteWnd = ::CreateWindowEx(WS_EX_TOPMOST,"ColorPalette","Palatte",WS_POPUP|WS_BORDER|WS_VISIBLE,rect.left,rect.top,rect.Width(),rect.Height(),m_hWnd,NULL,wndcls.hInstance,NULL)))
return FALSE;
}
::PostMessage(m_hPaletteWnd,WM_SETCOLOR,(WPARAM)m_CurrentColor,NULL);
::ShowWindow(m_hPaletteWnd,SW_SHOW);
::SetCapture(m_hPaletteWnd);
m_bPaletteWndActive = TRUE;
return TRUE;
}
void CColorPicker::DestroyPaletteWindow()
{
::DestroyWindow(m_hPaletteWnd);
::UnregisterClass("ColorPalette",::AfxGetInstanceHandle());
m_bPaletteWndActive = FALSE;
m_hPaletteWnd = NULL;
}
BOOL CColorPicker::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if(m_bPaletteWndActive && m_hCursorStraw && nHitTest != 1)
::SetCursor(m_hCursorStraw);
else
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return TRUE;
}
void CColorPicker::GetColor(CString& strColor)
{
char cColor[8]="#";
sprintf(cColor+1,"%02X%02X%02X",GetRValue(m_CurrentColor),GetGValue(m_CurrentColor),GetBValue(m_CurrentColor));
strColor = cColor;
}
COLORREF CColorPicker::GetColor()
{
return m_CurrentColor;
}
LRESULT CColorPicker::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_COLORDLG)
{
DestroyPaletteWindow();
CColorDialog dlg;
dlg.m_cc.Flags |= CC_FULLOPEN;
if(dlg.DoModal() == IDOK)
SetColor(dlg.GetColor());
return TRUE;
}
else
return CButton::DefWindowProc(message, wParam, lParam);
}
void CColorPicker::SetBuddy(HWND hWnd)
{
m_hwndBuddy = hWnd;
}
void CColorPicker::SetColor(COLORREF ref)
{
m_CurrentColor = ref;
::PostMessage(GetParent()->m_hWnd,WM_COMMAND,MAKELPARAM(::GetWindowLong(m_hWnd,GWL_ID),BN_CLICKED),(LPARAM)m_hWnd);
if(m_hwndBuddy)
{
CString strColor;
GetColor(strColor);
::SetWindowText(m_hwndBuddy,strColor);
}
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -