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

📄 pjctl.cpp

📁 Visual C++程序设计技巧与实例
💻 CPP
字号:
// PJCtl.cpp : Implementation of the CPJCtrl ActiveX Control class.

#include "stdafx.h"
#include "PJ.h"
#include "PJCtl.h"
#include "PJPpg.h"


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


IMPLEMENT_DYNCREATE(CPJCtrl, COleControl)


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

BEGIN_MESSAGE_MAP(CPJCtrl, COleControl)
	//{{AFX_MSG_MAP(CPJCtrl)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


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

BEGIN_DISPATCH_MAP(CPJCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CPJCtrl)
	DISP_PROPERTY_EX(CPJCtrl, "index", GetIndex, SetIndex, VT_I2)
	DISP_PROPERTY_EX(CPJCtrl, "IsBK", GetIsBK, SetIsBK, VT_BOOL)
	DISP_PROPERTY_EX(CPJCtrl, "CanMove", GetCanMove, SetCanMove, VT_BOOL)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CPJCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


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

BEGIN_EVENT_MAP(CPJCtrl, COleControl)
	//{{AFX_EVENT_MAP(CPJCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


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

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


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

IMPLEMENT_OLECREATE_EX(CPJCtrl, "PJ.PJCtrl.1",
	0x6f943bb3, 0xb9c1, 0x4e45, 0x8d, 0xe1, 0x4d, 0x8e, 0x8b, 0xab, 0xb8, 0x84)


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

IMPLEMENT_OLETYPELIB(CPJCtrl, _tlid, _wVerMajor, _wVerMinor)


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

const IID BASED_CODE IID_DPJ =
		{ 0xd9d3f9b1, 0xa916, 0x4be9, { 0x9b, 0xbf, 0xe6, 0xfd, 0xa7, 0xf5, 0x8d, 0xf5 } };
const IID BASED_CODE IID_DPJEvents =
		{ 0x3a484d0b, 0x19dd, 0x4fcb, { 0x83, 0x5a, 0x17, 0x20, 0x38, 0xac, 0x93, 0xa7 } };


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

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

IMPLEMENT_OLECTLTYPE(CPJCtrl, IDS_PJ, _dwPJOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl::CPJCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CPJCtrl

BOOL CPJCtrl::CPJCtrlFactory::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
	// afxRegInsertable | afxRegApartmentThreading to afxRegInsertable.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_PJ,
			IDB_PJ,
			afxRegInsertable | afxRegApartmentThreading,
			_dwPJOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl::CPJCtrl - Constructor

CPJCtrl::CPJCtrl()
{
	InitializeIIDs(&IID_DPJ, &IID_DPJEvents);

	// TODO: Initialize your control's instance data here.
	SetInitialSize(45,100);
	m_nIndex = 0;
	m_bBK = FALSE;
	m_bCanMove = TRUE;
	m_bMoving = FALSE;
}


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl::~CPJCtrl - Destructor

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


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl::OnDraw - Drawing function

void CPJCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
//	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
//	pdc->Ellipse(rcInvalid);

	CBitmap *pOldBitmap;
	CBitmap bitmap;	

	CDC ppdc;
	ppdc.CreateCompatibleDC(pdc);
	if(m_bBK)//背景图案
	{
		bitmap.LoadBitmap(IDB_BK);		
	}
	else//前景图案	
		bitmap.LoadBitmap(IDB_BITMAP1+m_nIndex);
	pOldBitmap=ppdc.SelectObject(&bitmap);	
	pdc->BitBlt(0,0,rcBounds.Width(),rcBounds.Height(),&ppdc,0,0,SRCCOPY);
	ppdc.SelectObject(pOldBitmap);

}


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl::DoPropExchange - Persistence support

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

	// TODO: Call PX_ functions for each persistent custom property.
    PX_Short(pPX,"m_nIndex",m_nIndex,0);
	PX_Bool(pPX,"m_bBK",m_bBK,FALSE);
	PX_Bool(pPX,"m_bCanMove",m_bCanMove,TRUE);
}


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

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

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


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

void CPJCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_PJ);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CPJCtrl message handlers

short CPJCtrl::GetIndex() 
{
	return m_nIndex;
}

void CPJCtrl::SetIndex(short nNewValue) 
{
	m_nIndex = nNewValue;
	SetModifiedFlag();
	InvalidateControl();
}



BOOL CPJCtrl::GetIsBK() 
{
	return m_bBK;
}

void CPJCtrl::SetIsBK(BOOL bNewValue) 
{
	m_bBK = bNewValue;
	SetModifiedFlag();
	InvalidateControl();
}

BOOL CPJCtrl::GetCanMove() 
{
	return m_bCanMove;
}

void CPJCtrl::SetCanMove(BOOL bNewValue) 
{
	m_bCanMove = bNewValue;
	SetModifiedFlag();
	InvalidateControl();
}


void CPJCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_bCanMove)   //是否允许拖动
	{
		m_bMoving = TRUE;
		//捕获鼠标
		SetCapture();
		//得到鼠标(相对于控件左上角)的坐标
		m_ptX = point.x;
		m_ptY = point.y;

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

		//得到相对量
		MoveWindow(0,0,45,100);
		GetClientRect(&m_rectOld);
		::ClientToScreen(m_hWnd,&m_rectOld.TopLeft());
	}

	COleControl::OnLButtonDown(nFlags, point);
}

void CPJCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_bMoving)
	{
		ReleaseCapture();
		m_bMoving = FALSE;
	}

	COleControl::OnLButtonUp(nFlags, point);
}

void CPJCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
	CPoint pt = point;
	if(m_bMoving)
	{
		ClientToScreen(&pt);
        MoveWindow(pt.x-m_ptX-m_rectOld.left,pt.y-m_ptY-m_rectOld.top,80,120);
	}

	COleControl::OnMouseMove(nFlags, point);
}



⌨️ 快捷键说明

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