void CClosableTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CRect rect = lpDrawItemStruct->rcItem; int nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; BOOL bSelected = (nTabIndex == GetCurSel()); TCHAR szLabel[256]; TC_ITEM tci; tci.mask = TCIF_TEXT | TCIF_IMAGE | TCIF_STATE; tci.pszText = szLabel; tci.cchTextMax = ARRSIZE(szLabel); tci.dwStateMask = TCIS_HIGHLIGHTED; if (!GetItem(nTabIndex, &tci)) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (!pDC) return; int iOldBkMode = pDC->SetBkMode(TRANSPARENT); // Draw image on left side CImageList* piml = GetImageList(); if (tci.iImage >= 0 && piml && piml->m_hImageList) { IMAGEINFO ii; piml->GetImageInfo(0, &ii); rect.left += bSelected ? 8 : 4; piml->Draw(pDC, tci.iImage, CPoint(rect.left, rect.top + 2), ILD_TRANSPARENT); rect.left += (ii.rcImage.right - ii.rcImage.left); if (!bSelected) rect.left += 4; } bool bCloseable = m_bCloseable; if (bCloseable && GetParent()->SendMessage(UM_QUERYTAB, nTabIndex)) bCloseable = false; // Draw 'Close button' at right side if (bCloseable && m_ImgLstCloseButton.m_hImageList) { CRect rcCloseButton; GetCloseButtonRect(rect, rcCloseButton); m_ImgLstCloseButton.Draw(pDC, 0, rcCloseButton.TopLeft(), ILD_TRANSPARENT); rect.right = rcCloseButton.left - 2; } COLORREF crOldColor = RGB(0, 0, 0); if (tci.dwState & TCIS_HIGHLIGHTED) crOldColor = pDC->SetTextColor(RGB(192, 0, 0)); rect.top += 4; pDC->DrawText(szLabel, rect, DT_SINGLELINE | DT_TOP | DT_CENTER | DT_NOPREFIX); if (tci.dwState & TCIS_HIGHLIGHTED) pDC->SetTextColor(crOldColor); pDC->SetBkMode(iOldBkMode); }
VOID CEasySkinTreeCtrl::DrawListImage( CDC * pDC, CRect rcRect, HTREEITEM hTreeItem, bool bSelected ) { CMapItemInfo::iterator iter= m_MapItemInfo.find(hTreeItem); if( iter == m_MapItemInfo.end() ) { CImageList *pImageList = GetImageList(TVSIL_NORMAL); if( pImageList == NULL ) return; //获取属性 INT nImage,nSelectedImage; GetItemImage(hTreeItem,nImage,nSelectedImage); //获取信息 IMAGEINFO ImageInfo; pImageList->GetImageInfo(bSelected?nSelectedImage:nImage,&ImageInfo); //绘画图标 INT nImageWidth=ImageInfo.rcImage.right-ImageInfo.rcImage.left; INT nImageHeight=ImageInfo.rcImage.bottom-ImageInfo.rcImage.top; pImageList->Draw(pDC,bSelected?nSelectedImage:nImage,CPoint(rcRect.left-nImageWidth-3,rcRect.top+(rcRect.Height()-nImageHeight)/2+1),ILD_TRANSPARENT); } else { CImageEx *pImage = iter->second.pImage; pImage->DrawImage(pDC,rcRect.left-pImage->GetWidth()-3,rcRect.top+(rcRect.Height()-pImage->GetHeight())/2+1); } }
void CXTPSkinObjectTab::DrawTabIcon(CDC* pDC, CRect& rcItem, int iItem) { CTabCtrl* pTabCtrl = (CTabCtrl*)this; TC_ITEM tci; tci.mask = TCIF_IMAGE; BOOL bResult = pTabCtrl->GetItem(iItem, &tci); #ifdef _UNICODE if (!bResult) { TC_ITEMA tcia; ::ZeroMemory(&tcia, sizeof(TC_ITEMA)); tcia.mask = TCIF_IMAGE; bResult = (BOOL)::SendMessage(pTabCtrl->m_hWnd, TCM_GETITEMA, iItem, (LPARAM)&tcia); tci.iImage = tcia.iImage; } #endif if (!bResult) return; CImageList* pImageList = pTabCtrl->GetImageList(); if (!pImageList || tci.iImage < 0) return; CPoint point = rcItem.TopLeft(); DWORD dwStyle = GetStyle(); if (IsHorz(dwStyle)) { point.Offset(GetMetrics()->m_cxEdge * 2, IsTop(dwStyle) ? GetMetrics()->m_cyEdge : 1); } else { point.Offset(IsLeft(dwStyle) ? GetMetrics()->m_cxEdge : 1, GetMetrics()->m_cyEdge * 2); } // Draw any associated icons. pImageList->Draw(pDC, tci.iImage, point, ILD_TRANSPARENT); IMAGEINFO info; ::ZeroMemory(&info, sizeof(info)); pImageList->GetImageInfo(tci.iImage, &info); CRect rcImage(info.rcImage); if (IsHorz(dwStyle)) { rcItem.left += rcImage.Width() + GetMetrics()->m_cxEdge; } else { rcItem.top += rcImage.Height() + GetMetrics()->m_cyEdge; } }
void CTabCtrlDoc::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CRect rect = lpDrawItemStruct->rcItem; int nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; BOOL bSelected = (nTabIndex == GetCurSel()); WCHAR label[64]; TC_ITEM tci; tci.mask = TCIF_TEXT|TCIF_IMAGE; tci.pszText = label; tci.cchTextMax = 63; if (!GetItem(nTabIndex, &tci)) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (!pDC) return; int nSavedDC = pDC->SaveDC(); // For some bizarre reason the rcItem you get extends above the actual // drawing area. We have to workaround this "feature". rect.top += ::GetSystemMetrics(SM_CYEDGE); pDC->SetBkMode(TRANSPARENT); pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE)); // Draw image CImageList* pImageList = GetImageList(); if (pImageList && tci.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin // Get height of image so we IMAGEINFO info; pImageList->GetImageInfo(tci.iImage, &info); CRect ImageRect(info.rcImage); int nYpos = rect.top; pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT); rect.left += ImageRect.Width(); } if (bSelected) { pDC->SetTextColor(m_crSelColour); pDC->SelectObject(&m_SelFont); rect.top -= ::GetSystemMetrics(SM_CYEDGE); pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); } else { pDC->SetTextColor(m_crUnselColour); pDC->SelectObject(&m_UnselFont); pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER); } pDC->RestoreDC(nSavedDC); }
LRESULT CSHeaderCtrl::OnSetImageList(WPARAM, LPARAM lParam) { CImageList* pImageList; pImageList = CImageList::FromHandle((HIMAGELIST)lParam); IMAGEINFO info; if (pImageList->GetImageInfo(0, &info)) { m_sizeImage.cx = info.rcImage.right - info.rcImage.left; m_sizeImage.cy = info.rcImage.bottom - info.rcImage.top; } return Default(); }
/************************************************************************ getBitmapFromImagelist 이미지 리스트로 부터 단일 오브젝트를 추출한다. @param : @return : @remark : http://www.codeproject.com/bitmap/getimagefromlist.asp @author : youngchang ([email protected]) @history: 2005/12/16:CREATED ************************************************************************/ void CFishBMPManager::GetBMPfromList(CDC* pDC, CImageList *lstImages, int nImage, CBitmap* destBitmap) { destBitmap->DeleteObject(); CImageList tmpList; tmpList.Create(lstImages); tmpList.Copy( 0, nImage, ILCF_SWAP ); IMAGEINFO lastImage; tmpList.GetImageInfo(0,&lastImage); CDC dcMem; dcMem.CreateCompatibleDC (pDC); CRect rect (lastImage.rcImage); destBitmap->CreateCompatibleBitmap (pDC, rect.Width (), rect.Height ()); CBitmap* pBmpOld = dcMem.SelectObject (destBitmap); tmpList.DrawIndirect (&dcMem, 0, CPoint (0, 0), CSize (rect.Width (), rect.Height ()), CPoint (0, 0)); dcMem.SelectObject (pBmpOld); }
// @pymethod iiii(iiii)|PyCImageList|GetImageInfo|Retrieves information about an image. PyObject *PyCImageList_GetImageInfo( PyObject *self, PyObject *args ) { CImageList *pList; int nIndex; if (!(pList=PyCImageList::GetImageList(self))) return NULL; // @pyparm int|index||Index of image. if (!PyArg_ParseTuple(args, "i:GetImageInfo", &nIndex)) return NULL; IMAGEINFO info; GUI_BGN_SAVE; BOOL ok = pList->GetImageInfo(nIndex, &info); GUI_END_SAVE; if (!ok) RETURN_ERR("GetImageInfo failed"); return Py_BuildValue("iiii(iiii)", info.hbmImage, info.hbmMask, info.Unused1, info.Unused2, info.rcImage.left,info.rcImage.top,info.rcImage.right,info.rcImage.bottom ); // @rdesc The return info is a tuple describing an IMAGELIST structure. }
void CCommonAppUtils::ResizeAllListCtrlCols(CListCtrl * pListCtrl) { int maxcol = ((CHeaderCtrl*)(pListCtrl->GetDlgItem(0)))->GetItemCount()-1; int nItemCount = pListCtrl->GetItemCount(); TCHAR textbuf[MAX_PATH] = { 0 }; CHeaderCtrl * pHdrCtrl = (CHeaderCtrl*)(pListCtrl->GetDlgItem(0)); if (pHdrCtrl) { int imgWidth = 0; CImageList * pImgList = pListCtrl->GetImageList(LVSIL_SMALL); if ((pImgList)&&(pImgList->GetImageCount())) { IMAGEINFO imginfo; pImgList->GetImageInfo(0, &imginfo); imgWidth = (imginfo.rcImage.right - imginfo.rcImage.left) + 3; // 3 pixels between icon and text } for (int col = 0; col <= maxcol; col++) { HDITEM hdi = {0}; hdi.mask = HDI_TEXT; hdi.pszText = textbuf; hdi.cchTextMax = _countof(textbuf); pHdrCtrl->GetItem(col, &hdi); int cx = pListCtrl->GetStringWidth(hdi.pszText)+20; // 20 pixels for col separator and margin for (int index = 0; index<nItemCount; ++index) { // get the width of the string and add 14 pixels for the column separator and margins int linewidth = pListCtrl->GetStringWidth(pListCtrl->GetItemText(index, col)) + 14; // add the image size if (col == 0) linewidth += imgWidth; if (cx < linewidth) cx = linewidth; } pListCtrl->SetColumnWidth(col, cx); } } }
HBITMAP CEmoticonRichEditCtrl::GetImage(CImageList& list, int num) { CBitmap dist; CClientDC dc(NULL); IMAGEINFO ii; list.GetImageInfo(num, &ii); int nWidth = ii.rcImage.right - ii.rcImage.left; int nHeight = ii.rcImage.bottom - ii.rcImage.top; dist.CreateCompatibleBitmap(&dc, nWidth, nHeight); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap* pOldBitmap = memDC.SelectObject(&dist); memDC.FillSolidRect(0, 0, nWidth, nHeight, SetBackgroundColor(TRUE, 0)); list.Draw(&memDC, num, CPoint(0, 0), ILD_NORMAL); memDC.SelectObject(pOldBitmap); return (HBITMAP)dist.Detach(); }
void CClosableTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDIS) { CRect rect(lpDIS->rcItem); int nTabIndex = lpDIS->itemID; if (nTabIndex < 0) return; TCHAR szLabel[256]; TC_ITEM tci; tci.mask = TCIF_TEXT | TCIF_IMAGE | TCIF_STATE; tci.pszText = szLabel; tci.cchTextMax = _countof(szLabel); tci.dwStateMask = TCIS_HIGHLIGHTED; if (!GetItem(nTabIndex, &tci)) return; //TRACE("CClosableTabCtrl::DrawItem: item=%u, state=%08x, color=%08x, rc=%3d,%3d,%3dx%3d\n", nTabIndex, tci.dwState, GetTextColor(lpDIS->hDC), lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right - lpDIS->rcItem.left, lpDIS->rcItem.bottom - lpDIS->rcItem.top); CDC* pDC = CDC::FromHandle(lpDIS->hDC); if (!pDC) return; CRect rcFullItem(lpDIS->rcItem); bool bSelected = (lpDIS->itemState & ODS_SELECTED) != 0; /////////////////////////////////////////////////////////////////////////////////////// // Adding support for XP Styles (Vista Themes) for owner drawn tab controls simply // does *not* work under Vista. Maybe it works under XP (did not try), but that is // meaningless because under XP a owner drawn tab control is already rendered *with* // the proper XP Styles. So, for XP there is no need to care about the theme API at all. // // However, under Vista, a tab control which has the TCS_OWNERDRAWFIXED // style gets additional 3D-borders which are applied by Vista *after* WM_DRAWITEM // was processed. Thus, there is no known workaround available to prevent Vista from // adding those old fashioned 3D-borders. We can render the tab control items within // the WM_DRAWITEM handler in whatever style we want, but Vista will in each case // overwrite the borders of each tab control item with old fashioned 3D-borders... // // To complete this experience, tab controls also do not support NMCUSTOMDRAW. So, the // only known way to customize a tab control is by using TCS_OWNERDRAWFIXED which does // however not work properly under Vista. // // The "solution" which is currently implemented to prevent Vista from drawing those // 3D-borders is by using "ExcludeClipRect" to reduce the drawing area which is used // by Windows after WM_DRAWITEM was processed. This "solution" is very sensitive to // the used rectangles and offsets in general. Incrementing/Decrementing one of the // "rcItem", "rcFullItem", etc. rectangles makes the entire "solution" flawed again // because some borders would become visible again. // HTHEME hTheme = NULL; int iPartId = TABP_TABITEM; int iStateId = TIS_NORMAL; bool bVistaHotTracked = false; bool bVistaThemeActive = theApp.IsVistaThemeActive(); if (bVistaThemeActive) { // To determine if the current item is in 'hot tracking' mode, we need to evaluate // the current foreground color - there is no flag which would indicate this state // more safely. This applies only for Vista and for tab controls which have the // TCS_OWNERDRAWFIXED style. bVistaHotTracked = pDC->GetTextColor() == GetSysColor(COLOR_HOTLIGHT); hTheme = g_xpStyle.OpenThemeData(m_hWnd, L"TAB"); if (hTheme) { if (bSelected) { // get the real tab item rect rcFullItem.left += 1; rcFullItem.right -= 1; rcFullItem.bottom -= 1; } else rcFullItem.InflateRect(2, 2); // get the real tab item rect CRect rcBk(rcFullItem); if (bSelected) { iStateId = TTIS_SELECTED; if (nTabIndex == 0) { // First item if (nTabIndex == GetItemCount() - 1) iPartId = TABP_TOPTABITEMBOTHEDGE; // First & Last item else iPartId = TABP_TOPTABITEMLEFTEDGE; } else if (nTabIndex == GetItemCount() - 1) { // Last item iPartId = TABP_TOPTABITEMRIGHTEDGE; } else { iPartId = TABP_TOPTABITEM; } } else { rcBk.top += 2; iStateId = bVistaHotTracked ? TIS_HOT : TIS_NORMAL; if (nTabIndex == 0) { // First item if (nTabIndex == GetItemCount() - 1) iPartId = TABP_TABITEMBOTHEDGE; // First & Last item else iPartId = TABP_TABITEMLEFTEDGE; } else if (nTabIndex == GetItemCount() - 1) { // Last item iPartId = TABP_TABITEMRIGHTEDGE; } else { iPartId = TABP_TABITEM; } } if (g_xpStyle.IsThemeBackgroundPartiallyTransparent(hTheme, iPartId, iStateId)) g_xpStyle.DrawThemeParentBackground(m_hWnd, *pDC, &rcFullItem); g_xpStyle.DrawThemeBackground(hTheme, *pDC, iPartId, iStateId, &rcBk, NULL); } } // Following background clearing is needed for: // WinXP/Vista (when used without an application theme) // Vista (when used with an application theme but without a theme for the tab control) if ( (!g_xpStyle.IsThemeActive() || !g_xpStyle.IsAppThemed()) || (hTheme == NULL && bVistaThemeActive) ) pDC->FillSolidRect(&lpDIS->rcItem, GetSysColor(COLOR_BTNFACE)); int iOldBkMode = pDC->SetBkMode(TRANSPARENT); // Draw image on left side CImageList *piml = GetImageList(); if (tci.iImage >= 0 && piml && piml->m_hImageList) { IMAGEINFO ii; piml->GetImageInfo(0, &ii); rect.left += bSelected ? 8 : 4; piml->Draw(pDC, tci.iImage, CPoint(rect.left, rect.top + 2), ILD_TRANSPARENT); rect.left += (ii.rcImage.right - ii.rcImage.left); if (!bSelected) rect.left += 4; } bool bCloseable = m_bCloseable; if (bCloseable && GetParent()->SendMessage(UM_QUERYTAB, nTabIndex)) bCloseable = false; // Draw 'Close button' at right side if (bCloseable && m_ImgLstCloseButton.m_hImageList) { CRect rcCloseButton; GetCloseButtonRect(nTabIndex, rect, rcCloseButton, bSelected, bVistaThemeActive); HTHEME hThemeNC = bVistaThemeActive ? g_xpStyle.OpenThemeData(m_hWnd, _T("WINDOW")) : NULL; if (hThemeNC) { // Possible "Close" parts: WP_CLOSEBUTTON, WP_SMALLCLOSEBUTTON, WP_MDICLOSEBUTTON int iPartId = WP_SMALLCLOSEBUTTON; int iStateId = (bSelected || bVistaHotTracked) ? CBS_NORMAL : CBS_DISABLED; if (g_xpStyle.IsThemeBackgroundPartiallyTransparent(hTheme, iPartId, iStateId)) g_xpStyle.DrawThemeParentBackground(m_hWnd, *pDC, &rcCloseButton); g_xpStyle.DrawThemeBackground(hThemeNC, *pDC, iPartId, iStateId, rcCloseButton, NULL); g_xpStyle.CloseThemeData(hThemeNC); } else { m_ImgLstCloseButton.Draw(pDC, (bSelected || bVistaHotTracked) ? 0 : 1, rcCloseButton.TopLeft(), ILD_TRANSPARENT); } rect.right = rcCloseButton.left - 2; if (bSelected) rect.left += hTheme ? 4 : 2; } COLORREF crOldColor = CLR_NONE; if (tci.dwState & TCIS_HIGHLIGHTED) crOldColor = pDC->SetTextColor(RGB(192, 0, 0)); else if (bVistaHotTracked) crOldColor = pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT)); rect.top += bSelected ? 4 : 3; // Vista: Tab control has troubles with determining the width of a tab if the // label contains one '&' character. To get around this, we use the old code which // replaces one '&' character with two '&' characters and we do not specify DT_NOPREFIX // here when drawing the text. // // Vista: "DrawThemeText" can not be used in case we need a certain foreground color. Thus we always us // "DrawText" to always get the same font and metrics (just for safety). pDC->DrawText(szLabel, rect, DT_SINGLELINE | DT_TOP | DT_CENTER /*| DT_NOPREFIX*/); if (crOldColor != CLR_NONE) pDC->SetTextColor(crOldColor); pDC->SetBkMode(iOldBkMode); if (hTheme) { CRect rcClip(rcFullItem); if (bSelected) { rcClip.left -= 2 + 1; rcClip.right += 2 + 1; } else { rcClip.top += 2; } pDC->ExcludeClipRect(&rcClip); g_xpStyle.CloseThemeData(hTheme); } }
//------------------------------------------------------------------------ //! Appends the checkbox state images to the list control image list //! //! @param owner The list control adding column //! @param imagelist The image list assigned to the list control //! @return Image index where the two state images (unchecked/checked) was inserted //------------------------------------------------------------------------ int CGridColumnTraitImage::AppendStateImages(CGridListCtrlEx& owner, CImageList& imagelist) { if (!(owner.GetExtendedStyle() & LVS_EX_SUBITEMIMAGES)) owner.SetExtendedStyle(owner.GetExtendedStyle() | LVS_EX_SUBITEMIMAGES); if (!imagelist) imagelist.Create(16, 16, ILC_COLOR16 | ILC_MASK, 1, 0); if (!owner.GetImageList(LVSIL_SMALL)) owner.SetImageList(&imagelist, LVSIL_SMALL); VERIFY(owner.GetImageList(LVSIL_SMALL) == &imagelist); bool createdStateImages = false; CImageList* pStateList = owner.GetImageList(LVSIL_STATE); if (pStateList == NULL) { if (!(owner.GetExtendedStyle() & LVS_EX_CHECKBOXES)) { createdStateImages = true; owner.SetExtendedStyle(owner.GetExtendedStyle() | LVS_EX_CHECKBOXES); pStateList = owner.GetImageList(LVSIL_STATE); } } int imageCount = -1; ASSERT(pStateList != NULL); if (pStateList != NULL && pStateList->GetImageCount() >= 2) { imageCount = imagelist.GetImageCount(); // Get the icon size of current imagelist CSize iconSize(16, 16); if (imageCount > 0) { IMAGEINFO iconSizeInfo = { 0 }; VERIFY(imagelist.GetImageInfo(0, &iconSizeInfo)); iconSize = CSize(iconSizeInfo.rcImage.right - iconSizeInfo.rcImage.left, iconSizeInfo.rcImage.bottom - iconSizeInfo.rcImage.top); } // Scale the icon-position if necessary CPoint iconPos(1, 0); // +1 pixel to avoid overlap with left-grid-line { IMAGEINFO stateSizeInfo = { 0 }; VERIFY(pStateList->GetImageInfo(0, &stateSizeInfo)); int stateIconHeight = stateSizeInfo.rcImage.bottom - stateSizeInfo.rcImage.top; if (iconSize.cy > stateIconHeight) iconPos.y = (iconSize.cy - stateIconHeight) / 2; } // Redraw the state-icon to match the icon size of the current imagelist (without scaling image) CClientDC clienDC(&owner); CDC memDC; VERIFY(memDC.CreateCompatibleDC(&clienDC)); CBitmap dstBmp; VERIFY(dstBmp.CreateCompatibleBitmap(&clienDC, iconSize.cx, iconSize.cy)); CBitmap* pBmpOld = memDC.SelectObject(&dstBmp); COLORREF oldBkColor = pStateList->SetBkColor(imagelist.GetBkColor()); CBrush brush(imagelist.GetBkColor()); memDC.FillRect(CRect(0, 0, iconSize.cx, iconSize.cy), &brush); VERIFY(pStateList->Draw(&memDC, 0, iconPos, ILD_NORMAL)); memDC.SelectObject(pBmpOld); VERIFY(imagelist.Add(&dstBmp, oldBkColor) != -1); pBmpOld = memDC.SelectObject(&dstBmp); memDC.FillRect(CRect(0, 0, iconSize.cx, iconSize.cy), &brush); VERIFY(pStateList->Draw(&memDC, 1, iconPos, ILD_NORMAL)); memDC.SelectObject(pBmpOld); VERIFY(imagelist.Add(&dstBmp, oldBkColor) != -1); pStateList->SetBkColor(oldBkColor); } if (createdStateImages) owner.SetExtendedStyle(owner.GetExtendedStyle() & ~LVS_EX_CHECKBOXES); return imageCount; }
void CXTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CRect rect = lpDrawItemStruct->rcItem; rect.bottom+=2; rect.top += ::GetSystemMetrics(SM_CYEDGE); int nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; BOOL bSelected = (nTabIndex == GetCurSel()); COLORREF crSelected = m_bColorSelected ? m_crSelected : GetSysColor(COLOR_BTNTEXT); COLORREF crNormal = m_bColorNormal ? m_crNormal : GetSysColor(COLOR_BTNTEXT); COLORREF crDisabled = m_bColorDisabled ? m_crDisabled : GetSysColor(COLOR_GRAYTEXT); TCHAR label[64]; TC_ITEM item; item.mask = TCIF_TEXT|TCIF_IMAGE; item.pszText = label; item.cchTextMax = 63; if (!GetItem(nTabIndex, &item)) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (!pDC) return; int nSavedDC = pDC->SaveDC(); CRect rectItem; POINT pt; GetItemRect(nTabIndex, &rectItem); GetCursorPos(&pt); ScreenToClient(&pt); if (rectItem.PtInRect(pt)) m_iIndexMouseOver = nTabIndex; pDC->SetBkMode(TRANSPARENT); pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE)); //** Draw the image CImageList* pImageList = GetImageList(); if (pImageList && item.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; IMAGEINFO info; pImageList->GetImageInfo(item.iImage, &info); CRect ImageRect(info.rcImage); int nYpos = rect.top; pImageList->Draw(pDC, item.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT); rect.left += ImageRect.Width(); } if (!IsTabEnabled(nTabIndex)) { pDC->SetTextColor(crDisabled); rect.top -= ::GetSystemMetrics(SM_CYEDGE); pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); } else { //** selected item ----- if (bSelected) pDC->SetTextColor(crSelected); else //** other item --- { if (m_bColorMouseOver && nTabIndex == m_iIndexMouseOver) { pDC->SetTextColor(m_crMouseOver); } else { pDC->SetTextColor(crNormal); } } rect.top -= ::GetSystemMetrics(SM_CYEDGE); pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); } pDC->RestoreDC(nSavedDC); }
void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDraw) {try{ int cCols = GetHeaderCtrl ()->GetItemCount (); CDC *dc = CDC::FromHandle (lpDraw->hDC); int xStart = lpDraw->rcItem.left; CImageList *pImages = GetImageList (LVSIL_SMALL); BOOL bNeedBorder = FALSE; CHeaderCtrl* pHdr = GetHeaderCtrl (); for (int i = 0; i < cCols; i++) { LVITEM item; TCHAR szItem [10000]; int colWidth = GetColumnWidth (pHdr->OrderToIndex (i)); xStart += 5; colWidth -= 5; item.iItem = lpDraw->itemID; item.iSubItem = i; item.pszText = szItem; item.cchTextMax = sizeof (szItem); item.mask = LVIF_IMAGE | LVIF_TEXT; GetItem (&item); if (i == 0) { item.state = GetItemState (lpDraw->itemID, LVIS_SELECTED|LVIS_FOCUSED); COLORREF clrBg = m_vInfo [lpDraw->itemID].clrBg; COLORREF clrText = m_vInfo [lpDraw->itemID].clrText; if (item.state & LVIS_FOCUSED) bNeedBorder = TRUE; if (item.state & LVIS_SELECTED) { clrBg = GetSysColor (COLOR_HIGHLIGHT); clrText = GetSysColor (COLOR_HIGHLIGHTTEXT); if (bNeedBorder == FALSE) { if (GetSelectionMark () == (int)lpDraw->itemID) bNeedBorder = TRUE; } } CBrush br; CPen pen; CBrush *oldBr; CPen *oldPen; if (RGBIsEqual (clrBg, clrText)) clrText = (~clrText) & 0x00FFFFFF; dc->SetTextColor (clrText); br.CreateSolidBrush (clrBg); pen.CreatePen (PS_SOLID, 1, clrBg); oldBr = dc->SelectObject (&br); oldPen = dc->SelectObject (&pen); dc->Rectangle (&lpDraw->rcItem); if (m_bGrid) { CPen pen1 (PS_SOLID, 1, m_clrGrid); dc->SelectObject (&pen1); dc->MoveTo (lpDraw->rcItem.left, lpDraw->rcItem.bottom-1); dc->LineTo (lpDraw->rcItem.right, lpDraw->rcItem.bottom-1); dc->SelectObject (oldPen); } dc->SelectObject (oldBr); dc->SelectObject (oldPen); if (pImages) { CPoint pt; pt.x = xStart; pt.y = lpDraw->rcItem.top; if (m_pSelImages && (item.state & LVIS_SELECTED)) m_pSelImages->Draw (dc, item.iImage, pt, ILD_TRANSPARENT); else pImages->Draw (dc, item.iImage, pt, ILD_TRANSPARENT); IMAGEINFO inf; pImages->GetImageInfo (item.iImage, &inf); xStart += inf.rcImage.right - inf.rcImage.left + 5; colWidth -= inf.rcImage.right - inf.rcImage.left + 5; } } if (*item.pszText) { int needX = GetStringWidth (item.pszText); BOOL bDrawText = TRUE; RECT rcText = lpDraw->rcItem; rcText.left = xStart; rcText.right = xStart + colWidth - 5; if (needX > colWidth-5) { RECT rc = rcText; int dx = GetStringWidth ("..."); if (dx <= colWidth-5) { rc.left = rc.right - dx; dc->DrawText ("...", &rc, DT_SINGLELINE | DT_LEFT | DT_VCENTER); rcText.right = rc.left - 5; } else bDrawText = FALSE; } if (bDrawText) dc->DrawText (szItem, &rcText, DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_NOPREFIX); } xStart += colWidth; if (m_bGrid) { CPen pen (PS_SOLID, 1, m_clrGrid); CPen *oldPen = dc->SelectObject (&pen); dc->MoveTo (xStart-1, lpDraw->rcItem.top); dc->LineTo (xStart-1, lpDraw->rcItem.bottom); dc->SelectObject (oldPen); } } if (bNeedBorder) { dc->SetTextColor (m_vInfo [lpDraw->itemID].clrText); RECT rc = lpDraw->rcItem; rc.bottom--; rc.right--; dc->DrawFocusRect (&rc); } }catch (...){} }
// Based on code by Frank Kobs. CImageList* CDragDropListCtrl::CreateDragImageEx(LPPOINT lpPoint) { CRect rectSingle; CRect rectComplete(0, 0, 0, 0); int nIndex = -1; BOOL bFirst = TRUE; // Determine the size of the drag image. POSITION pos = GetFirstSelectedItemPosition(); while (pos) { nIndex = GetNextSelectedItem(pos); GetItemRect(nIndex, rectSingle, LVIR_BOUNDS); if (bFirst) { // Initialize the CompleteRect GetItemRect(nIndex, rectComplete, LVIR_BOUNDS); bFirst = FALSE; } rectComplete.UnionRect(rectComplete, rectSingle); } // Create bitmap in memory DC CClientDC dcClient(this); CDC dcMem; CBitmap Bitmap; if (!dcMem.CreateCompatibleDC(&dcClient)) { return NULL; } if (!Bitmap.CreateCompatibleBitmap(&dcClient, rectComplete.Width(), rectComplete.Height())) { return NULL; } CBitmap* pOldMemDCBitmap = dcMem.SelectObject(&Bitmap); // Here green is used as mask color. dcMem.FillSolidRect(0, 0, rectComplete.Width(), rectComplete.Height(), RGB(0, 255, 0)); // Paint each DragImage in the DC. CImageList* pSingleImageList = NULL; CPoint pt; pos = GetFirstSelectedItemPosition(); while (pos) { nIndex = GetNextSelectedItem(pos); GetItemRect(nIndex, rectSingle, LVIR_BOUNDS); pSingleImageList = CreateDragImage(nIndex, &pt); if (pSingleImageList) { // Make sure width takes into account not using LVS_EX_FULLROWSELECT style. IMAGEINFO ImageInfo; pSingleImageList->GetImageInfo(0, &ImageInfo); rectSingle.right = rectSingle.left + (ImageInfo.rcImage.right - ImageInfo.rcImage.left); pSingleImageList->DrawIndirect( &dcMem, 0, CPoint(rectSingle.left - rectComplete.left, rectSingle.top - rectComplete.top), rectSingle.Size(), CPoint(0,0)); delete pSingleImageList; } } dcMem.SelectObject(pOldMemDCBitmap); // Create the imagelist with the merged drag images. CImageList* pCompleteImageList = new CImageList; pCompleteImageList->Create(rectComplete.Width(), rectComplete.Height(), ILC_COLOR | ILC_MASK, 0, 1); // Here green is used as mask color. pCompleteImageList->Add(&Bitmap, RGB(0, 255, 0)); Bitmap.DeleteObject(); // As an optional service: // Find the offset of the current mouse cursor to the imagelist // this we can use in BeginDrag(). if (lpPoint) { CPoint ptCursor; GetCursorPos(&ptCursor); ScreenToClient(&ptCursor); lpPoint->x = ptCursor.x - rectComplete.left; lpPoint->y = ptCursor.y - rectComplete.top; } return pCompleteImageList; }
void CTabCtrlEx::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ) { if(lpDrawItemStruct->CtlType == ODT_TAB) { CRect rect = lpDrawItemStruct->rcItem; INT nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; TCHAR label[64]; TC_ITEM tci; tci.mask = TCIF_TEXT|TCIF_IMAGE; tci.pszText = label; tci.cchTextMax = 63; GetItem(nTabIndex, &tci ); CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (!pDC) return; int nSavedDC = pDC->SaveDC(); //填充背景色 COLORREF rcBack; if (lpDrawItemStruct->itemState & CDIS_SELECTED ) rcBack = RGB(255, 255,255); else if(lpDrawItemStruct->itemState & (CDIS_DISABLED | CDIS_GRAYED) ) rcBack = RGB(0, 255, 0); else rcBack = GetSysColor(COLOR_BTNFACE); pDC->FillSolidRect(rect, rcBack); rect.top += ::GetSystemMetrics(SM_CYEDGE); pDC->SetBkMode(TRANSPARENT); //绘制图片 CImageList *pImageList = GetImageList(); if (pImageList && tci.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin // Get height of image so we IMAGEINFO info; pImageList->GetImageInfo(tci.iImage, &info); CRect ImageRect(info.rcImage); INT nYpos = rect.top; pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT); rect.left += ImageRect.Width(); } //绘制字体 COLORREF txtColor; if (lpDrawItemStruct->itemState & CDIS_SELECTED ) { rect.top -= ::GetSystemMetrics(SM_CYEDGE); txtColor = RGB(0,0,255); } else if(lpDrawItemStruct->itemState & (CDIS_DISABLED | CDIS_GRAYED) ) txtColor = RGB(128, 128, 128); else txtColor = GetSysColor(COLOR_WINDOWTEXT); pDC->SetTextColor(txtColor); int r=rect.right; rect.right-=15; pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); rect.right=r; rect.left=rect.right-17; rect.top+=3; rect.bottom-=6; pDC->DrawFrameControl(&rect,DFCS_CAPTIONCLOSE,DFCS_BUTTONPUSH); pDC->RestoreDC(nSavedDC); } }
BOOL CSelectChan::OnChanListNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { NM_LISTVIEW *pNm = (NM_LISTVIEW *)lParam; LV_DISPINFO *ptvd= (LV_DISPINFO *)lParam; NMHDR *pnmhdr=(LPNMHDR )lParam; UINT state; DWORD dwpos; LV_HITTESTINFO lvhti; int iItemClicked; switch (pnmhdr->code) { case NM_CLICK: case NM_RCLICK: { CImageList *himl = m_chan.GetImageList(LVSIL_STATE); int cxImage = 0, cyImage = 0; if (himl) { IMAGEINFO im; himl->GetImageInfo(0,&im); cxImage=im.rcImage.right; cyImage=im.rcImage.bottom; } // Find out where the cursor was dwpos = GetMessagePos(); lvhti.pt.x = LOWORD(dwpos); lvhti.pt.y = HIWORD(dwpos); m_chan.ScreenToClient(&lvhti.pt); if(lvhti.pt.x>cxImage) break; // Now do a hittest with this point. iItemClicked = m_chan.HitTest(&lvhti); if (lvhti.flags & LVHT_ONITEMSTATEICON) { // Now lets get the state from the item and toggle it. state = m_chan.GetItemState(iItemClicked,LVIS_STATEIMAGEMASK); state = (state == 0x1000) ? 0x2000 : 0x1000; /* { LV_ITEM lvi; int i,j,n; union { struct LISTINDX lst; LPARAM lParam; } LL; memset(&lvi,0,sizeof(lvi)); lvi.mask=LVIF_PARAM; lvi.iItem=iItemClicked; lvi.iSubItem=0; PhasesList.GetItem(&lvi); LL.lParam=lvi.lParam; i=LL.lst.sta; j=LL.lst.chan; n=LL.lst.phase; SEISMIC_PHASE *pPhase; pPhase=allWSGPhases.GetPhase(i,j,n);*/ CString Chan=m_chan.GetItemText(iItemClicked,0); if(state==0x2000) { SetChanDrawState(ActiveSta, Chan, TRUE); } else { SetChanDrawState(ActiveSta, Chan, FALSE); } } m_chan.SetItemState(iItemClicked, state, LVIS_STATEIMAGEMASK); SetModified(TRUE); } } return CPropertyPage::OnNotify(wParam, lParam, pResult); }