www.pudn.com > ToolBar_ATL.rar > KToolBarCtrl.h


    // KToolBarCtrl.h : Declaration of the CKToolBarCtrl 
 
#ifndef __KTOOLBARCTRL_H_ 
#define __KTOOLBARCTRL_H_ 
 
#include "resource.h"       // main symbols 
#include  
#include "KToolBarCP.h" 
#include "KToolBar.h" 
#include "picholder.h" 
#include "typedef.h" 
#include "ToolBarDoc.h" 
#include "ToolBarView.h" 
#include  
using namespace std; 
 
#define VBOOL2BOOL(VT) (VT==-1?TRUE:FALSE) 
#define BOOL2VBOOL(B) (B==TRUE?-1:0) 
#define ICON_READ_SIZE 446 
 
///////////////////////////////////////////////////////////////////////////// 
// CKToolBarCtrl 
class ATL_NO_VTABLE CKToolBarCtrl :  
	public CComObjectRootEx, 
	public CStockPropImpl, 
	public CComControl, 
	public IPersistStreamInitImpl, 
	public IOleControlImpl, 
	public IOleObjectImpl, 
	public IOleInPlaceActiveObjectImpl, 
	public IViewObjectExImpl, 
	public IOleInPlaceObjectWindowlessImpl, 
	public IConnectionPointContainerImpl, 
	public IPersistStorageImpl, 
	public ISpecifyPropertyPagesImpl, 
	public IQuickActivateImpl, 
	public IDataObjectImpl, 
	public IProvideClassInfo2Impl<&CLSID_KToolBarCtrl, &DIID__IKToolBarCtrlEvents, &LIBID_KTOOLBARLib>, 
	public IPropertyNotifySinkCP, 
	public CComCoClass, 
	public CProxy_IKToolBarCtrlEvents< CKToolBarCtrl > 
{ 
public: 
	CKToolBarCtrl(); 
	~CKToolBarCtrl(); 
 
DECLARE_REGISTRY_RESOURCEID(IDR_KTOOLBARCTRL) 
 
DECLARE_PROTECT_FINAL_CONSTRUCT() 
 
BEGIN_COM_MAP(CKToolBarCtrl) 
	COM_INTERFACE_ENTRY(IKToolBarCtrl) 
	COM_INTERFACE_ENTRY(IDispatch) 
	COM_INTERFACE_ENTRY(IViewObjectEx) 
	COM_INTERFACE_ENTRY(IViewObject2) 
	COM_INTERFACE_ENTRY(IViewObject) 
	COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless) 
	COM_INTERFACE_ENTRY(IOleInPlaceObject) 
	COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless) 
	COM_INTERFACE_ENTRY(IOleInPlaceActiveObject) 
	COM_INTERFACE_ENTRY(IOleControl) 
	COM_INTERFACE_ENTRY(IOleObject) 
	COM_INTERFACE_ENTRY(IPersistStreamInit) 
	COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit) 
	COM_INTERFACE_ENTRY(IConnectionPointContainer) 
	COM_INTERFACE_ENTRY(ISpecifyPropertyPages) 
	COM_INTERFACE_ENTRY(IQuickActivate) 
	COM_INTERFACE_ENTRY(IPersistStorage) 
	COM_INTERFACE_ENTRY(IDataObject) 
	COM_INTERFACE_ENTRY(IProvideClassInfo) 
	COM_INTERFACE_ENTRY(IProvideClassInfo2) 
	COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer) 
END_COM_MAP() 
 
BEGIN_PROP_MAP(CKToolBarCtrl) 
	PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4) 
	PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4) 
	//PROP_DATA_ENTRY("Buttons", m_pTBItems, VT_UNKNOWN) 
	PROP_ENTRY("Value",5,CLSID_NULL) 
	PROP_ENTRY("Enabled", DISPID_ENABLED, CLSID_NULL) 
	// Example entries 
	// PROP_ENTRY("Property Description", dispid, clsid) 
	PROP_PAGE(CLSID_KToolBarPpgGeneral) 
END_PROP_MAP() 
 
BEGIN_CONNECTION_POINT_MAP(CKToolBarCtrl) 
	CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink) 
	CONNECTION_POINT_ENTRY(DIID__IKToolBarCtrlEvents) 
END_CONNECTION_POINT_MAP() 
 
BEGIN_MSG_MAP(CKToolBarCtrl) 
	CHAIN_MSG_MAP(CComControl) 
	DEFAULT_REFLECTION_HANDLER() 
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown) 
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUP) 
	MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove) 
	MESSAGE_HANDLER(WM_SIZE, OnSize) 
	MESSAGE_HANDLER(WM_CREATE, OnCreate) 
	MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk) 
END_MSG_MAP() 
// Handler prototypes: 
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); 
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled); 
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled); 
 
 
// IViewObjectEx 
	DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE) 
 
// IKToolBarCtrl 
public: 
	STDMETHOD(get_BtnKey)(/*[in]*/int Index, /*[out, retval]*/ BSTR *pVal); 
	STDMETHOD(put_BtnKey)(/*[in]*/int Index, /*[in]*/ BSTR newVal); 
	STDMETHOD(get_BtnToolTips)(/*[in]*/ int Index, /*[out, retval]*/ BSTR *pVal); 
	STDMETHOD(put_BtnToolTips)(/*[in]*/ int Index, /*[in]*/ BSTR newVal); 
	STDMETHOD(get_BtnCnt)(/*[out, retval]*/ short *pVal); 
	STDMETHOD(put_BtnCnt)(/*[in]*/ short newVal); 
	STDMETHOD(get_Value)(/*[out, retval]*/ short *pVal); 
	STDMETHOD(put_Value)(/*[in]*/ short newVal); 
	STDMETHOD(get_BtnIcon)(/*[in]*/ short Index, /*[out, retval]*/ LPPICTUREDISP *pVal); 
	STDMETHOD(put_BtnIcon)(/*[in]*/ short Index, /*[in]*/ LPPICTUREDISP newVal); 
	STDMETHOD(get_BtnStyle)(/*[in]*/ short Index, /*[out, retval]*/ BTNSTYLE *pVal); 
	STDMETHOD(put_BtnStyle)(/*[in]*/ short Index, /*[in]*/ BTNSTYLE newVal); 
	STDMETHOD(get_BtnEnabled)(/*[in]*/ short Index, /*[out, retval]*/ BOOL *pVal); 
	STDMETHOD(put_BtnEnabled)(/*[in]*/ short Index, /*[in]*/ BOOL newVal); 
	HRESULT OnDraw(ATL_DRAWINFO& di); 
 
	STDMETHOD(DoVerb)(LONG iVerb,LPMSG lpmsg, IOleClientSite *pActiveSite, LONG lindex, HWND hwndParent, LPCRECT lprcPosRect) 
	{ 
		if(iVerb == 1){ 
			return DoVerbProperties(lprcPosRect, hwndParent); 
		} 
 
		return IOleObjectImpl::DoVerb(iVerb, lpmsg, pActiveSite, 
												lindex, hwndParent, lprcPosRect); 
	} 
	 
	STDMETHOD(SetExtent)(DWORD dwDrawAspect, SIZEL *psizel) 
	{ 
		ATLTRACE(_T("SetExtent sizing control to ??x29\n")); 
		SIZEL szlPixels; 
		AtlHiMetricToPixel(psizel, &szlPixels); 
		szlPixels.cy = 29; 
		m_pView->SetBarHeight(szlPixels.cy); 
		m_pView->SetBarWidth(szlPixels.cx); 
		AtlPixelToHiMetric(&szlPixels,psizel); 
		return IOleObjectImpl::SetExtent(dwDrawAspect, psizel); 
	} 
 
	STDMETHOD(InitNew)(){ 
		m_pDoc->SetBtnCnt(0); 
		return S_OK; 
	} 
 
	STDMETHOD(Save)(LPSTREAM pStm, BOOL fClearDirty) 
	{ 
		int i; 
		LONG cbWrite; 
		int nBtnCnt; 
		int nSecReadSize; 
 
		if(fClearDirty == FALSE){ 
			return S_OK; 
		} 
 
		IPersistStreamInitImpl::Save(pStm,fClearDirty); 
 
		nBtnCnt = m_pDoc->GetBtnCnt(); 
		pStm->Write(&nBtnCnt,sizeof(int),NULL); 
		//Store button infomation. 
		for(i=0;iWrite(m_pDoc->GetButton(i),sizeof(TBItem),(ULONG*)&cbWrite); 
		} 
 
		//Store icons. 
		for(i=0;iGetButton(i)->IconSize > 0){ 
				m_picHolder.SetPictureDispatch(m_pDoc->GetButton(i)->Icon); 
				 
				cbWrite = 0; 
				m_picHolder.m_pPict->SaveAsFile(pStm,FALSE,&cbWrite); 
				//For Test 
				//char arrDisp[20]; 
				//itoa(cbWrite,arrDisp,10); 
				//::MessageBox(NULL,arrDisp,0,0); 
			} 
			 
			nSecReadSize = m_pDoc->GetButton(i)->ToolTipsSize; 
			if(nSecReadSize>0){ 
				pStm->Write(m_pDoc->GetButton(i)->ToolTips,nSecReadSize,NULL); 
			} 
			 
			nSecReadSize = m_pDoc->GetButton(i)->KeySize; 
			if(nSecReadSize>0){ 
				pStm->Write(m_pDoc->GetButton(i)->Key,nSecReadSize,NULL); 
			} 
		} 
		return S_OK; 
	} 
	STDMETHOD(Load)(LPSTREAM pStm) 
	{ 
		int i; 
		int maxSize=0; 
		int nBtnCnt = 0; 
		int nSecReadSize; 
		IPersistStreamInitImpl::Load(pStm); 
		 
		pStm->Read(&nBtnCnt,sizeof(int),NULL); 
		m_pDoc->SetBtnCnt(nBtnCnt); 
		//为Buttons分配空间 
		if(nBtnCnt > 0){ 
			for(i=0;iRead(m_pDoc->GetButton(i),sizeof(TBItem),NULL); 
 
				if(m_pDoc->GetButton(i)->IconSize > maxSize){ 
					maxSize = m_pDoc->GetButton(i)->IconSize; 
				} 
			} 
 
			HGLOBAL hGlobal=NULL; 
			LPPICTUREDISP lpPicDisp=NULL; 
			LPSTREAM stmPicture; 
			void* pvData; 
 
			if(maxSize>0){ 
				hGlobal = GlobalAlloc(GMEM_MOVEABLE, maxSize);   
			} 
			//Read icon from stream. 
			for(i=0;iGetButton(i)->IconSize; 
				if(nSecReadSize > 0){ 
					//Lock buffer to get the picture data stream. 
					pvData = GlobalLock(hGlobal); 
					pStm->Read(pvData,nSecReadSize,NULL); 
					GlobalUnlock(hGlobal); 
 
					CreateStreamOnHGlobal(hGlobal,FALSE,&stmPicture); 
					OleLoadPicture(stmPicture, 0, FALSE,IID_IPictureDisp, (LPVOID*)&lpPicDisp); 
 
					stmPicture->Release(); 
 
					m_picHolder.SetPictureDispatch(lpPicDisp); 
					m_pDoc->GetButton(i)->Icon = m_picHolder.GetPictureDispatch(); 
					lpPicDisp->Release(); 
				} 
 
				nSecReadSize = m_pDoc->GetButton(i)->ToolTipsSize; 
				if(nSecReadSize>0){ 
					m_pDoc->GetButton(i)->ToolTips = new char[nSecReadSize+1]; 
					pStm->Read(m_pDoc->GetButton(i)->ToolTips,nSecReadSize,NULL); 
					m_pDoc->GetButton(i)->ToolTips[nSecReadSize] = 0; 
				} 
 
				nSecReadSize = m_pDoc->GetButton(i)->KeySize; 
				if(nSecReadSize>0){ 
					m_pDoc->GetButton(i)->Key = new char[nSecReadSize+1]; 
					pStm->Read(m_pDoc->GetButton(i)->Key,nSecReadSize,NULL); 
					m_pDoc->GetButton(i)->Key[nSecReadSize] = 0; 
				} 
			} 
 
			//Release global buffer. 
			if(hGlobal){ 
				GlobalFree(hGlobal); 
			}		 
		} 
		 
		m_pView->CalCoor(); 
		//Draw toolBar. 
		FireViewChange(); 
		return S_OK; 
	} 
 
 
	STDMETHOD(get_Enabled)(VARIANT_BOOL *pbool){		 
		*pbool = BOOL2VBOOL(m_Enabled); 
		return S_OK; 
	} 
	STDMETHOD(put_Enabled)(VARIANT_BOOL pbool){ 
		m_Enabled = VBOOL2BOOL(pbool); 
		return S_OK; 
	} 
	 
	BOOL m_bRequiresSave; //  支持永久性的基类使用 
	BOOL m_bEnabled; 
	CComPtr m_pMouseIcon; 
	LONG m_nMousePointer; 
	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); 
	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); 
	LRESULT OnLButtonUP(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); 
	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); 
 
private: 
 
	BOOL SetBtnState(POINT point, BOOL isCursorInBar); 
 
	MouseState m_MouseLState; 
 
	BOOL m_BtnTrace; 
	BOOL m_MouseCaptured; 
	BOOL m_Enabled; 
	short m_Value; 
	 
	TBItem m_SwapBtn;			//ToolBar Button 
 
	CPictureHolder m_picHolder; 
 
	CToolBarDoc *m_pDoc; 
	CToolBarView *m_pView; 
 
	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
	{ 
		// TODO : Add Code for message handler. Call DefWindowProc if necessary.
		//char arrDisp[100]; 
		//itoa((long)this->m_hWndCD,arrDisp,10); 
		//::MessageBox(NULL,arrDisp,0,0); 
		m_pView->SetPara(this->m_hWndCD, _Module.m_hInst); 
 
		return S_OK; 
	} 
	LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
	{ 
		// TODO : Add Code for message handler. Call DefWindowProc if necessary.
		return DoVerbProperties(NULL, ::GetActiveWindow()); 
	} 
}; 
 
#endif //__KTOOLBARCTRL_H_