?? mytoolbar.cpp
字號:
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);
}
m_ButtonImageSize.cx=pData->wWidth;
m_ButtonImageSize.cy=pData->wHeight;
UnlockResource(hGlobal);
FreeResource(hGlobal);
return bResult;
}
BOOL CMyToolBar::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 CMyToolBar::SetButtonEx(UINT nIDResource)
{
LPCTSTR lpszResourceName=MAKEINTRESOURCE(nIDResource);
return SetButtonEx(lpszResourceName);
}
BOOL CMyToolBar::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);
CRect rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
if(m_bBKStyle==bs_RGB)
{
int r1,g1,b1;
for(int i=0;i<=rc4.Height();i++)
{
r1=GetRValue(m_clrBack);
g1=GetGValue(m_clrBack);
b1=GetBValue(m_clrBack);
r1=(r1+3*abs(rc4.top+i-rect.Height()/2-rect.top))>255?255:(r1+3*abs(rc4.top+i-rect.Height()/2-rect.top));
g1=(g1+3*abs(rc4.top+i-rect.Height()/2-rect.top))>255?255:(g1+3*abs(rc4.top+i-rect.Height()/2-rect.top));
b1=(b1+3*abs(rc4.top+i-rect.Height()/2-rect.top))>255?255:(b1+3*abs(rc4.top+i-rect.Height()/2-rect.top));
CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
CPen *old = dc4.SelectObject(&pen);
dc4.MoveTo(rc4.left,rc4.top+i);
dc4.LineTo(rc4.right,rc4.top+i);
dc4.SelectObject(old);
}
}
else
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 CMyToolBar::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 CMyToolBar::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg->message)
{
case WM_MOUSEMOVE:
MouseHover(pMsg->pt);
break;
}
return CToolBar::PreTranslateMessage(pMsg);
}
void CMyToolBar::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();
}
}
void CMyToolBar::SetShowText(BOOL blnShow)
{
CSize szButton;
int i;
if(blnShow)
{
for(i = 0; i <GetCount(); i++) //為每個按鈕增加文本
{
CString strButton;
UINT id =GetItemID(i);
if(!strButton.LoadString(id)) continue;
int j = strButton.Find(_T('\n'));
if(j < 0) continue;
strButton = strButton.Right(strButton.GetLength() - j - 1);
SetButtonText(i,strButton);
}
szButton.cx=m_ButtonSize.cx+15;
szButton.cy=m_ButtonSize.cy+15;
}
else
{
szButton.cx=m_ButtonSize.cx;
szButton.cy=m_ButtonSize.cy;
}
SetSizes(szButton,m_ButtonImageSize);
}
void CMyToolBar::SetSizes(SIZE sizeButton)
{
SetSizes(sizeButton,m_ButtonImageSize);
}
void CMyToolBar::SetSizes(SIZE sizeButton, SIZE sizeImage )
{
CToolBar::SetSizes(sizeButton,sizeImage);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -