当前位置: 代码迷 >> VC >> 初学者问个有关问题
  详细解决方案

初学者问个有关问题

热度:7266   发布时间:2013-02-25 00:00:00.0
菜鸟问个问题
最近在学习孙鑫老师的VC++的视频,主要目的是学习一下基本原理,在听到第十讲,即图形绘制以及控件背景和文本颜色。最后老师提出一个问题,是实现在CDialogBar上添加一些控件,并在列表框中添加背景位图。以下所做的是SDI应用程序。
现在碰到了一个问题:
①创建对话条模板,ID为IDD_DIALOGBAR;并为它创建了对应的类CDialogBar,刚开始是从CDialog派生而来的,后来按照网上的说法被我进行修改,改后结果如下
头文件:
C/C++ code
#pragma once// CDlgBar 对话框class CDlgBar : public CDialogBar    // 修改基类CDialog为CDialogBar{    DECLARE_DYNAMIC(CDlgBar)public:    CDlgBar(CWnd* pParent = NULL);   // 标准构造函数    virtual ~CDlgBar();// 对话框数据    enum { IDD = IDD_DIALOGBAR };protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持    DECLARE_MESSAGE_MAP()public:    virtual BOOL OnInitDialog();};

实现文件:
C/C++ code
// DlgBar.cpp : 实现文件//#include "stdafx.h"#include "mytest01.h"#include "DlgBar.h"// CDlgBar 对话框IMPLEMENT_DYNAMIC(CDlgBar, CDialog)CDlgBar::CDlgBar(CWnd* pParent /*=NULL*/)//    : CDialog(CDlgBar::IDD, pParent)            去除对CDialog的构造函数{}CDlgBar::~CDlgBar(){}void CDlgBar::DoDataExchange(CDataExchange* pDX){    CDialogBar::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CDlgBar, CDialogBar)END_MESSAGE_MAP()// CDlgBar 消息处理程序BOOL CDlgBar::OnInitDialog(){    // CDialog::OnInitDialog();        // 去掉对CDialog初始化函数部分    // TODO:  在此添加额外的初始化    UpdateData(FALSE);    SetWindowText(_T("My Dialog Bar"));    return TRUE;  // return TRUE unless you set the focus to a control    // 异常: OCX 属性页应返回 FALSE}

其中,我已经覆盖了初始化虚函数。
②框架类中添加CDlgBar的对象,头文件代码如下
C/C++ code
// MainFrm.h : CMainFrame 类的接口//#pragma once#include "dlgbar.h"class CMainFrame : public CFrameWnd{    protected: // 仅从序列化创建    CMainFrame();    DECLARE_DYNCREATE(CMainFrame)// 属性public:// 操作public:// 重写public:    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);// 实现public:    virtual ~CMainFrame();#ifdef _DEBUG    virtual void AssertValid() const;    virtual void Dump(CDumpContext& dc) const;#endifprotected:  // 控件条嵌入成员    CStatusBar  m_wndStatusBar;    CToolBar    m_wndToolBar;    CDlgBar        m_wndDlgBar;// 生成的消息映射函数protected:    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);    DECLARE_MESSAGE_MAP()    };

实现文件调用了Create函数进行创建并显示:
C/C++ code
// MainFrm.cpp : CMainFrame 类的实现//#include "stdafx.h"#include "mytest01.h"#include "MainFrm.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)    ON_WM_CREATE()END_MESSAGE_MAP()static UINT indicators[] ={    ID_SEPARATOR,           // 状态行指示器    ID_INDICATOR_CAPS,    ID_INDICATOR_NUM,    ID_INDICATOR_SCRL,};// CMainFrame 构造/析构CMainFrame::CMainFrame(){    // TODO: 在此添加成员初始化代码}CMainFrame::~CMainFrame(){}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)        return -1;        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP        | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))    {        TRACE0("未能创建工具栏\n");        return -1;      // 未能创建    }    if (!m_wndStatusBar.Create(this) ||        !m_wndStatusBar.SetIndicators(indicators,          sizeof(indicators)/sizeof(UINT)))    {        TRACE0("未能创建状态栏\n");        return -1;      // 未能创建    }    // TODO: 如果不需要可停靠工具栏,则删除这三行    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);    EnableDocking(CBRS_ALIGN_ANY);    DockControlBar(&m_wndToolBar);    if(!m_wndDlgBar.Create(this, IDD_DIALOGBAR,CBRS_LEFT | CBRS_TOOLTIPS | CBRS_FLYBY, IDD_DIALOGBAR))    {        TRACE0("Failed to create DlgBar\n");        return -1;    }    m_wndDlgBar.EnableDocking(CBRS_ALIGN_ANY);    EnableDocking(CBRS_ALIGN_ANY);    DockControlBar(&m_wndDlgBar);    return 0;}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs){    if( !CFrameWnd::PreCreateWindow(cs) )        return FALSE;    // TODO: 在此处通过修改    //  CREATESTRUCT cs 来修改窗口类或样式    return TRUE;}// CMainFrame 诊断#ifdef _DEBUGvoid CMainFrame::AssertValid() const{    CFrameWnd::AssertValid();}void CMainFrame::Dump(CDumpContext& dc) const{    CFrameWnd::Dump(dc);}#endif //_DEBUG// CMainFrame 消息处理程序
  相关解决方案