?? mytoolbar.cpp
字號:
#include "stdafx.h"
#include "MyToolBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct CToolBarData
{
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
//WORD aItems[wItemCount]
WORD* items()
{ return (WORD*)(this+1); }
};
void TransparentBltEx( HDC hdcDest,
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
HDC hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
UINT crTransparent
)
{
HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest); // 創建兼容位圖
HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL); // 創建單色掩碼位圖
HDC hImageDC = CreateCompatibleDC(hdcDest);
HDC hMaskDC = CreateCompatibleDC(hdcDest);
hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP);
hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC, hMaskBMP);
if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc)
BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
StretchBlt(hImageDC, 0, 0, nWidthDest, nHeightDest,
hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
SetBkColor(hImageDC, crTransparent);
BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);
SetBkColor(hImageDC, RGB(0,0,0));
SetTextColor(hImageDC, RGB(255,255,255));
BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
SetBkColor(hdcDest,RGB(255,255,255));
SetTextColor(hdcDest,RGB(0,0,0));
BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
// or
BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);
SelectObject(hImageDC, hOldImageBMP);
DeleteDC(hImageDC);
SelectObject(hMaskDC, hOldMaskBMP);
DeleteDC(hMaskDC);
DeleteObject(hImageBMP);
DeleteObject(hMaskBMP);
}
/////////////////////////////////////////////////////////////////////////////
// CMyToolBar
IMPLEMENT_DYNAMIC(CMyToolBar,CToolBar)
BEGIN_MESSAGE_MAP(CMyToolBar, CToolBar)
//{{AFX_MSG_MAP(CMyToolBar)
ON_WM_ERASEBKGND()
ON_WM_WINDOWPOSCHANGING()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMyToolBar::CMyToolBar()
{
m_bBKStyle=bs_RGB;
m_clrBack=RGB(161,192,245);
m_bkbrush.CreateSolidBrush(RGB(161,192,245));
}
CMyToolBar::~CMyToolBar()
{
try{
delete[] m_pbtButtonStyle;
}
catch(...)
{}
}
/////////////////////////////////////////////////////////////////////////////
// CMyToolBar message handlers
void CMyToolBar::EraseNonClient()
{
// get window DC that is clipped to the non-client area
CWindowDC dc(this);
CRect rectClient;
GetClientRect(rectClient);
CRect rectWindow;
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);
rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
dc.ExcludeClipRect(rectClient);
// draw borders in non-client area
rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
DrawBorders(&dc, rectWindow);
// erase parts not drawn
dc.IntersectClipRect(rectWindow);
SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
DrawGripper(&dc, rectWindow); //
}
BOOL CMyToolBar::OnEraseBkgnd(CDC *pDC)
{
CRect rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
int r1,g1,b1,i;
switch(m_bBKStyle)
{
case bs_RGB:
r1=GetRValue(m_clrBack);
g1=GetGValue(m_clrBack);
b1=GetBValue(m_clrBack);
for(i=rect.Height()/2;i>=0;i--)
{
r1=(r1+3)>255?255:(r1+3);
g1=(g1+3)>255?255:(g1+3);
b1=(b1+3)>255?255:(b1+3);
CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
CPen *old = pDC->SelectObject(&pen);
pDC->MoveTo(rect.left,rect.top+i);
pDC->LineTo(rect.right,rect.top+i);
pDC->MoveTo(rect.left,rect.bottom-i);
pDC->LineTo(rect.right,rect.bottom-i);
pDC->SelectObject(old);
}
break;
case bs_BITMAP:
case bs_ORG:
pDC->FillRect(&rect,&m_bkbrush);
break;
default:
CToolBar::OnEraseBkgnd(pDC);
break;
}
CPen spen(PS_SOLID,1,RGB(255,255,255));
CPen *mpen=pDC->SelectObject(&spen);
pDC->MoveTo(rect.left,rect.bottom-3);
pDC->LineTo(rect.right,rect.bottom-3);
pDC->MoveTo(rect.left,rect.bottom-1);
pDC->LineTo(rect.right,rect.bottom-1);
CPen npen(PS_SOLID,1,RGB(178,178,178));
pDC->SelectObject(&npen);
pDC->MoveTo(rect.left,rect.bottom-2);
pDC->LineTo(rect.right,rect.bottom-2);
pDC->SelectObject(mpen);
return TRUE;
}
void CMyToolBar::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanging(lpwndpos);
// TODO: Add your message handler code here
RepaintBackground();
}
void CMyToolBar::RepaintBackground()
{
// get parent window (there should be one)
CWnd* pParent = GetParent();
if (pParent) {
// get rect for this toolbar
CRect rw; GetWindowRect(&rw);
// convert rect to parent coords
CRect rc = rw; pParent->ScreenToClient(&rc);
// invalidate this part of parent
pParent->InvalidateRect(&rc);
// now do all the other toolbars (etc) that belong to the parent
for (
CWnd* pSibling = pParent->GetWindow(GW_CHILD);
pSibling;
pSibling = pSibling->GetNextWindow(GW_HWNDNEXT)
) {
// but do not draw ourselves
if (pSibling == this) continue;
// convert rect to siblings coords
CRect rc = rw; pSibling->ScreenToClient(&rc);
// invalidate this part of sibling
pSibling->InvalidateRect(&rc);
}
}
}
void CMyToolBar::OnPaint()
{
//CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// standard tolbar
CToolBar::OnPaint();
// erase the background
EraseNonClient();
// plus separators
DrawSeparators();
// Do not call CToolBar::OnPaint() for painting messages
}
// Draw the separators in the client area
void CMyToolBar::DrawSeparators()
{
// get a dc for the client area
CClientDC dc(this);
// draw the separators on it
DrawSeparators(&dc);
}
// Draw the separators
void CMyToolBar::DrawSeparators(CClientDC* pDC)
{
// horizontal vs vertical
bool ishorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
// get number of buttons
int nIndexMax = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
int nIndex;
// try each button
for (nIndex = 0; nIndex < nIndexMax; nIndex++)
{
UINT dwStyle=GetButtonStyle(nIndex);
UINT wStyle=LOWORD(dwStyle);
// if it is a separator
if (wStyle == TBBS_SEPARATOR)
{
// get it's rectangle and width
CRect rect;
GetItemRect(nIndex,rect);
// if small enough to be a true separator
int w=rect.Width();
if (w <= 8)
{
if (ishorz)
{
// draw the separator bar in the middle
CRect rectbar=rect;
int x=(rectbar.left+rectbar.right)/2;
rectbar.left=x-1;
rectbar.right=x+1;
pDC->Draw3dRect(rectbar,::GetSysColor(COLOR_3DSHADOW),::GetSysColor(COLOR_3DHILIGHT));
}
else
{
// draw the separator bar in the middle
CRect rectbar = rect;
rectbar.left = rectbar.left - m_sizeButton.cx;
rectbar.right = rectbar.left + m_sizeButton.cx;
rectbar.top = rectbar.bottom+1;
rectbar.bottom = rectbar.top+3;
int y = (rectbar.top+rectbar.bottom)/2;
rectbar.top = y-1;
rectbar.bottom = y+1;
pDC->Draw3dRect(rectbar,::GetSysColor(COLOR_3DSHADOW),::GetSysColor(COLOR_3DHILIGHT));
}
}
}
}
}
// Draw the gripper at left or top
void CMyToolBar::DrawGripper(CWindowDC *pDC, CRect& rectWindow)
{
// get the gripper rect (1 pixel smaller than toolbar)
CRect gripper = rectWindow;
CRect rc;
CBrush brush1,brush2;
brush1.CreateSolidBrush(RGB(192,192,192));
brush2.CreateSolidBrush(RGB(255,255,255));
gripper.DeflateRect(1,1);
if (m_dwStyle & CBRS_FLOATING) {
// no grippers
} else if (m_dwStyle & CBRS_ORIENT_HORZ) {
// gripper at left
rc.top=gripper.top+3;
rc.left=gripper.left+1;
rc.right=rc.left+2;
rc.bottom=rc.top+2;
for(int i=0;i<(gripper.Height()-4)/5;i++)
{
//pDC->Draw3dRect(rc,RGB(128,128,128),RGB(255,255,255));//::GetSysColor(COLOR_3DHIGHLIGHT),::GetSysColor(COLOR_3DSHADOW));
rc.OffsetRect(+1,+1);
pDC->FillRect(&rc,&brush2) ;
rc.OffsetRect(-1,-1);
pDC->FillRect(&rc,&brush1) ;
rc.OffsetRect(0,+5);
}
} else {
// gripper at top
rc.top=gripper.top+1;
rc.left=gripper.left+3;
rc.right=rc.left+2;
rc.bottom=rc.top+2;
for(int i=0;i<(gripper.Width()-4)/5;i++)
{
//pDC->Draw3dRect(rc,RGB(128,128,128),RGB(255,255,255));//::GetSysColor(COLOR_3DHIGHLIGHT),::GetSysColor(COLOR_3DSHADOW));
rc.OffsetRect(+1,+1);
pDC->FillRect(&rc,&brush2) ;
rc.OffsetRect(-1,-1);
pDC->FillRect(&rc,&brush1) ;
rc.OffsetRect(+5,0);
}
}
}
void CMyToolBar::SetBKColor(COLORREF color)
{
m_bkbrush.DeleteObject();
m_bkbrush.CreateSolidBrush(color);
m_bBKStyle=bs_RGB;
m_clrBack=color;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
void CMyToolBar::SetDefaultStyle()
{
SetBKColor(RGB(161,192,245));
}
void CMyToolBar::SetBKImage(UINT nIDResource)
{
CBitmap tmp;
tmp.LoadBitmap(nIDResource);
m_bkbrush.DeleteObject();
m_bkbrush.CreatePatternBrush(&tmp);
m_bBKStyle=bs_BITMAP;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
void CMyToolBar::SetBKImage(LPCTSTR lpszResourceName)
{
CBitmap tmp;
tmp.LoadBitmap(lpszResourceName);
m_bkbrush.DeleteObject();
m_bkbrush.CreatePatternBrush(&tmp);
m_bBKStyle=bs_BITMAP;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
BOOL CMyToolBar::LoadToolBar(UINT nIDResource)
{
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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -