ActiveX ñ íóëÿ

         

Ôàéë MyCtrlCtl.cpp


// MyCtrlCtl.cpp : Implementation of the CMyCtrlCtrl ActiveX Control class.

#include "stdafx.h"

#include "MyCtrl.h"

#include "MyCtrlCtl.h"

#include "MyCtrlPpg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

IMPLEMENT_DYNCREATE(CMyCtrlCtrl, COleControl)

/////////////////////////////////////////////////////////////////////////////

// Message map

BEGIN_MESSAGE_MAP(CMyCtrlCtrl, COleControl)

            //{{AFX_MSG_MAP(CMyCtrlCtrl)

            ON_WM_LBUTTONDOWN()



            ON_WM_TIMER()

            ON_WM_CREATE()

            ON_WM_CLOSE()

            //}}AFX_MSG_MAP

            ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// Dispatch map

BEGIN_DISPATCH_MAP(CMyCtrlCtrl, COleControl)

            //{{AFX_DISPATCH_MAP(CMyCtrlCtrl)

            DISP_PROPERTY_EX(CMyCtrlCtrl, "Shape", GetShape, SetShape, VT_I2)

            DISP_PROPERTY_EX(CMyCtrlCtrl, "Selected", GetSelected, SetNotSupported, VT_BOOL)

            DISP_PROPERTY_EX(CMyCtrlCtrl, "SelectColor", GetSelectColor, SetSelectColor, VT_COLOR)

            DISP_PROPERTY_EX(CMyCtrlCtrl, "TextColor", GetTextColor, SetTextColor, VT_COLOR)

            DISP_PROPERTY_EX(CMyCtrlCtrl, "Sleep", GetSleep, SetSleep, VT_I4)

            DISP_FUNCTION(CMyCtrlCtrl, "DoChangeTimer", DoChangeTimer, VT_BOOL, VTS_NONE)

            DISP_DEFVALUE(CMyCtrlCtrl, "Shape")

            DISP_STOCKPROP_FORECOLOR()

            DISP_STOCKPROP_CAPTION()

            DISP_STOCKPROP_FONT()

            //}}AFX_DISPATCH_MAP

            DISP_FUNCTION_ID(CMyCtrlCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)

END_DISPATCH_MAP()

/////////////////////////////////////////////////////////////////////////////


// Event map

BEGIN_EVENT_MAP(CMyCtrlCtrl, COleControl)

            //{{AFX_EVENT_MAP(CMyCtrlCtrl)

            EVENT_CUSTOM("Select", FireSelect, VTS_BOOL)

            EVENT_CUSTOM("Tick", FireTick, VTS_I4)

            //}}AFX_EVENT_MAP

END_EVENT_MAP()

/////////////////////////////////////////////////////////////////////////////

// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!

BEGIN_PROPPAGEIDS(CMyCtrlCtrl, 3)

            PROPPAGEID(CMyCtrlPropPage::guid)

            PROPPAGEID(CLSID_CColorPropPage)

            PROPPAGEID(CLSID_CFontPropPage)

END_PROPPAGEIDS(CMyCtrlCtrl)

/////////////////////////////////////////////////////////////////////////////

// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CMyCtrlCtrl, "MYCTRL.MyCtrlCtrl.1",

            0x49e2f37f, 0xb48d, 0x11d3, 0xa2, 0xb0, 0, 0xa0, 0xc9, 0x55, 0xfa, 0x9e)

/////////////////////////////////////////////////////////////////////////////

// Type library ID and version

IMPLEMENT_OLETYPELIB(CMyCtrlCtrl, _tlid, _wVerMajor, _wVerMinor)

/////////////////////////////////////////////////////////////////////////////

// Interface IDs

const IID BASED_CODE IID_DMyCtrl =

                        { 0x49e2f37d, 0xb48d, 0x11d3, { 0xa2, 0xb0, 0, 0xa0, 0xc9, 0x55, 0xfa, 0x9e } };

const IID BASED_CODE IID_DMyCtrlEvents =

                        { 0x49e2f37e, 0xb48d, 0x11d3, { 0xa2, 0xb0, 0, 0xa0, 0xc9, 0x55, 0xfa, 0x9e } };

/////////////////////////////////////////////////////////////////////////////

// Control type information

static const DWORD BASED_CODE _dwMyCtrlOleMisc =

            OLEMISC_ACTIVATEWHENVISIBLE |

            OLEMISC_SETCLIENTSITEFIRST |

            OLEMISC_INSIDEOUT |

            OLEMISC_CANTLINKINSIDE |

            OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CMyCtrlCtrl, IDS_MYCTRL, _dwMyCtrlOleMisc)



/////////////////////////////////////////////////////////////////////////////

// CMyCtrlCtrl::CMyCtrlCtrlFactory::UpdateRegistry -

// Adds or removes system registry entries for CMyCtrlCtrl

BOOL CMyCtrlCtrl::CMyCtrlCtrlFactory::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_MYCTRL,

                                    IDB_MYCTRL,

                                    afxRegApartmentThreading,

                                    _dwMyCtrlOleMisc,

                                    _tlid,

                                    _wVerMajor,

                                    _wVerMinor);

            else

                        return AfxOleUnregisterClass(m_clsid, m_lpszProgID);

}

/////////////////////////////////////////////////////////////////////////////

// CMyCtrlCtrl::CMyCtrlCtrl - Constructor

CMyCtrlCtrl::CMyCtrlCtrl()

{

            InitializeIIDs(&IID_DMyCtrl, &IID_DMyCtrlEvents);

            m_bSelected = FALSE;

            m_nShape = 0;

            SetForeColor(RGB(0,255,0));

            m_timeSleep = 1000;

            m_tick = 0;

}

/////////////////////////////////////////////////////////////////////////////

// CMyCtrlCtrl::~CMyCtrlCtrl - Destructor

CMyCtrlCtrl::~CMyCtrlCtrl()

{

}

/////////////////////////////////////////////////////////////////////////////



// CMyCtrlCtrl::OnDraw - Drawing function

void CMyCtrlCtrl::OnDraw(

                                    CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)

{

            CPen pen;

            CBrush foreBrush, backBrush;

            CPoint points[3];

            pdc->SaveDC();

            pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));

            ULONG foreColor = TranslateColor(GetForeColor());

            backBrush.CreateSolidBrush(TranslateColor(AmbientBackColor()));

            foreBrush.CreateSolidBrush(GetSelected() ? foreColor : TranslateColor(m_selectColor));

            pdc->FillRect(rcBounds, &backBrush);

            pdc->SelectObject(&pen);

            pdc->SelectObject(&foreBrush);

            switch (m_nShape)

            {

            case 0:

                        pdc->Ellipse(rcBounds);

                        break;

            case 1:

                        pdc->Rectangle(rcBounds);

                        break;

            case 2:

                        points[0].x = rcBounds.left;

                        points[0].y = rcBounds.bottom - 1;

                        points[1].x = (rcBounds.left + rcBounds.right - 1) / 2;

                        points[1].y = rcBounds.top;

                        points[2].x = rcBounds.right - 1;

                        points[2].y = rcBounds.bottom - 1;

                        pdc->Polygon(points, 3);

                        break;

            }

            CFont *pOldFont;

            TEXTMETRIC tm;

           

            pdc->SetTextColor(TranslateColor(m_textColor));

            pdc->SetBkMode(TRANSPARENT);

           

            CString strCaption = GetText();

            pOldFont = SelectStockFont(pdc);

            pdc->GetTextMetrics(&tm);

            pdc->SetTextAlign(TA_CENTER|TA_TOP);

            pdc->ExtTextOut((rcBounds.left + rcBounds.right)/2,(rcBounds.top + rcBounds.bottom - tm.tmHeight)/2,



                                                            ETO_CLIPPED, rcBounds, strCaption, strCaption.GetLength(),NULL);

           

            CString strTick;

            strTick.Format("%d",m_tick);

            pdc->ExtTextOut((rcBounds.left + rcBounds.right)/2,(rcBounds.top + tm.tmHeight),

                                                            ETO_CLIPPED, rcBounds, strTick, strTick.GetLength(),NULL);

           

            pdc->RestoreDC(-1);

}

/////////////////////////////////////////////////////////////////////////////

// CMyCtrlCtrl::DoPropExchange - Persistence support

void CMyCtrlCtrl::DoPropExchange(CPropExchange* pPX)

{

            ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));

            COleControl::DoPropExchange(pPX);

            PX_Short(pPX, _T("Shape"), m_nShape, 0);

            PX_Long(pPX, _T("SelectColor"), (long&) m_selectColor, RGB(255,0,0));

            PX_Long(pPX, _T("TextColor"), (long&) m_textColor, RGB(0,0,0));

            PX_Long(pPX, _T("Sleep"),  m_timeSleep, 1000);

}

/////////////////////////////////////////////////////////////////////////////

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

void CMyCtrlCtrl::OnResetState()

{

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

            // TODO: Reset any other control state here.

}

/////////////////////////////////////////////////////////////////////////////

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

void CMyCtrlCtrl::AboutBox()

{

            CDialog dlgAbout(IDD_ABOUTBOX_MYCTRL);

            dlgAbout.DoModal();

}

/////////////////////////////////////////////////////////////////////////////

// CMyCtrlCtrl message handlers

short CMyCtrlCtrl::GetShape()

{

            return m_nShape;



}

void CMyCtrlCtrl::SetShape(short nNewValue)

{

            m_nShape = nNewValue;

            SetModifiedFlag();

            InvalidateControl();

}

BOOL CMyCtrlCtrl::GetSelected()

{

            return m_bSelected;

}

void CMyCtrlCtrl::OnLButtonDown( UINT nFlags, CPoint point)

{

            COleControl::OnLButtonDown(nFlags, point);

            m_bSelected = !m_bSelected;

            InvalidateControl();

            FireSelect(m_bSelected);

}

OLE_COLOR CMyCtrlCtrl::GetSelectColor()

{

            return m_selectColor;

}

void CMyCtrlCtrl::SetSelectColor(OLE_COLOR nNewValue)

{

            m_selectColor = nNewValue;

            SetModifiedFlag();

            InvalidateControl();

}

OLE_COLOR CMyCtrlCtrl::GetTextColor()

{

            return m_textColor;

}

void CMyCtrlCtrl::SetTextColor(OLE_COLOR nNewValue)

{

            m_textColor = nNewValue;

            SetModifiedFlag();

            InvalidateControl();

}

long CMyCtrlCtrl::GetSleep()

{

            return m_timeSleep;

}

void CMyCtrlCtrl::SetSleep(long nNewValue)

{

            m_timeSleep = nNewValue;

            SetModifiedFlag();

}

void CMyCtrlCtrl::OnTimer(UINT nIDEvent)

{

            m_tick++;                   

            FireTick(m_tick);

            SetModifiedFlag();

            InvalidateControl();

           

            COleControl::OnTimer(nIDEvent);

}

#define ID_TIMER    1001

int CMyCtrlCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

            if (COleControl::OnCreate(lpCreateStruct) == -1)

                        return -1;

           

            idTimer = SetTimer( ID_TIMER, m_timeSleep, NULL );

            timerOn = FALSE;    

            return 0;

}

void CMyCtrlCtrl::OnClose()

{

            if ( timerOn )

            {

                        KillTimer( idTimer );

                        timerOn =FALSE;     

            }

            CWnd::OnClose();

}

BOOL CMyCtrlCtrl::DoChangeTimer()

{

            if ( timerOn )

            {

                        KillTimer( idTimer );

                        timerOn =FALSE;     

            }

            else

            {

                        idTimer = SetTimer( ID_TIMER, m_timeSleep, NULL );

                        timerOn =TRUE;       

            }

            return timerOn;

}


Ñîäåðæàíèå ðàçäåëà