I wanted to create a popup control which is similar to CXTPControlPopupColor. Note the small color bar at bottom of the CXTPControlPopupColor Icon could be updated by calling its SetColor(nColor) function. Now I wanted it add one more parameter, nWidth, to this function, so that the Color bar's thickness could be updated as well. To do this, I created a derived a class from CXTPControlPopup, the code is something like below.
// in MyControlPopupColor.h
class CMyControlPopupColor : public CXTPControlPopup {
...
public:
void SetColorWidth(COLORREF clr, int nWidth);
private:
void RedrawIcon(CXTPImageManagerIcon* pImage, CXTPImageManagerIconHandle* hIcon );
private:
COLORREF m_clr; int m_nWidth;
...
}
//in MyControlPopupColor.cpp
//SetColorWidth function
void CMyControlPopupColor::SetColorWidth(COLORREF clr, int nWidth) { if (clr != m_clr) { m_clr = clr;
if (nWidth < 1) m_nWidth = 1; else if(nWidth > 10) m_nWidth = 10; else m_nWidth = nWidth;
CXTPImageManagerIconSet* pIconSet = GetImageManager()->GetIconSet(GetIconId()); if (pIconSet) { pIconSet->RefreshAll();
CXTPImageManagerIconSet::CIconSetMap* pMap = pIconSet->GetIcons();
POSITION pos = pMap->GetStartPosition(); UINT nWidth; CXTPImageManagerIcon* pImage; while (pos != NULL) { pMap->GetNextAssoc(pos, nWidth, pImage);
CXTPImageManagerIconHandle hIcon; RedrawIcon(pImage, &hIcon);
if (!hIcon.IsEmpty()) { pImage->SetIcon(hIcon); } } RedrawParent(); } } }
//RedrawIcon function
void CMyControlPopupColor::RedrawIcon(CXTPImageManagerIcon* pImage, CXTPImageManagerIconHandle* pHandle) { CXTPImageManagerIconHandle& hIcon = pImage->GetIcon();
if (hIcon.IsEmpty()) return;
if (!hIcon.IsAlpha()) { ICONINFO info;
if (GetIconInfo(hIcon.GetIcon(), &info)) { { CXTPCompatibleDC dc(NULL, CBitmap::FromHandle(info.hbmColor)); CXTPCompatibleDC dcMask(NULL, CBitmap::FromHandle(info.hbmMask));
BITMAP bmp; ::GetObject(info.hbmColor, sizeof(BITMAP), &bmp); //int nHeight = int((double)bmp.bmHeight / 5);
int nHeight = int(1 + (bmp.bmHeight) / 30.0 * m_nWidth);
CRect rc(0, bmp.bmHeight - nHeight, bmp.bmWidth, bmp.bmHeight); dc.FillSolidRect(rc, m_clr); dcMask.FillSolidRect(rc, 1); }
*pHandle = CreateIconIndirect(&info);
::DeleteObject(info.hbmMask); ::DeleteObject(info.hbmColor); } } else { CDC dcSrc; dcSrc.CreateCompatibleDC(NULL);
PBYTE pBits = 0; PBITMAPINFO pBitmapInfo = 0;
pHandle->CopyHandle(hIcon);
TRY { UINT nSize; if (!CXTPImageManagerIcon::GetBitmapBits(dcSrc, pHandle->GetBitmap(), pBitmapInfo, (LPVOID&)pBits, nSize)) AfxThrowMemoryException();
DWORD dwColor = 0xFF000000 | CLR_TO_RGBQUAD(m_clr); int temp = int (1 + (pBitmapInfo->bmiHeader.biHeight) / 30.0 * m_nWidth);
//int nCount = pBitmapInfo->bmiHeader.biHeight / 5 * pBitmapInfo->bmiHeader.biWidth * 4; int nCount = temp * pBitmapInfo->bmiHeader.biWidth * 4;
for (int i = 0; i < nCount; i += 4) { *LPDWORD(&pBits) = dwColor; }
SetDIBits(dcSrc, pHandle->GetBitmap(), 0, pBitmapInfo->bmiHeader.biHeight, pBits, pBitmapInfo, DIB_RGB_COLORS);
} CATCH (CMemoryException, e) { TRACE(_T("Failed -- Memory exception thrown.")); } END_CATCH
free(pBits); free(pBitmapInfo);
} }
//The SetColorWidth(.) function and RedrawIcon(.) functions are slightly modified versions of that in the CXTPControlPopupColor class. I have highlighted my modified code in red.
With the derived class, I found I can change Color bar's width now but there is a problem.
If I call SetColorWidth function twice, first time with thicker width and and second time with a thinner width, e.g.
first time,
pColorControl->SetColorWidth(COLORRED, 10);
second time,
pColorControl->SetColorWidth(COLORRED, 2);
after the second call, the color bar thichness does not reduced to 2. The reason is color bar drawn first time is not removed during the 2nd to the function.
To solve this problem, I have tried to restore the original toolbar control's Icon by calling
pColorControl->Reset();
pColorControl->SetIconId(Orignal_Icon_ID);
before the second call to SetColorWidth(.) function. However, this does not work. I cannot remove previously drawn color bar on the Icon.
Could anybody help by giving some suggestions on how the above code should be modified to achieve the purpose? I would like to use Alpha toolbar icons in my application, hence I don't want the modified control Icon appear in different style from other icons on the Toolbar.
------------- Sharing makes life better
|