?? dicectl.cpp
字號:
// DiceCtl.cpp : Implementation of the CDiceCtrl ActiveX Control class.
#include "stdafx.h"
#include "dice.h"
#include "DiceCtl.h"
#include "DicePpg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIMER_ROLLING 1
#define TIMER_UPDATE 2
IMPLEMENT_DYNCREATE(CDiceCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CDiceCtrl, COleControl)
//{{AFX_MSG_MAP(CDiceCtrl)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(OCM_COMMAND, OnOcmCommand)
// ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CDiceCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CDiceCtrl)
DISP_PROPERTY_NOTIFY(CDiceCtrl, "RollingInterval", m_rollingInterval, OnRollingIntervalChanged, VT_I4)
DISP_PROPERTY_NOTIFY(CDiceCtrl, "BallColor", m_ballColor, OnBallColorChanged, VT_COLOR)
DISP_PROPERTY_NOTIFY(CDiceCtrl, "PanelColor", m_panelColor, OnPanelColorChanged, VT_COLOR)
DISP_FUNCTION(CDiceCtrl, "StartRolling", StartRolling, VT_EMPTY, VTS_I2)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CDiceCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CDiceCtrl, COleControl)
//{{AFX_EVENT_MAP(CDiceCtrl)
EVENT_CUSTOM("FinishRolling", FireFinishRolling, VTS_I2)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CDiceCtrl, 1)
PROPPAGEID(CDicePropPage::guid)
END_PROPPAGEIDS(CDiceCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CDiceCtrl, "DICE.DiceCtrl.1",
0xb2e07673, 0xf099, 0x47c1, 0x90, 0xcb, 0x71, 0x72, 0x69, 0x1, 0x7e, 0xa1)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CDiceCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DDice =
{ 0x5823d2f6, 0x2688, 0x49f9, { 0x81, 0xbf, 0xf2, 0xc5, 0x82, 0x4e, 0xe3, 0x92 } };
const IID BASED_CODE IID_DDiceEvents =
{ 0x43589c22, 0xdca3, 0x4028, { 0x97, 0x9c, 0x6f, 0xe5, 0xd6, 0x60, 0xe2, 0x12 } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwDiceOleMisc =
OLEMISC_SIMPLEFRAME |
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CDiceCtrl, IDS_DICE, _dwDiceOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::CDiceCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CDiceCtrl
BOOL CDiceCtrl::CDiceCtrlFactory::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_DICE,
IDB_DICE,
afxRegApartmentThreading,
_dwDiceOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::CDiceCtrl - Constructor
CDiceCtrl::CDiceCtrl()
{
InitializeIIDs(&IID_DDice, &IID_DDiceEvents);
EnableSimpleFrame();
// TODO: Initialize your control's instance data here.
rolling=FALSE;
timeStart=0;
srand(time(NULL));
m_ballColor = 0x00FF00;
m_rollingInterval = 500;
m_panelColor = 0xFF00FF;
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::~CDiceCtrl - Destructor
CDiceCtrl::~CDiceCtrl()
{
// TODO: Cleanup your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::OnDraw - Drawing function
void CDiceCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (rolling) //如果骰子正在滾動,重新繪制控件
UpdatePicture();
else //否則調用系統函數
DoSuperclassPaint(pdc, rcBounds);
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::DoPropExchange - Persistence support
void CDiceCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::OnResetState - Reset control to default state
void CDiceCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::AboutBox - Display an "About" box to the user
void CDiceCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_DICE);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::PreCreateWindow - Modify parameters for CreateWindowEx
BOOL CDiceCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass = _T("BUTTON");
return COleControl::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::IsSubclassedControl - This is a subclassed control
BOOL CDiceCtrl::IsSubclassedControl()
{
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl::OnOcmCommand - Handle command messages
LRESULT CDiceCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
{
#ifdef _WIN32
WORD wNotifyCode = HIWORD(wParam);
#else
WORD wNotifyCode = HIWORD(lParam);
#endif
// TODO: Switch on wNotifyCode here.
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CDiceCtrl message handlers
void CDiceCtrl::OnRollingIntervalChanged()
{
// TODO: Add notification handler code
if (m_rollingInterval<0 || m_rollingInterval>m_rollingSpan)
m_rollingInterval=m_rollingSpan;
SetModifiedFlag();
}
void CDiceCtrl::OnBallColorChanged()
{
// TODO: Add notification handler code
if (m_panelColor<0 || m_panelColor>0xFFFFFF)
m_panelColor=RGB(255,255,255);
SetModifiedFlag();
}
void CDiceCtrl::OnPanelColorChanged()
{
// TODO: Add notification handler code
if (m_panelColor<0 || m_panelColor>0xFFFFFF)
m_panelColor=RGB(0,0,0);
SetModifiedFlag();
}
void CDiceCtrl::StartRolling(short max)
{
// TODO: Add your dispatch handler code here
if (max<1 || max>100) max=3;
m_rollingSpan=(rand()%max)*1000+1000;
rolling=TRUE;
KillTimer(TIMER_ROLLING);
KillTimer(TIMER_UPDATE);
SetTimer(TIMER_ROLLING,m_rollingSpan,NULL);
SetTimer(TIMER_UPDATE,m_rollingInterval,NULL);
}
void CDiceCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
time(&timeStart);
COleControl::OnLButtonDown(nFlags, point);
}
void CDiceCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
time_t time_end;
time(&time_end);
m_rollingSpan=(time_end-timeStart)*2000+1000;
rolling=TRUE;
KillTimer(TIMER_ROLLING);
KillTimer(TIMER_UPDATE);
SetTimer(TIMER_ROLLING,m_rollingSpan,NULL);
SetTimer(TIMER_UPDATE,m_rollingInterval,NULL);
COleControl::OnLButtonUp(nFlags, point);
}
void CDiceCtrl::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent==TIMER_UPDATE)
{
UpdateState();
UpdatePicture();
}
if (nIDEvent==TIMER_ROLLING)
{
KillTimer(TIMER_UPDATE);
KillTimer(TIMER_ROLLING);
rolling=FALSE;
FireFinishRolling(state);
}
COleControl::OnTimer(nIDEvent);
}
void CDiceCtrl::UpdatePicture(void)
{
CDC* dc=GetDC();
int oldDC=dc->SaveDC();
RECT rect;
GetClientRect(&rect);
dc->SelectObject(&CPen(PS_SOLID,1,dc->GetBkColor()));
dc->SelectObject(&CBrush(m_panelColor));
dc->Rectangle(&rect);
int cx=(rect.right-rect.left)/3;
int cy=(rect.bottom-rect.top)/3;
if (cx<cy) cy=cx;
else cx=cy;
POINT center;
center.x=(rect.left+rect.right)/2;
center.y=(rect.top+rect.bottom)/2;
dc->SelectObject(&CPen(PS_SOLID,1,m_ballColor&m_panelColor));
dc->SelectObject(&CBrush(m_ballColor));
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
{
if (!index[i][j]) continue;
RECT rect1;
rect1.left=center.x+(j*2-3)*cx/2;
rect1.right=center.x+(j*2-1)*cx/2;
rect1.top=center.y+(i*2-3)*cy/2;
rect1.bottom=center.y+(i*2-1)*cy/2;
dc->Ellipse(&rect1);
}
dc->RestoreDC(oldDC);
ReleaseDC(dc);
}
void CDiceCtrl::UpdateState()
{
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
index[i][j]=FALSE;
state=rand()%6+1;
if (state>1)
{
index[0][0]=TRUE;
index[2][2]=TRUE;
}
if (state>7)
{
index[0][1]=TRUE;
index[2][1]=TRUE;
}
if (state>3)
{
index[0][2]=TRUE;
index[2][0]=TRUE;
}
if (state>5)
{
index[1][0]=TRUE;
index[1][2]=TRUE;
}
if (state%2==1)
index[1][1]=TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -