📄 toolbarex.cpp
字号:
LPCTSTR lpszResourceName=MAKEINTRESOURCE(nIDResource);
ASSERT_VALID(this);
ASSERT(lpszResourceName != NULL);
// determine location of the bitmap in resource fork
HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR);
HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
if (hRsrc == NULL)
return FALSE;
CBitmap tmp;
BITMAP BitMap;
tmp.LoadBitmap(lpszResourceName);
if(tmp.GetBitmap(&BitMap)==0)
return FALSE;
HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if (hGlobal == NULL)
return FALSE;
CToolBarData* pData = (CToolBarData*)LockResource(hGlobal);
if (pData == NULL)
return FALSE;
ASSERT(pData->wVersion == 1);
m_pbtButtonStyle=new BYTE[pData->wItemCount];
UINT* pItems = new UINT[pData->wItemCount];
for (int i = 0; i < pData->wItemCount; i++)
pItems[i] = pData->items()[i];
BOOL bResult = SetButtons(pItems, pData->wItemCount);
delete[] pItems;
m_nButtonCount=pData->wItemCount;
for(int j = 0; j <pData->wItemCount; j++)
{
if(TBBS_SEPARATOR==GetButtonStyle(j))
{
m_nButtonCount--;
m_pbtButtonStyle[j]=1;
}
else
m_pbtButtonStyle[j]=0;
}
m_nSeparatorCount=pData->wItemCount-m_nButtonCount;
pData->wWidth=BitMap.bmWidth/m_nButtonCount;
pData->wHeight=(UINT)BitMap.bmHeight;
m_nButtonWidth=BitMap.bmWidth/m_nButtonCount;
m_nButtonHeight=pData->wHeight;
if (bResult)
{
// set new sizes of the buttons
CSize sizeImage(pData->wWidth, pData->wHeight);
CSize sizeButton(pData->wWidth + 7, pData->wHeight + 7);
m_ButtonSize=sizeButton;
SetSizes(sizeButton, sizeImage);
// load bitmap now that sizes are known by the toolbar control
bResult = LoadBitmap(lpszResourceName);
}
UnlockResource(hGlobal);
FreeResource(hGlobal);
return bResult;
}
BOOL CToolBarEx::LoadBitmap(LPCTSTR lpszResourceName)
{
ASSERT_VALID(this);
ASSERT(lpszResourceName != NULL);
// determine location of the bitmap in resource fork
HINSTANCE hInstImageWell = AfxFindResourceHandle(lpszResourceName, RT_BITMAP);
HRSRC hRsrcImageWell = ::FindResource(hInstImageWell, lpszResourceName, RT_BITMAP);
if (hRsrcImageWell == NULL)
return FALSE;
HGLOBAL hglb;
if ((hglb = LoadResource(hInstImageWell, hRsrcImageWell)) == NULL)
return FALSE;
LPBITMAPINFOHEADER lpBitmap = (LPBITMAPINFOHEADER)LockResource(hglb);
if (lpBitmap == NULL)
return NULL;
m_wToolBarBitCount=lpBitmap->biBitCount;
::FreeResource(hglb);
//if(m_wToolBarBitCount<8)//如果工具栏颜色小于等于256色则调用原函数
//{
// return CToolBar::LoadBitmap(lpszResourceName);
//}
m_lpszResourceName=lpszResourceName;
SetButtonEx(lpszResourceName);
return TRUE;
}
BOOL CToolBarEx::SetButtonEx(UINT nIDResource)
{
LPCTSTR lpszResourceName=MAKEINTRESOURCE(nIDResource);
return SetButtonEx(lpszResourceName);
}
BOOL CToolBarEx::SetButtonEx(LPCTSTR lpszResourceName)
{
CClientDC rdc(this);
CDC dc1,dc2,dc3,dc4;
CBitmap bmp1,bmp2,bmp3,bmp4,bmpd;
COLORREF pixelcolor,maskcolor;
BITMAP bitmap;
BYTE gray=0;
CRect rc4;
rc4.top=rc4.left=0;
if (m_dwStyle & CBRS_ORIENT_HORZ)
{
rc4.right=m_ButtonSize.cx*m_nButtonCount+6*m_nSeparatorCount;
rc4.bottom=m_ButtonSize.cy;
}
else
{
rc4.right=m_ButtonSize.cx;
rc4.bottom=m_ButtonSize.cy*m_nButtonCount+6*m_nSeparatorCount;
}
bmp1.LoadBitmap(lpszResourceName);
bmp1.GetBitmap(&bitmap);
bmp2.CreateBitmap(bitmap.bmWidth,bitmap.bmHeight,1,bitmap.bmBitsPixel,NULL);
bmp3.CreateBitmap(bitmap.bmWidth,bitmap.bmHeight,1,bitmap.bmBitsPixel,NULL);
bmp4.CreateBitmap(rc4.right,rc4.bottom,1,bitmap.bmBitsPixel,NULL);
bmpd.CreateBitmap(bitmap.bmWidth,bitmap.bmHeight,1,bitmap.bmBitsPixel,NULL);
dc1.CreateCompatibleDC(&rdc);
dc2.CreateCompatibleDC(&rdc);
dc3.CreateCompatibleDC(&rdc);
dc4.CreateCompatibleDC(&rdc);
dc1.SelectObject(&bmp1);
dc2.SelectObject(&bmp2);
dc3.SelectObject(&bmp3);
dc4.SelectObject(&bmp4);
dc4.FillRect(&rc4,&m_bkbrush);
maskcolor=dc1.GetPixel(0,0);
CRect rc2;
rc2.left=rc2.top=0;
rc2.right=bitmap.bmWidth;
rc2.bottom=bitmap.bmHeight;
///////////////////////////////////////////////////////////////////
//dc2.FillRect(&rc2,&m_bkbrush);
int totalbuttons=m_nButtonCount+m_nSeparatorCount;
int buttonindex=0;
int btnindex=0;
for(buttonindex=0,btnindex=0;buttonindex<totalbuttons;buttonindex++)
{
if(0==m_pbtButtonStyle[buttonindex])
{
if (m_dwStyle & CBRS_ORIENT_HORZ)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,btnindex*m_ButtonSize.cx+6*(buttonindex-btnindex)+3,3,SRCCOPY);
else if(m_dwStyle & CBRS_ORIENT_VERT)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,3,btnindex*m_ButtonSize.cy+6*(buttonindex-btnindex)+3,SRCCOPY);
//dc2.BitBlt(0,btnindex*m_nButtonHeight,m_nButtonWidth,m_nButtonHeight,&dc4,3,3,SRCCOPY);
btnindex++;
}
}
TransparentBltEx(dc2.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,
dc1.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,maskcolor);
LPBYTE lpbts;
WORD ncount=0;
ncount=(WORD)(bitmap.bmHeight*bitmap.bmWidthBytes);
lpbts=new BYTE[ncount];
bmp2.GetBitmapBits(ncount,lpbts);
bmpd.SetBitmapBits(ncount,lpbts);
m_imglstNormal.DeleteImageList();
m_imglstNormal.Create(m_nButtonWidth, m_nButtonHeight, bitmap.bmBitsPixel|ILC_MASK, 1, 1);
m_imglstNormal.Add(&bmpd, maskcolor);
GetToolBarCtrl().SetImageList(&m_imglstNormal);
/////////////////////////////////////////////////////////////////////
//dc3.FillRect(&rc2,&m_bkbrush);
dc3.BitBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,&dc1,0,0,SRCCOPY);
for(int l1=0;l1<bitmap.bmWidth;l1++)
{
for(int c1=0;c1<bitmap.bmHeight;c1++)
{
pixelcolor=dc3.GetPixel(l1,c1);
//gray=(BYTE)(GetRValue(pixelcolor)*0.2+GetGValue(pixelcolor)*0.4+GetBValue(pixelcolor)*0.2);
if(pixelcolor!=maskcolor)
dc3.SetPixel(l1,c1,RGB(250,250,250));
}
}
for(buttonindex=0,btnindex=0;buttonindex<totalbuttons;buttonindex++)
{
if(0==m_pbtButtonStyle[buttonindex])
{
if (m_dwStyle & CBRS_ORIENT_HORZ)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,btnindex*m_ButtonSize.cx+6*(buttonindex-btnindex)+3,3,SRCCOPY);
else if(m_dwStyle & CBRS_ORIENT_VERT)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,3,btnindex*m_ButtonSize.cy+6*(buttonindex-btnindex)+3,SRCCOPY);
//dc2.BitBlt(0,btnindex*m_nButtonHeight,m_nButtonWidth,m_nButtonHeight,&dc4,3,3,SRCCOPY);
btnindex++;
}
}
TransparentBltEx(dc2.m_hDC,1,1,bitmap.bmWidth,bitmap.bmHeight,
dc3.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,maskcolor);
dc3.BitBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,&dc1,0,0,SRCCOPY);
for(int l=0;l<bitmap.bmWidth;l++)
{
for(int c=0;c<bitmap.bmHeight;c++)
{
pixelcolor=dc3.GetPixel(l,c);
gray=(BYTE)(GetRValue(pixelcolor)*0.2+GetGValue(pixelcolor)*0.4+GetBValue(pixelcolor)*0.2);
if(pixelcolor!=maskcolor)
dc3.SetPixel(l,c,RGB(gray,gray,gray));
}
}
TransparentBltEx(dc2.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,
dc3.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,maskcolor);
bmp2.GetBitmapBits(ncount,lpbts);
bmpd.SetBitmapBits(ncount,lpbts);
m_imglstDisabled.DeleteImageList();
m_imglstDisabled.Create(m_nButtonWidth, m_nButtonHeight, bitmap.bmBitsPixel|ILC_MASK, 1, 1);
m_imglstDisabled.Add(&bmpd, maskcolor);
GetToolBarCtrl().SetDisabledImageList(&m_imglstDisabled);
/////////////////////////////////////////////////////////////////////
dc3.BitBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,&dc1,0,0,SRCCOPY);
for(int l3=0;l3<bitmap.bmWidth;l3++)
{
for(int c3=0;c3<bitmap.bmHeight;c3++)
{
pixelcolor=dc3.GetPixel(l3,c3);
if(pixelcolor!=maskcolor)
dc3.SetPixel(l3,c3,\
RGB(\
GetRValue(pixelcolor)>240?255:GetRValue(pixelcolor)+15,\
GetGValue(pixelcolor)>200?255:GetGValue(pixelcolor)+55,\
GetBValue(pixelcolor)>240?255:GetBValue(pixelcolor)+15
));
}
}
for(buttonindex=0,btnindex=0;buttonindex<totalbuttons;buttonindex++)
{
if(0==m_pbtButtonStyle[buttonindex])
{
if (m_dwStyle & CBRS_ORIENT_HORZ)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,btnindex*m_ButtonSize.cx+6*(buttonindex-btnindex)+3,3,SRCCOPY);
else if(m_dwStyle & CBRS_ORIENT_VERT)
dc2.BitBlt(btnindex*m_nButtonWidth,0,m_nButtonWidth,m_nButtonHeight,&dc4,3,btnindex*m_ButtonSize.cy+6*(buttonindex-btnindex)+3,SRCCOPY);
//dc2.BitBlt(0,btnindex*m_nButtonHeight,m_nButtonWidth,m_nButtonHeight,&dc4,3,3,SRCCOPY);
btnindex++;
}
}
TransparentBltEx(dc2.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,\
dc3.m_hDC,0,0,bitmap.bmWidth,bitmap.bmHeight,maskcolor);
bmp2.GetBitmapBits(ncount,lpbts);
bmpd.SetBitmapBits(ncount,lpbts);
m_imglstHot.DeleteImageList();
m_imglstHot.Create(m_nButtonWidth, m_nButtonHeight, bitmap.bmBitsPixel|ILC_MASK, 1, 1);
m_imglstHot.Add(&bmpd, maskcolor);
GetToolBarCtrl().SetHotImageList(&m_imglstHot);
CRect rt;
GetToolBarCtrl().GetClientRect(&rt);
TBBUTTONINFO btinfo;
int wx=0;
for(UINT k=0;k<m_nButtonCount;k++)
{
GetToolBarCtrl().GetButtonInfo(k,&btinfo);
wx+=btinfo.cx;
}
delete[] lpbts;
return TRUE;
}
void CToolBarEx::MouseHover(POINT pt)
{
if (m_dwStyle & CBRS_FLOATING) {
// no grippers
} else if (m_dwStyle & CBRS_ORIENT_HORZ) {
// gripper at left
CRect rc;
GetWindowRect(&rc);
rc.right=rc.left+8;
if(rc.PtInRect(pt))
::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
} else {
// gripper at top
CRect rc;
GetWindowRect(&rc);
rc.bottom=rc.top+8;
if(rc.PtInRect(pt))
::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
}
}
BOOL CToolBarEx::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg->message)
{
case WM_MOUSEMOVE:
MouseHover(pMsg->pt);
break;
}
return CToolBar::PreTranslateMessage(pMsg);
}
void CToolBarEx::OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle)
{
CToolBar::OnBarStyleChange(dwOldStyle,dwNewStyle);
if(( (dwOldStyle & CBRS_ORIENT_HORZ)&& (dwOldStyle & CBRS_ORIENT_VERT))||\
( (dwOldStyle & CBRS_ORIENT_VERT)&& (dwOldStyle & CBRS_ORIENT_HORZ)))
{
LoadBitmap(m_lpszResourceName);
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -