我是按照书上说的,做了一个程序,准备生成ActiveX控件。
结果编译总过不去。
不知道为什么?

解决方案 »

  1.   

    没人知道么?
    csdn不是高手很多么?
      

  2.   

    这里的宏,最好不要完全用手写代码,尽量用VC的Wizard来生成,能保证正确性。
      

  3.   

    // ClockCtl.cpp : Implementation of the CClockCtrl ActiveX Control class.#include "stdafx.h"
    #include "Clock.h"
    #include "ClockCtl.h"
    #include "ClockPpg.h"
    #include "math.h"
    #define PI 3.1415926535897931#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    IMPLEMENT_DYNCREATE(CClockCtrl, COleControl)
    /////////////////////////////////////////////////////////////////////////////
    // Message mapBEGIN_MESSAGE_MAP(CClockCtrl, COleControl)
    //{{AFX_MSG_MAP(CClockCtrl)
    ON_WM_CREATE()
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
    ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Dispatch mapBEGIN_DISPATCH_MAP(CClockCtrl, COleControl)
    //{{AFX_DISPATCH_MAP(CClockCtrl)
    DISP_PROPERTY_NOTIFY(CClockCtrl, "hourPosColor", m_hourPosColor, OnHourPosColorChanged, VT_COLOR)
    DISP_STOCKPROP_BACKCOLOR()
    //}}AFX_DISPATCH_MAP
    DISP_FUNCTION_ID(CClockCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
    END_DISPATCH_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Event mapBEGIN_EVENT_MAP(CClockCtrl, COleControl)
    //{{AFX_EVENT_MAP(CClockCtrl)
    // 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(CClockCtrl, 1)
    PROPPAGEID(CClockPropPage::guid)
    PROPPAGEID(CLSID_CColorPropPage)
    END_PROPPAGEIDS(CClockCtrl)
    /////////////////////////////////////////////////////////////////////////////
    // Initialize class factory and guidIMPLEMENT_OLECREATE_EX(CClockCtrl, "CLOCK.ClockCtrl.1",
    0x2d0e5ca7, 0xb34c, 0x11d5, 0x85, 0x82, 0, 0, 0xe8, 0x80, 0xb7, 0xc)
    /////////////////////////////////////////////////////////////////////////////
    // Type library ID and versionIMPLEMENT_OLETYPELIB(CClockCtrl, _tlid, _wVerMajor, _wVerMinor)
    /////////////////////////////////////////////////////////////////////////////
    // Interface IDsconst IID BASED_CODE IID_DClock =
    { 0x2d0e5ca5, 0xb34c, 0x11d5, { 0x85, 0x82, 0, 0, 0xe8, 0x80, 0xb7, 0xc } };
    const IID BASED_CODE IID_DClockEvents =
    { 0x2d0e5ca6, 0xb34c, 0x11d5, { 0x85, 0x82, 0, 0, 0xe8, 0x80, 0xb7, 0xc } };
    /////////////////////////////////////////////////////////////////////////////
    // Control type informationstatic const DWORD BASED_CODE _dwClockOleMisc =
    OLEMISC_ACTIVATEWHENVISIBLE |
    OLEMISC_SETCLIENTSITEFIRST |
    OLEMISC_INSIDEOUT |
    OLEMISC_CANTLINKINSIDE |
    OLEMISC_RECOMPOSEONRESIZE;IMPLEMENT_OLECTLTYPE(CClockCtrl, IDS_CLOCK, _dwClockOleMisc)
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::CClockCtrlFactory::UpdateRegistry -
    // Adds or removes system registry entries for CClockCtrlBOOL CClockCtrl::CClockCtrlFactory::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_CLOCK,
    IDB_CLOCK,
    afxRegApartmentThreading,
    _dwClockOleMisc,
    _tlid,
    _wVerMajor,
    _wVerMinor);
    else
    return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::CClockCtrl - ConstructorCClockCtrl::CClockCtrl()
    {
    InitializeIIDs(&IID_DClock, &IID_DClockEvents); // TODO: Initialize your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::~CClockCtrl - DestructorCClockCtrl::~CClockCtrl()
    {
    // TODO: Cleanup your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::OnDraw - Drawing functionvoid CClockCtrl::OnDraw(
    CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    // TODO: Replace the following code with your own drawing code.
    //画背景
    CBrush brush(TranslateColor(GetBackColor()));
    pdc->FillRect(rcBounds, &brush);

    //计算表盘中心和半径
    int r,xc,yc;
    if(rcBounds.Width ()>rcBounds.Height ())
    r=rcBounds.Height ()/2;
    else
    r=rcBounds.Width ()/2; xc=rcBounds.right /2;
    yc=rcBounds.bottom /2; //画出分针和秒针刻度
    int i;
    for(i=0;i<60;i++)
    {
    pdc->MoveTo((long)(xc+(r-10)*sin(PI*i/30.0)),(long)(yc-(r-10)*cos(PI*i/30.0)));
    pdc->LineTo((long)(xc+(r-4)*sin(PI*i/30.0)),(long)(yc-(r-4)*cos(PI*i/30.0)));
    } //画出时针刻度
    CPen pen(PS_SOLID,2,TranslateColor(m_hourPosColor));
    CPen * oldPen=pdc->SelectObject (&pen); for(i=0;i<12;i++)
    {
    pdc->MoveTo((long)(xc+(r-20)*sin(PI*i/6.0)),(long)(yc-(r-20)*cos(PI*i/6.0)));
    pdc->LineTo((long)(xc+(r-4)*sin(PI*i/6.0)),(long)(yc-(r-4)*cos(PI*i)/6.0));
    }
    pdc->SelectObject(oldPen);

    //画出当前时刻的时针、分针和秒针
    DrawPointer(pdc,30,10,10,m_hourAngle);
    DrawPointer(pdc,10,8,10,m_minAngle);
    DrawPointer(pdc,4,2,15,m_secAngle); //画时针、分针和秒针的轴

    pdc->Ellipse(xc-4,yc-4,xc+4,yc+4);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::DoPropExchange - Persistence supportvoid CClockCtrl::DoPropExchange(CPropExchange* pPX)
    {
    ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
    COleControl::DoPropExchange(pPX); // TODO: Call PX_ functions for each persistent custom property.
    PX_Color(pPX,"MinutePosColor",m_hourPosColor,RGB(0,255,255));
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::OnResetState - Reset control to default statevoid CClockCtrl::OnResetState()
    {
    COleControl::OnResetState();  // Resets defaults found in DoPropExchange // TODO: Reset any other control state here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl::AboutBox - Display an "About" box to the uservoid CClockCtrl::AboutBox()
    {
    CDialog dlgAbout(IDD_ABOUTBOX_CLOCK);
    dlgAbout.DoModal();
    }
    /////////////////////////////////////////////////////////////////////////////
    // CClockCtrl message handlersvoid CClockCtrl::DrawPointer(CDC *pDC, double d1, double d2, double d3, double angle)
    {
    CRect rect;
    GetClientRect(&rect); int r,xc,yc;
    if(rect.Width ()>rect.Height ())
    r=rect.Height ()/2;
    else
    r=rect.Width ()/2; xc=rect.right /2;
    yc=rect.bottom /2; CPoint points[4];
    points[0].x=(long)(xc+(r-d1)*sin(angle));
    points[0].y=(long)(yc-(r-d1)*cos(angle));
    points[1].x=(long)(xc-d2*cos(angle));
    points[1].y=(long)(yc-d2*sin(angle));
    points[2].x=(long)(xc-d3*sin(angle));
    points[2].y=(long)(yc+d3*cos(angle));
    points[3].x=(long)(xc+d2*cos(angle));
    points[3].y=(long)(yc+d2*sin(angle)); pDC->SetBkMode (TRANSPARENT);
    pDC->Polygon(points,4);
    }int CClockCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (COleControl::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    m_timer=SetTimer(1,1000,NULL);

    return 0;
    }BOOL CClockCtrl::DestroyWindow()
    {
    KillTimer(m_timer);
    return COleControl::DestroyWindow();
    }void CClockCtrl::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    SetPointerPos();

    InvalidateControl(); COleControl::OnTimer(nIDEvent);
    }void CClockCtrl::SetPointerPos()
    {
    struct tm *osTime;
    CTime t=CTime::GetCurrentTime();
    osTime=t.GetLocalTm(NULL); //计算钟表指针的位置
    m_hourAngle=PI*(osTime->tm_hour +osTime->tm_min/60.0)/6.0;
    m_minAngle=PI*(osTime->tm_min+osTime->tm_sec/60.0)/30.0;
    m_secAngle=PI*(osTime->tm_sec)/30.0;
    }void CClockCtrl::OnHourPosColorChanged() 
    {
    // TODO: Add notification handler code
    InvalidateControl();
    SetModifiedFlag();
    }
      

  4.   

    前面必须#include "AddtlPropPage.h"和#include "ClockPropPage.h"和define IDS_SAMPLE_ADDPPG_CAPTIION  一个值,书我看过,书上只能告诉你大概怎么办,不具体!!!你理解了就会了!!
      

  5.   

    多谢指教!
    现在只剩2个错误了。
    AddtlPropPage.cpp
    e:\ZYX\PROGRAM\VC\Clock\AddtlPropPage.cpp(78) : error C2084: function 'int __thiscall CAddtlPropPage::CAddtlPropPageFactory::UpdateRegistry(int)' already has a body
    e:\ZYX\PROGRAM\VC\Clock\AddtlPropPage.cpp(80) : error C2065: 'AfxGetInstancehandle' : undeclared identifier
    Error executing cl.exe.Clock.ocx - 2 error(s), 0 warning(s)
      

  6.   

    // AddtlPropPage.cpp : implementation file
    //#include "stdafx.h"
    #include "Clock.h"
    #include "AddtlPropPage.h"
    #include "clockctl.h"
    #include "clockppg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAddtlPropPage dialogIMPLEMENT_DYNCREATE(CAddtlPropPage, COlePropertyPage)
    /////////////////////////////////////////////////////////////////////////////
    // Message mapBEGIN_MESSAGE_MAP(CAddtlPropPage, COlePropertyPage)
    //{{AFX_MSG_MAP(CAddtlPropPage)
    // NOTE - ClassWizard will add and remove message map entries
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()BEGIN_PROPPAGEIDS(CClockCtrl, 3)
    PROPPAGEID(CClockPropPage::guid)
    PROPPAGEID(CLSID_CColorPropPage)
    PROPPAGEID(CAddtlPropPage::guid)
    END_PROPPAGEIDS(CClockCtrl)/////////////////////////////////////////////////////////////////////////////
    // Initialize class factory and guid// {2D0E5CBA-B34C-11D5-8582-0000E880B70C}
    IMPLEMENT_OLECREATE_EX(CAddtlPropPage, "Clock.CAddtlPropPage",
    0x2d0e5cba, 0xb34c, 0x11d5, 0x85, 0x82, 0x0, 0x0, 0xe8, 0x80, 0xb7, 0xc)
    /////////////////////////////////////////////////////////////////////////////
    // CAddtlPropPage::CAddtlPropPageFactory::UpdateRegistry -
    // Adds or removes system registry entries for CAddtlPropPageBOOL CAddtlPropPage::CAddtlPropPageFactory::UpdateRegistry(BOOL bRegister)
    {
    // TODO: Define string resource for page type; replace '0' below with ID. if (bRegister)
    return AfxOleRegisterPropertyPageClass(AfxGetInstanceHandle(),
    m_clsid, 0);
    else
    return AfxOleUnregisterClass(m_clsid, NULL);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CAddtlPropPage::CAddtlPropPage - Constructor// TODO: Define string resource for page caption; replace '0' below with ID.CAddtlPropPage::CAddtlPropPage() :
    COlePropertyPage(IDD, IDS_SAMPLE_ADDPPG_CAPTION)
    {
    //{{AFX_DATA_INIT(CAddtlPropPage)
    // NOTE: ClassWizard will add member initialization here
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_DATA_INIT}BOOL CAddtlPropPage::CAddtlPropPageFactory::UpdateRegistry(BOOL bRegister)
    {
    if(bRegister)
    return AfxOleRegisterPropertyPageClass(AfxGetInstancehandle(),m_clsid,IDS_SAMPLE_ADDPPG_CAPTION);
    else
    return AfxOleUnregisterClass(m_clsid,NULL);
    }/////////////////////////////////////////////////////////////////////////////
    // CAddtlPropPage::DoDataExchange - Moves data between page and propertiesvoid CAddtlPropPage::DoDataExchange(CDataExchange* pDX)
    {
    // NOTE: ClassWizard will add DDP, DDX, and DDV calls here
    //    DO NOT EDIT what you see in these blocks of generated code !
    //{{AFX_DATA_MAP(CAddtlPropPage)
    //}}AFX_DATA_MAP
    DDP_PostProcessing(pDX);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CAddtlPropPage message handlers