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

📄 cardsctl.cpp

📁 此控件有五十四张牌
💻 CPP
字号:
// CardsCtl.cpp : Implementation of the CCardsCtrl ActiveX Control class.

#include "stdafx.h"
#include "Cards.h"
#include "CardsCtl.h"
#include "CardsPpg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CCardsCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CCardsCtrl, COleControl)
	//{{AFX_MSG_MAP(CCardsCtrl)
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_MENUITEM_ABOUT, OnMenuitemAbout)
	ON_UPDATE_COMMAND_UI(ID_MENUITEM_ABOUT, OnUpdateMenuitemAbout)
	ON_COMMAND(ID_MENUITEM_HELP, OnMenuitemHelp)
	ON_UPDATE_COMMAND_UI(ID_MENUITEM_HELP, OnUpdateMenuitemHelp)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CCardsCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CCardsCtrl)
	DISP_PROPERTY_EX(CCardsCtrl, "value", GetValue, SetValue, VT_I2)
	DISP_PROPERTY_EX(CCardsCtrl, "background", GetBackground, SetBackground, VT_BOOL)
	DISP_PROPERTY_EX(CCardsCtrl, "backbmp", GetBackbmp, SetBackbmp, VT_I2)
	DISP_PROPERTY_EX(CCardsCtrl, "CancelPopup", GetCancelPopup, SetCancelPopup, VT_BOOL)
	DISP_PROPERTY_EX(CCardsCtrl, "IsMove", GetIsMove, SetIsMove, VT_BOOL)
	DISP_PROPERTY_EX(CCardsCtrl, "IsGoHome", GetIsGoHome, SetIsGoHome, VT_BOOL)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CCardsCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CCardsCtrl, COleControl)
	//{{AFX_EVENT_MAP(CCardsCtrl)
	EVENT_STOCK_CLICK()
	EVENT_STOCK_DBLCLICK()
	EVENT_STOCK_MOUSEDOWN()
	EVENT_STOCK_MOUSEMOVE()
	EVENT_STOCK_MOUSEUP()
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CCardsCtrl, 1)
	PROPPAGEID(CCardsPropPage::guid)
END_PROPPAGEIDS(CCardsCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CCardsCtrl, "CARDS.CardsCtrl.1",
	0xf968806, 0xd214, 0x11d5, 0x90, 0x22, 0x52, 0x54, 0xab, 0x12, 0x3a, 0x61)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CCardsCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DCards =
		{ 0xf968804, 0xd214, 0x11d5, { 0x90, 0x22, 0x52, 0x54, 0xab, 0x12, 0x3a, 0x61 } };
const IID BASED_CODE IID_DCardsEvents =
		{ 0xf968805, 0xd214, 0x11d5, { 0x90, 0x22, 0x52, 0x54, 0xab, 0x12, 0x3a, 0x61 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwCardsOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CCardsCtrl, IDS_CARDS, _dwCardsOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::CCardsCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CCardsCtrl

BOOL CCardsCtrl::CCardsCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_CARDS,
			IDB_CARDS,
			afxRegApartmentThreading,
			_dwCardsOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::CCardsCtrl - Constructor

CCardsCtrl::CCardsCtrl()
{
	InitializeIIDs(&IID_DCards, &IID_DCardsEvents);
	value=1;backbmp=1;
	IsMove=false;m_IsMove=false;IsGoHome=true;
	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::~CCardsCtrl - Destructor

CCardsCtrl::~CCardsCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::OnDraw - Drawing function

void CCardsCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	//让控件里显示扑克牌的图形
	CBitmap *pOldBitmap;
	CBitmap bitmap;
	CRect rect;
	GetClientRect(rect);
	CDC ppdc;
	CClientDC dc(this);
	ppdc.CreateCompatibleDC(&dc);
	if(background)//背景图案
	{
		if(backbmp==1)bitmap.LoadBitmap(255);
		if(backbmp==2)bitmap.LoadBitmap(256);
	}
	else//前景图案
		bitmap.LoadBitmap(IDB_BITMAP1+value-1);
	pOldBitmap=ppdc.SelectObject(&bitmap);
	dc.BitBlt(0,0,rect.Width(),rect.Height(),&ppdc,0,0,SRCCOPY);
	ppdc.SelectObject(pOldBitmap);
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::DoPropExchange - Persistence support

void CCardsCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::OnResetState - Reset control to default state

void CCardsCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl::AboutBox - Display an "About" box to the user

void CCardsCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_CARDS);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CCardsCtrl message handlers

short CCardsCtrl::GetValue() 
{
	// TODO: Add your property handler here
	return value;
}

void CCardsCtrl::SetValue(short nNewValue) 
{
	// TODO: Add your property handler here
	value=nNewValue;
	SetModifiedFlag();
	Invalidate();//立即刷新
}

BOOL CCardsCtrl::GetBackground() 
{
	// TODO: Add your property handler here
	return background;
}

void CCardsCtrl::SetBackground(BOOL bNewValue) 
{
	// TODO: Add your property handler here
	background=bNewValue;
	SetModifiedFlag();
	Invalidate();
}

short CCardsCtrl::GetBackbmp() 
{
	// TODO: Add your property handler here
	return backbmp;
}

void CCardsCtrl::SetBackbmp(short nNewValue) 
{
	// TODO: Add your property handler here
	backbmp=nNewValue;
	SetModifiedFlag();
	Invalidate();
}

void CCardsCtrl::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	if(!CancelPopup)
	{
		CMenu menu1;
		menu1.LoadMenu(IDR_MENU1);
		menu1.GetSubMenu(0)->TrackPopupMenu(TPM_CENTERALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,point.x,point.y,this);
	}
}

void CCardsCtrl::OnMenuitemAbout() 
{
	// TODO: Add your command handler code here
	MessageBox("控件制作:龙浩天\r\nE_mail:cyuyan@sohu.com","54张牌的扑克控件1.0");
}

void CCardsCtrl::OnUpdateMenuitemAbout(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}

void CCardsCtrl::OnMenuitemHelp() 
{
	// TODO: Add your command handler code here
	MessageBox("此控件有五十四张牌,包括了大小王。能够处\r\n理的事件有单击和双击。如果你在使用中觉\r\n得有什么不足之处,请通知我。","54张牌的扑克控件1.0");
}

void CCardsCtrl::OnUpdateMenuitemHelp(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}

BOOL CCardsCtrl::GetCancelPopup() 
{
	// TODO: Add your property handler here
	return CancelPopup;
}

void CCardsCtrl::SetCancelPopup(BOOL bNewValue) 
{
	// TODO: Add your property handler here
	CancelPopup=bNewValue;
	SetModifiedFlag();
	Invalidate();
}

void CCardsCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(IsMove)//属性框中设置的是否是允许拖动鼠标
	{
		m_IsMove=true;
		SetCapture();//捕获鼠标
		xx=point.x;//得到鼠标(相对于控件左上角)的坐标
		yy=point.y;

		GetClientRect(&rect);//保存拖动前的扑克位置
		::ClientToScreen(m_hWnd,&rect.TopLeft());
		OldPoint=rect.TopLeft();

		MoveWindow(0,0,71,96);//得到相对量
		GetClientRect(&rect);
		::ClientToScreen(m_hWnd,&rect.TopLeft());
	}
	COleControl::OnLButtonDown(nFlags, point);
}

void CCardsCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CPoint MyPoint=point;
	if(m_IsMove)
	{
		ClientToScreen(&MyPoint);
        MoveWindow(MyPoint.x-xx-rect.left,MyPoint.y-yy-rect.top,71,96);
	}
	COleControl::OnMouseMove(nFlags, point);
}

void CCardsCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_IsMove)
	{
		ReleaseCapture();
		m_IsMove=false;
		if(IsGoHome)
		    MoveWindow(OldPoint.x-rect.left,OldPoint.y-rect.top,71,96);//还原到扑克牌拖动前的位置
	}
	COleControl::OnLButtonUp(nFlags, point);
}


BOOL CCardsCtrl::GetIsMove() 
{
	// TODO: Add your property handler here
	return IsMove;
}

void CCardsCtrl::SetIsMove(BOOL bNewValue) 
{
	// TODO: Add your property handler here
	IsMove=bNewValue;
	SetModifiedFlag();
	Invalidate();
}

BOOL CCardsCtrl::GetIsGoHome() 
{
	// TODO: Add your property handler here

	return IsGoHome;
}

void CCardsCtrl::SetIsGoHome(BOOL bNewValue) 
{
	// TODO: Add your property handler here
	IsGoHome=bNewValue;
	SetModifiedFlag();
	Invalidate();
}

⌨️ 快捷键说明

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