📄 toolbarex.cpp
字号:
// ToolBarEx.cpp : implementation file
//程序中借用了不少人的代码,没有他们,就没有本工具栏
//修改:无心2006.2.28
#include "stdafx.h"
#include "ToolBarEx.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* 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);
}
/////////////////////////////////////////////////////////////////////////////
// CToolBarEx
IMPLEMENT_DYNAMIC(CToolBarEx,CToolBar)
BEGIN_MESSAGE_MAP(CToolBarEx, CToolBar)
//{{AFX_MSG_MAP(CToolBarEx)
ON_WM_ERASEBKGND()
ON_WM_WINDOWPOSCHANGING()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CToolBarEx::CToolBarEx()
{
m_bkbrush.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
m_bBKStyle=bs_ORG;
}
CToolBarEx::~CToolBarEx()
{
try{
delete[] m_pbtButtonStyle;
}
catch(...)
{}
}
/////////////////////////////////////////////////////////////////////////////
// CToolBarEx message handlers
void CToolBarEx::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 CToolBarEx::OnEraseBkgnd(CDC *pDC)
{
switch(m_bBKStyle)
{
case bs_RGB:
case bs_BITMAP:case bs_ORG:
RECT rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
pDC->FillRect(&rect,&m_bkbrush);
break;
default:
CToolBar::OnEraseBkgnd(pDC);
break;
}
return TRUE;
}
void CToolBarEx::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanging(lpwndpos);
// TODO: Add your message handler code here
RepaintBackground();
}
void CToolBarEx::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 CToolBarEx::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 CToolBarEx::DrawSeparators()
{
// get a dc for the client area
CClientDC dc(this);
// draw the separators on it
DrawSeparators(&dc);
}
// Draw the separators
void CToolBarEx::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 CToolBarEx::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 CToolBarEx::SetBKColor(COLORREF color)
{
m_bkbrush.DeleteObject();
m_bkbrush.CreateSolidBrush(color);
m_bBKStyle=bs_RGB;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
void CToolBarEx::SetBKImage(UINT nIDResource)
{
CBitmap tmp;
tmp.LoadBitmap(nIDResource);
m_bkbrush.DeleteObject();
m_bkbrush.CreatePatternBrush(&tmp);
m_bBKStyle=bs_BITMAP;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
void CToolBarEx::SetBKImage(LPCTSTR lpszResourceName)
{
CBitmap tmp;
tmp.LoadBitmap(lpszResourceName);
m_bkbrush.DeleteObject();
m_bkbrush.CreatePatternBrush(&tmp);
m_bBKStyle=bs_BITMAP;
SetButtonEx(m_lpszResourceName);
Invalidate();
}
BOOL CToolBarEx::LoadToolBar(UINT nIDResource)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -