⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guitoolbarwnd.cpp

📁 这是一个串口监视程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------//
// 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		 //	
// fco_campos@tutopia.com													 //
//-----------------------------------------------------------------------//


#include "stdafx.h" 
#include "..\header\GuiToolBarWnd.h"
#include "..\header\GuiDrawlayer.h"
#include "..\header\GuiDockContext.h"
#include "..\header\guitoolbarwnd.h"
#include "..\header\GuiMiniFrameWnd.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;
	CWnd* m_pWnd;
	LPCTSTR mTitle;
	CDropmenu(UINT nID,UINT nMENU)
	{
		nIDboton=nID;
		nMenu=nMENU;
		m_pWnd=NULL;
		mTitle=NULL;
	}

	CDropmenu(UINT nID,CWnd* pWnd,LPCTSTR lpTitle)
	{
		nIDboton=nID;
		nMenu=NULL;
		m_pWnd=pWnd;
		mTitle=lpTitle;
	
	}
};
//////////////////////////////////////////////////////////////////////
// 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_WM_RBUTTONDOWN()
	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
	ON_WM_DESTROY()
	ON_WM_MOUSEMOVE()
	
END_MESSAGE_MAP()


int _afxGuiDropDownWidth = -1;

int AFXAPI _AfxGuiGetDropDownWidth()
{
	// return cached version if already determined...
	if (_afxGuiDropDownWidth != -1)
		return _afxGuiDropDownWidth;

	// 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', &_afxGuiDropDownWidth));
	if (hFont != NULL)
	{
		SelectObject(hDC, hFont);
		DeleteObject(hFont);
	}
	ReleaseDC(NULL, hDC);
	ASSERT(_afxGuiDropDownWidth != -1);
	return _afxGuiDropDownWidth;
}

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(0,TBSTYLE_FLAT);
	SetWindowText("ToolBar");
	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 = 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));
	_AfxGuiGetDropDownWidth();
	//m_iWidthDrowDown=GetSystemMetrics(SM_CXHTHUMB);
	CBitmap cbmp;
	BITMAP bmp;
	cbmp.LoadBitmap(IDB_DOCKBAR);
	cbmp.GetBitmap(&bmp);
	mHeight=CPoint(bmp.bmWidth,bmp.bmHeight);	
	//m_cfont.CreateFont(-11,0,0,0,400,0,0,0,0,1,2,1,34,"MS Sans Serif");
	m_cfont.CreateFont(14,0,0,0,400,0,0,0,0,1,2,1,34,_T("Marlett"));
	m_idLock=-1;
	bLoad=FALSE;
	m_ActualBtn=-1;
	
}

CGuiToolBarWnd::~CGuiToolBarWnd()
{
	m_pArray.RemoveAll();
	m_imgList.Detach();

}

void CGuiToolBarWnd::SetXp(BOOL m_bXp)
{
	m_bIsXp=m_bXp;
}

void CGuiToolBarWnd::SetTextButton(int IdButton,CString sztexto)
{
	
	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 +110/*drop height*/;
	if(!pControl->Create(dwStyle, rect, this, nID))
	{
		TRACE(_T("Failed to create combo-box\n"));
		return FALSE;
	}
	
	return TRUE;
}
BOOL CGuiToolBarWnd::CreateEdit(CEdit* pEdit,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 = 3;
	rect.bottom = rect.top +18/*drop height*/;
	if(!pEdit->Create(dwStyle, rect, this, nID))
	{
		TRACE(_T("Failed to create edit-box\n"));
		return FALSE;
	}
	return TRUE;
}
void CGuiToolBarWnd::SetToolTip(LPCTSTR lpMsg)
{
	CRect rc;
	LPCTSTR m_lpMsg=lpMsg;
	if (GetSafeHwnd ()== NULL) return;
	if (m_lpMsg != NULL)
	{
		if (m_tooltipCtrl.GetSafeHwnd () == NULL) 
		{
			m_tooltipCtrl.Create(this,TTS_ALWAYSTIP);
			m_tooltipCtrl.Activate(TRUE);
			m_tooltipCtrl.AddTool(this,lpMsg);
		}
		else
		{
			m_tooltipCtrl.UpdateTipText(lpMsg,this);	
		}
	}
	else
	{
		if (m_tooltipCtrl.GetSafeHwnd () == NULL) 
		{
			m_tooltipCtrl.Create(this,TTS_ALWAYSTIP);
			m_tooltipCtrl.AddTool(this,lpMsg);
		}
		else
			m_tooltipCtrl.UpdateTipText(lpMsg,this);	
		m_tooltipCtrl.Activate(FALSE);
	}
	

}

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++;
}


void CGuiToolBarWnd::SetButtonDropDown(UINT nID,int iImage,CWnd* pWnd,LPCTSTR lpTitle)
{
	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);
	pWnd->ShowWindow(SW_HIDE);
	m_pArray.SetAtGrow(m_iElements,new CDropmenu(nID,pWnd,lpTitle));
	m_iElements++;
}


BOOL CGuiToolBarWnd::OnDropDown(NMHDR* pNMHDR, LRESULT* pRes)
{
	
	const NMTOOLBAR& m_nmTb = *(NMTOOLBAR*)pNMHDR;
	CRect rc;
	BOOL bFind=FALSE;
	GetToolBarCtrl().GetRect(m_nmTb.iItem, rc);
	ClientToScreen(&rc);
	for(int i=0; i < m_iElements; i++)
	{
		if (((CDropmenu*)m_pArray[i])->nIDboton == (UINT)m_nmTb.iItem)
		{
			bFind=TRUE;
			break;
		}
	}
	
	if (!bFind)	return FALSE;
	
	if (((CDropmenu*)m_pArray[i])->nMenu > 0)
	{
		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;
	}
	else
	{

		if(((CDropmenu*)m_pArray[i])->m_pWnd)
		{
			bPressDown=TRUE;
			CRect rc;
			
			GetItemRect (CommandToIndex (((CDropmenu*)m_pArray[i])->nIDboton), rc);
			m_idLock=((CDropmenu*)m_pArray[i])->nIDboton;
			ClientToScreen (rc);
			rc.bottom += 1;
			CGuiMiniFrameWnd* p = new CGuiMiniFrameWnd;
			if (!p->Create(this,((CDropmenu*)m_pArray[i])->m_pWnd,rc,
				((CDropmenu*)m_pArray[i])->mTitle,rc.Width()-1))
			{
				if (((CDropmenu*)m_pArray[i])->m_pWnd->IsWindowVisible())
				{
					UnlockWindowUpdate();
					bPressDown=FALSE;
					return TRUE;
				}
			}
			InvalidateRect(m_rcSaveFloat,TRUE);
			UpdateWindow();
			LockWindowUpdate();
			bPressDown=FALSE;
		}
	}
	return TRUE;
}




//Thanks to Ernesto Garcia 
void CGuiToolBarWnd::OnDestroy()
{
	for( int i=0; i<m_iElements; i++ )
    {
    CDropmenu *pArr = (CDropmenu*)m_pArray.GetAt(i);
    if( pArr )
        delete pArr;
    }
	CToolBar::OnDestroy();
	// TODO: Add your message handler code here
}

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);
	TBBUTTON tbButton;
	HIMAGELIST hiImg;
	int nCountBtn;
	CRect rcBtn;
	CBrush cb;
	CRect rcClient;
	CRect rcAreaUpdate;
	UINT nID;
	UINT nStyle;
	WORD wStyle, wState;
	int iBitmap;
	int iUltbot=0;
	BOOL mbIsPress;
	BOOL mbIsEnabled;
	int OldMode=dc.SetBkMode(TRANSPARENT);
	GetClientRect(&rcClient);
	hiImg = (HIMAGELIST)SendMessage (TB_GETIMAGELIST, 0, 0);
    nCountBtn = SendMessage (TB_BUTTONCOUNT, 0, 0);
	cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorXP());
	dc.FillRect(rcClient,&cb);
	int j=0;
	dc.GetClipBox (rcAreaUpdate);
	for(int i=0; i < nCountBtn; i++)
	{
		SendMessage(TB_GETBUTTON, i, (LPARAM)&tbButton);
		GetItemRect(i,&rcBtn);
		GetButtonInfo(i,nID,nStyle,iBitmap);
		wStyle= LOWORD(nStyle);
		wState= HIWORD(nStyle);
		bOver=FALSE;
		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));
				
				if (!((wStyle &  TBSTYLE_SEP) &&(nID !=0)))
					dc.FillRect(m_Separ,&cbt);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -