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

📄 atldiceobj.h

📁 控件的相用的实现利用MFC实现相拥的控件
💻 H
字号:
// AtlDiceObj.h : Declaration of the CAtlDiceObj

#ifndef __ATLDICEOBJ_H_
#define __ATLDICEOBJ_H_

#include "resource.h"       // main symbols
#include <atlctl.h>

#include "time.h"

#define MAX_DIEFACES 6
extern HBITMAP m_dieBitmaps[MAX_DIEFACES];
extern unsigned short m_nFirstDieValue;
extern unsigned short m_nSecondDieValue;
extern char m_nTimesRolled;

/////////////////////////////////////////////////////////////////////////////
// CAtlDiceObj
class ATL_NO_VTABLE CAtlDiceObj : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public IDispatchImpl<IAtlDiceObj, &IID_IAtlDiceObj, &LIBID_ATLDICELib>,
	public CComControl<CAtlDiceObj>,
	public IPersistStreamInitImpl<CAtlDiceObj>,
	public IOleControlImpl<CAtlDiceObj>,
	public IOleObjectImpl<CAtlDiceObj>,
	public IOleInPlaceActiveObjectImpl<CAtlDiceObj>,
	public IViewObjectExImpl<CAtlDiceObj>,
	public IOleInPlaceObjectWindowlessImpl<CAtlDiceObj>,
	public IConnectionPointContainerImpl<CAtlDiceObj>,
	public IPersistStorageImpl<CAtlDiceObj>,
	public ISpecifyPropertyPagesImpl<CAtlDiceObj>,
	public IQuickActivateImpl<CAtlDiceObj>,
	public IDataObjectImpl<CAtlDiceObj>,
	public IProvideClassInfo2Impl<&CLSID_AtlDiceObj, &DIID__IAtlDiceObjEvents, &LIBID_ATLDICELib>,
	public IPropertyNotifySinkCP<CAtlDiceObj>,
	public CComCoClass<CAtlDiceObj, &CLSID_AtlDiceObj>
{
public:
	short m_nDiceColor;
	short m_nTimestoRoll;
	CAtlDiceObj()
	{
		LoadBitmaps();
		srand((unsigned)time(NULL));
		m_nTimesRolled=0;
		m_nFirstDieValue=(rand()%(MAX_DIEFACES))+1;
		m_nSecondDieValue=(rand()%(MAX_DIEFACES))+1;
	}

DECLARE_REGISTRY_RESOURCEID(IDR_ATLDICEOBJ)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CAtlDiceObj)
	COM_INTERFACE_ENTRY(IAtlDiceObj)
	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)
END_COM_MAP()

BEGIN_PROP_MAP(CAtlDiceObj)
	PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
	PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
	// Example entries
	// PROP_ENTRY("Property Description", dispid, clsid)
	// PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()

BEGIN_CONNECTION_POINT_MAP(CAtlDiceObj)
	CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
END_CONNECTION_POINT_MAP()

BEGIN_MSG_MAP(CAtlDiceObj)
	CHAIN_MSG_MAP(CComControl<CAtlDiceObj>)
	DEFAULT_REFLECTION_HANDLER()
	MESSAGE_HANDLER(WM_TIMER, OnTimer)
	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)

// IAtlDiceObj
public:
	STDMETHOD(get_TimestoRoll)(/*[out, retval]*/ short *pVal);
	STDMETHOD(put_TimestoRoll)(/*[in]*/ short newVal);
	STDMETHOD(get_m_nDiceColor)(/*[out, retval]*/ short *pVal);
	STDMETHOD(put_m_nDiceColor)(/*[in]*/ short newVal);
	STDMETHOD(RollDice)();
	void ShowSecondDieFace(ATL_DRAWINFO& di);
	void ShowFirstDieFace(ATL_DRAWINFO& di);
	BOOL LoadBitmaps();

	HRESULT OnDraw(ATL_DRAWINFO& di)
	{
		ShowFirstDieFace(di);
		ShowSecondDieFace(di);
		/*
		RECT& rc = *(RECT*)di.prcBounds;
		Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);

		SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
		LPCTSTR pszText = _T("ATL 3.0 : AtlDiceObj");
		TextOut(di.hdcDraw, 
			(rc.left + rc.right) / 2, 
			(rc.top + rc.bottom) / 2, 
			pszText, 
			lstrlen(pszText));
	*/
		return S_OK;
	
	}
	LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// TODO : Add Code for message handler. Call DefWindowProc if necessary.
		if(m_nTimesRolled>m_nTimestoRoll){
			m_nTimesRolled=0;
			KillTimer(1);
		}
		else{
			m_nFirstDieValue=(rand()%(MAX_DIEFACES))+1;
			m_nSecondDieValue=(rand()%(MAX_DIEFACES))+1;
			FireViewChange();
			m_nTimesRolled++;
		}
		bHandled=TRUE;
		return 0;
	}
	LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// TODO : Add Code for message handler. Call DefWindowProc if necessary.
		RollDice();
		bHandled=TRUE;		return 0;
	}
};

#endif //__ATLDICEOBJ_H_

⌨️ 快捷键说明

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