📄 guitoolbarwnd.cpp.svn-base
字号:
//-----------------------------------------------------------------------//
// This is a part of the GuiLib MFC Extention. //
// Autor : Francisco Campos //
// (C) 2002 Francisco Campos <www.beyondata.com> All rights reserved //
// This code is provided "as is", with absolutely no warranty expressed //
// or implied. Any use is at your own risk. //
// You must obtain the author's consent before you can include this code //
// in a software library. //
// If the source code in this file is used in any application //
// then acknowledgement must be made to the author of this program //
// fcampos@tutopia.com //
//-----------------------------------------------------------------------//
#include "stdafx.h"
#include "GuiToolBarWnd.h"
#include "resource.h"
#include "GuiDrawlayer.h"
#include "GuiDockContext.h"
//#include "GuiColors.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define BTNS_SHOWTEXT 0x0040
extern int gbintHorz; //Horz=0, Vert=1
extern CRect rcMenu; //CRect of button
class CDropmenu
{
public:
UINT nIDboton;
UINT nMenu;
CDropmenu(UINT nID,UINT nMENU)
{
nIDboton=nID;
nMenu=nMENU;
}
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CGuiToolBarWnd,CToolBar)
BEGIN_MESSAGE_MAP(CGuiToolBarWnd,CToolBar)
//{{AFX_MSG_MAP(CGuiToolBarWnd)
ON_WM_PAINT()
ON_WM_NCPAINT()
ON_WM_NCCALCSIZE()
ON_WM_WINDOWPOSCHANGING()
ON_WM_LBUTTONDOWN()
ON_NOTIFY_REFLECT_EX(TBN_DROPDOWN,OnDropDown)
ON_WM_TIMER()
ON_WM_LBUTTONUP()
ON_WM_SYSCOLORCHANGE()
ON_WM_SETCURSOR()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
int _afxDropDownWidth = -1;
int AFXAPI _AfxGetDropDownWidth()
{
// return cached version if already determined...
if (_afxDropDownWidth != -1)
return _afxDropDownWidth;
// otherwise calculate it...
HDC hDC = GetDC(NULL);
ASSERT(hDC != NULL);
HFONT hFont;
if ((hFont = CreateFont(GetSystemMetrics(SM_CYMENUCHECK), 0, 0, 0,
FW_NORMAL, 0, 0, 0, SYMBOL_CHARSET, 0, 0, 0, 0, _T("Marlett"))) != NULL)
hFont = (HFONT)SelectObject(hDC, hFont);
VERIFY(GetCharWidth(hDC, '6', '6', &_afxDropDownWidth));
if (hFont != NULL)
{
SelectObject(hDC, hFont);
DeleteObject(hFont);
}
ReleaseDC(NULL, hDC);
ASSERT(_afxDropDownWidth != -1);
return _afxDropDownWidth;
}
int CGuiToolBarWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
lpCreateStruct->dwExStyle |= WS_CLIPCHILDREN; // prevents flashing
if (CToolBar::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_pDockContext==NULL)
m_pDockContext=new CGuiDockContext(this);
ASSERT(m_pDockContext);
//ModifyStyle(WS_CAPTION,0);
return 0;
}
//***********************************************************************************
void CGuiToolBarWnd::OnUpdateCmdUI( CFrameWnd* pTarget, BOOL bDisableIfNoHndler )
{
CToolBar::OnUpdateCmdUI( pTarget, bDisableIfNoHndler );
}
//***********************************************************************************
void CGuiToolBarWnd::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanging(lpwndpos);
if(!(m_dwStyle & CBRS_FLOATING))
SetSizes(m_sizeButton, m_sizeImage);
if( !(lpwndpos->flags & SWP_NOMOVE) )
Invalidate(FALSE);
}
//***********************************************************************************
void CGuiToolBarWnd::OnSysColorChange( )
{
CToolBar::OnSysColorChange( );
if(!(m_dwStyle & CBRS_FLOATING))
SetSizes(m_sizeButton, m_sizeImage);
Invalidate(TRUE);
GetParentFrame()->RecalcLayout();
}
//***********************************************************************************
CGuiToolBarWnd::CGuiToolBarWnd()
{
m_style = Office2000; //Office97; //by default
m_pArray.RemoveAll();
m_iElements=0;
m_bIsXp=TRUE;
bPressDown=FALSE;
m_imgArrow.Create(IDB_DOCKBAR,9,10,RGB(255,0,255));
_AfxGetDropDownWidth();
//m_iWidthDrowDown=GetSystemMetrics(SM_CXHTHUMB);
CBitmap cbmp;
BITMAP bmp;
cbmp.LoadBitmap(IDB_DOCKBAR);
cbmp.GetBitmap(&bmp);
mHeight=CPoint(bmp.bmWidth,bmp.bmHeight);
// Modified Bu SunZhenyu
if( !m_cfont.CreateStockObject( DEFAULT_GUI_FONT ) )
m_cfont.CreateStockObject( ANSI_VAR_FONT );
// m_cfont.CreateFont(14,0,0,0,400,0,0,0,0,1,2,1,34,_T("Marlett"));
}
CGuiToolBarWnd::~CGuiToolBarWnd()
{
/* Modified By SunZhenyu 2003/8/22, Add next 7 lines. */
for(int i=0; i<m_pArray.GetSize(); i++)
{
CDropmenu * p = (CDropmenu*)m_pArray[i];
if( p )
delete p;
}
m_pArray.RemoveAll();
}
void CGuiToolBarWnd::SetXp(BOOL m_bXp)
{
m_bIsXp=m_bXp;
}
void CGuiToolBarWnd::SetTextButton(int IdButton,CString sztexto)
{
// CToolBarCtrl &tbc=GetToolBarCtrl();
// tbc.SetStyle(tbc.GetStyle()| TBSTYLE_LIST | CCS_ADJUSTABLE);
sztexto+=sztexto.Find( '&' ) == -1 ?" ":" ";
CClientDC dc(this);
SetButtonText(IdButton,sztexto);
SetButtonStyle(IdButton,TBSTYLE_AUTOSIZE);
SetSizes(m_sizeButton, m_sizeImage);
}
void CGuiToolBarWnd::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CToolBar::OnNcCalcSize(bCalcValidRects, lpncsp);
}
BOOL CGuiToolBarWnd::CreateCombo(CComboBox* pControl,UINT nID,int iSize,DWORD dwStyle)
{
CRect rect;
CFont m_Font;
m_Font.CreateStockObject (DEFAULT_GUI_FONT);
int nIndex = this->GetToolBarCtrl().CommandToIndex(nID);
this->SetButtonInfo(nIndex, nID, TBBS_SEPARATOR, iSize);
this->GetToolBarCtrl().GetItemRect(nIndex, &rect);
rect.bottom-=1;
rect.top = 1;
rect.bottom = rect.top +iSize/*drop height*/;
if(!pControl->Create(dwStyle, rect, this, nID))
{
TRACE(_T("Failed to create combo-box\n"));
return FALSE;
}
return TRUE;
}
BOOL CGuiToolBarWnd::CreateCombo(CComboBoxEx* pControl,UINT nID,int iSize,CImageList* m_imgList,DWORD dwStyle)
{
CRect rect;
int nIndex = this->GetToolBarCtrl().CommandToIndex(nID);
this->SetButtonInfo(nIndex, nID, TBBS_SEPARATOR, iSize);
this->GetToolBarCtrl().GetItemRect(nIndex, &rect);
rect.top = 1;
rect.bottom = rect.top + 250 /*drop height*/;
if(!pControl->Create(dwStyle, rect, this, nID))
{
TRACE(_T("Failed to create combo-box\n"));
return FALSE;
}
pControl->SetImageList(m_imgList);
return TRUE;
}
void CGuiToolBarWnd::AddRowComBoboxEx(CComboBoxEx* pControl,CString szMsg,int iItem,int iImage)
{
COMBOBOXEXITEM cb;
cb.mask=CBEIF_TEXT | CBEIF_IMAGE |CBEIF_SELECTEDIMAGE;
cb.pszText=(LPTSTR)(LPCTSTR)szMsg;
cb.iItem=iItem;
cb.iImage=cb.iSelectedImage=iImage;
cb.cchTextMax=20;
pControl->InsertItem(&cb);
}
void CGuiToolBarWnd::SetButtonDropDown(UINT nID,int iImage,UINT nMENU)
{
CToolBarCtrl &m_tbc= GetToolBarCtrl();
m_tbc.SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
m_tbc.SetStyle(m_tbc.GetStyle() | TBSTYLE_LIST | CCS_ADJUSTABLE);
int nIndex = this->GetToolBarCtrl().CommandToIndex(nID);
this->SetButtonInfo(nIndex, nID, TBBS_DROPDOWN, iImage);
m_pArray.SetAtGrow(m_iElements,new CDropmenu(nID,nMENU));
m_iElements++;
}
BOOL CGuiToolBarWnd::OnDropDown(NMHDR* pNMHDR, LRESULT* pRes)
{
const NMTOOLBAR& m_nmTb = *(NMTOOLBAR*)pNMHDR;
CRect rc;
GetToolBarCtrl().GetRect(m_nmTb.iItem, rc);
ClientToScreen(&rc);
int i;
for(i=0; i < m_iElements; i++)
{
if (((CDropmenu*)m_pArray[i])->nIDboton == (UINT)m_nmTb.iItem)
break;
}
bPressDown=TRUE;
CMenu m_menu;
m_menu.LoadMenu(((CDropmenu*)m_pArray[i])->nMenu);
CMenu* m_SubMenu = m_menu.GetSubMenu(0);
gbintHorz=0; //Horz=0, Vert=1
rcMenu=rc; //CRect of button
m_SubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL,
rc.left, rc.bottom-2, AfxGetMainWnd(), &rc);
bPressDown=FALSE;
return TRUE;
}
void CGuiToolBarWnd::OnNcPaint()
{
CRect rcWindow;
CRect rcClient;
CWindowDC dc(this);
GetWindowRect(&rcWindow);
GetClientRect(&rcClient);
rcClient.OffsetRect(-rcWindow.TopLeft());
rcWindow.OffsetRect(-rcWindow.TopLeft());
ScreenToClient(rcWindow);
rcClient.OffsetRect(-rcWindow.left,-rcWindow.top);
//if (!m_bIsXp)
if (m_dwStyle & CBRS_ORIENT_HORZ && !(m_dwStyle & CBRS_FLOATING))
{rcClient.top+=2;rcClient.left+=1;}
dc.ExcludeClipRect(rcClient);
rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);
rcWindow.DeflateRect(1,1);
//pintamos la izquierda
dc.FillSolidRect(0,0,rcWindow.right+1,rcWindow.bottom+2,::GetSysColor(COLOR_BTNFACE));
dc.FillSolidRect(0,rcWindow.top+1,rcWindow.right+1,1,::GetSysColor(COLOR_BTNFACE));
//pintamos el borde inferior
dc.FillSolidRect(1,rcWindow.bottom-1,rcWindow.right+1,rcWindow.bottom-1,::GetSysColor(COLOR_BTNFACE));
dc.FillSolidRect(0,rcWindow.bottom+1,rcWindow.right+1,rcWindow.bottom+1,::GetSysColor(COLOR_BTNFACE ));
//pintamos la derecha
dc.FillSolidRect(rcWindow.right,0,rcWindow.right-1,rcWindow.bottom,::GetSysColor(COLOR_BTNFACE));
dc.FillSolidRect(rcWindow.right+1,-1,rcWindow.right+4,rcWindow.bottom+4,::GetSysColor(COLOR_BTNFACE ));
dc.IntersectClipRect(rcWindow);
CBrush cb;
cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorXP());
dc.FillRect(rcWindow,&cb);
DrawGripper(&dc,&rcWindow);
}
void CGuiToolBarWnd::OnPaint()
{
CPaintDC dc(this);
CRect rcBtn;
int nBtn=GetToolBarCtrl().GetButtonCount();
int iBitmap;
UINT nID;
UINT nStyle;
WORD wStyle, wState;
DWORD dwBarStyle = GetToolBarCtrl().GetStyle();
BOOL mbIsPress;
BOOL mbIsEnabled;
CBrush cb;
CImageList m_imgList;
CRect rcUpdate;
int iUltbot;
CRect rcClient;
GetClientRect(&rcClient);
cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorXP());
// draw borders in non-client area
CRect rcDraw = rcClient;
dc.FillRect(rcDraw, &cb);
HIMAGELIST hiImg = 0;
iBitmap=0;
hiImg = HIMAGELIST(SendMessage(TB_GETIMAGELIST));
m_imgList.Attach(hiImg);
int j=0;
for(int i=0; i < nBtn; i++)
{
bOver=FALSE;
GetItemRect(i,&rcBtn);
GetButtonInfo(i,nID,nStyle,iBitmap);
wStyle= LOWORD(nStyle);
wState= HIWORD(nStyle);
if (wState & TBSTATE_HIDDEN) continue;
if (wStyle == TBSTYLE_SEP)
{
if (!(wState & TBSTATE_WRAP) || ! IsFloating() )
{
CRect m_Separ;
m_Separ=rcBtn;
//si existe un separador vertical
if (m_dwStyle & CBRS_ORIENT_HORZ)
{
dc.FillRect(&rcBtn,&cb);
int nhalf=(m_Separ.Width()/2)-1;
m_Separ.top-=1;
m_Separ.left+=nhalf;
m_Separ.right=m_Separ.left+1;
m_Separ.bottom+=1;
}
else
{
int nhalf=(m_Separ.Width()/2)-1;
int iHeigh=m_Separ.Height();
m_Separ.left=0;
m_Separ.right=m_Separ.left+m_Separ.Height()+1;
m_Separ.top=iUltbot;
m_Separ.bottom=m_Separ.top+m_Separ.Width();
dc.FillRect(&m_Separ,&cb);
m_Separ.left=0;
m_Separ.right=m_Separ.left+iHeigh+1;
m_Separ.top=iUltbot+nhalf;
m_Separ.bottom=m_Separ.top+1;
}
CBrush cbt;
cbt.CreateSolidBrush(RGB(167, 167, 167));
dc.FillRect(m_Separ,&cbt);
continue;
}
}
else {
mbIsPress=(wState & TBSTATE_CHECKED) || (wState & TBSTATE_PRESSED);
mbIsEnabled= GetToolBarCtrl().IsButtonEnabled((int)nID);
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
CBrush cblu;
if(!rcBtn.PtInRect(pt))
{
if (wState & TBSTATE_CHECKED)
{
cblu.CreateSolidBrush(GuiDrawLayer::GetRGBPressBXP());
dc.FillRect(rcBtn,&cblu);
}
else
{
dc.FillRect(rcBtn,&cb);
}
}
else
{
if (bPressDown)
dc.Draw3dRect(rcBtn,GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorShadow());
else
dc.Draw3dRect(rcBtn,GuiDrawLayer::GetRGBCaptionXP(),GuiDrawLayer::GetRGBCaptionXP());
rcBtn.DeflateRect(1,1);
if (wState & TBSTATE_CHECKED)
{
cblu.CreateSolidBrush(GuiDrawLayer::GetRGBFondoXP());
dc.FillRect(rcBtn,&cblu);
}
else
{
if (bPressDown)
{
cblu.CreateSolidBrush(GuiDrawLayer::GetRGBColorXP());
dc.FillRect(rcBtn,&cblu);
}
else
{
cblu.CreateSolidBrush(mbIsPress?GuiDrawLayer::GetRGBPressBXP():GuiDrawLayer::GetRGBFondoXP());
dc.FillRect(rcBtn,&cblu);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -