void CColorPalette::DrawNewSelHighlight(int OldSel,int NewSel) { HDC hdc; hdc=GetDC(m_hwnd); if (OldSel>=0) DrawSelRect(hdc,OldSel,false); if (NewSel>=0) DrawSelRect(hdc,NewSel,true); ReleaseDC(m_hwnd,hdc); }
LRESULT CColorPalette::OnMessage(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_CREATE: m_SelColor=-1; m_HotColor=-1; m_Tooltip.Create(hwnd); return 0; case WM_SIZE: { int sx=LOWORD(lParam),sy=HIWORD(lParam); m_ItemWidth=max(sx/16,6); m_ItemHeight=max(sy/16,6); m_Left=(sx-m_ItemWidth*16)/2; m_Top=(sy-m_ItemHeight*16)/2; if (m_pPalette!=NULL) SetToolTip(); } return 0; case WM_PAINT: if (m_pPalette!=NULL) { PAINTSTRUCT ps; int x,y; int i; RECT rc; ::BeginPaint(hwnd,&ps); for (i=0;i<m_NumColors;i++) { x=i%16; y=i/16; rc.left=m_Left+x*m_ItemWidth+2; rc.top=m_Top+y*m_ItemHeight+2; rc.right=rc.left+m_ItemWidth-4; rc.bottom=rc.top+m_ItemHeight-4; if (rc.left<ps.rcPaint.right && rc.top<ps.rcPaint.bottom && rc.right>ps.rcPaint.left && rc.bottom>ps.rcPaint.top) { DrawUtil::Fill(ps.hdc,&rc, RGB(m_pPalette[i].rgbRed, m_pPalette[i].rgbGreen, m_pPalette[i].rgbBlue)); } } if (m_SelColor>=0) DrawSelRect(ps.hdc,m_SelColor,true); ::EndPaint(hwnd,&ps); return 0; } break; case WM_MOUSEMOVE: if (m_pPalette!=NULL) { POINT ptCursor; int Hot; ptCursor.x=GET_X_LPARAM(lParam); ptCursor.y=GET_Y_LPARAM(lParam); Hot=(ptCursor.y-m_Top)/m_ItemHeight*16+ (ptCursor.x-m_Left)/m_ItemWidth; if (ptCursor.x<m_Left || ptCursor.x>=m_Left+m_ItemWidth*16 || ptCursor.y<m_Top || ptCursor.y>=m_Top+m_ItemHeight*16 || Hot>=m_NumColors) Hot=-1; if (Hot==m_HotColor) return 0; m_HotColor=Hot; SendNotify(NOTIFY_HOTCHANGE); } return 0; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: if (m_pPalette!=NULL) { POINT ptCursor; int Sel; ptCursor.x=GET_X_LPARAM(lParam); ptCursor.y=GET_Y_LPARAM(lParam); Sel=(ptCursor.y-m_Top)/m_ItemHeight*16+ (ptCursor.x-m_Left)/m_ItemWidth; if (ptCursor.x<m_Left || ptCursor.x>=m_Left+m_ItemWidth*16 || ptCursor.y<m_Top || ptCursor.y>=m_Top+m_ItemHeight*16 || Sel>=m_NumColors || Sel==m_SelColor) return 0; DrawNewSelHighlight(m_SelColor,Sel); m_SelColor=Sel; SendNotify(NOTIFY_SELCHANGE); if (uMsg==WM_RBUTTONDOWN) SendNotify(NOTIFY_RBUTTONDOWN); } return 0; case WM_LBUTTONDBLCLK: if (m_SelColor>=0) SendNotify(NOTIFY_DOUBLECLICK); return 0; case WM_NOTIFY: switch (((LPNMHDR)lParam)->code) { case TTN_NEEDTEXT: { LPNMTTDISPINFO pttdi=reinterpret_cast<LPNMTTDISPINFO>(lParam); int Index=(int)pttdi->hdr.idFrom; pttdi->lpszText=pttdi->szText; pttdi->hinst=NULL; if (Index>=0 && Index<m_NumColors) { int r,g,b; r=m_pPalette[Index].rgbRed; g=m_pPalette[Index].rgbGreen; b=m_pPalette[Index].rgbBlue; ::wsprintf(pttdi->szText,TEXT("%d,%d,%d #%02X%02X%02X"),r,g,b,r,g,b); } else { pttdi->szText[0]='\0'; } } return 0; } break; case WM_DESTROY: m_Tooltip.Destroy(); return 0; } return ::DefWindowProc(hwnd,uMsg,wParam,lParam); }
void CUIMenu::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { LPDRAWITEMSTRUCT lpds; UI_MENU_ITEM_INFO * lpUIMenuItemInfo; HDC hDC; CRect rcItem; BOOL bGrayed, bDisabled, bSelected, bChecked; BOOL bIsSeparator; CImageEx * lpIconImgN, *lpIconImgH; HDC hMemDC; HBITMAP hMemBmp, hOldBmp; lpds = lpDrawItemStruct; if (NULL == lpds || lpds->CtlType != ODT_MENU) return; lpUIMenuItemInfo = (UI_MENU_ITEM_INFO *)lpds->itemData; if (NULL == lpUIMenuItemInfo || lpUIMenuItemInfo->nMagic != UI_MENU_MAGIC) return; hDC = lpds->hDC; rcItem = lpds->rcItem; bGrayed = lpds->itemState & ODS_GRAYED; bDisabled = lpds->itemState & ODS_DISABLED; bSelected = lpds->itemState & ODS_SELECTED; //bChecked = lpds->itemState & ODS_CHECKED; bChecked = lpUIMenuItemInfo->nState & ODS_CHECKED; bIsSeparator = lpUIMenuItemInfo->nType & MFT_SEPARATOR; lpIconImgN = lpUIMenuItemInfo->lpIconImgN; lpIconImgH = lpUIMenuItemInfo->lpIconImgH; rcItem.OffsetRect(-rcItem.left, -rcItem.top); hMemDC = ::CreateCompatibleDC(hDC); hMemBmp = ::CreateCompatibleBitmap(hDC, rcItem.Width(), rcItem.Height()); hOldBmp = (HBITMAP)::SelectObject(hMemDC, hMemBmp); DrawBg(hMemDC, rcItem); // 绘制背景 if (bIsSeparator) // 绘制分隔符 { DrawSepartor(hMemDC, rcItem); } else { if (bSelected && !bGrayed) DrawSelRect(hMemDC, rcItem); // 绘制选中区域 if (bChecked) // 绘制Check状态 { DrawCheckState(hMemDC, rcItem); } else { if (bSelected) DrawIcon(hMemDC, rcItem, lpIconImgH); // 绘制选中图标 else DrawIcon(hMemDC, rcItem, lpIconImgN); // 绘制非选中图标 } DrawText(hMemDC, rcItem, bGrayed, bSelected, (BOOL)lpUIMenuItemInfo->hSubMenu, lpUIMenuItemInfo->szText.c_str()); // 绘制菜单文本 if (lpUIMenuItemInfo->hSubMenu != NULL) // 绘制子菜单的右三角箭头 DrawArrow(hMemDC, rcItem); } rcItem = lpds->rcItem; ::BitBlt(hDC, rcItem.left, rcItem.top, rcItem.Width(), rcItem.Height(), hMemDC, 0, 0, SRCCOPY); if (lpUIMenuItemInfo->hSubMenu != NULL) // 不让系统绘制弹出式菜单箭头 ::ExcludeClipRect(hDC, rcItem.left, rcItem.top, rcItem.right, rcItem.bottom); ::SelectObject(hMemDC, hOldBmp); ::DeleteObject(hMemBmp); ::DeleteDC(hMemDC); }