自己写了一个控件,继承与 CStatic ,现在将其简化如下:// 头文件定义
//我在头文件中定义了一个成员变量 CClientDC* m_dc; 用来代表这个控件自己的 ClientDC
//以便在绘制控件的过程中使用它
class CTrend : public CStatic
{
public:
CTrend();
Init();
public:
CDC m_MemDC;
CClientDC* m_dc;
CRect m_rctThis;
public:
virtual ~CTrend();protected:
//{{AFX_MSG(CTrend)
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
源文件在这里//cpp文件
CTrend::CTrend()
{
m_dc=new CClientDC(this);//调用无效,m_dc为空
}CTrend::~CTrend()
{
}
BEGIN_MESSAGE_MAP(CTrend, CStatic)
//{{AFX_MSG_MAP(CTrend)
ON_WM_SIZE()
ON_WM_CREATE() //}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTrend message handlersvoid CTrend::OnSize(UINT nType, int cx, int cy)
{
CStatic::OnSize(nType, cx, cy);
m_dc=new CClientDC(this);//调用无效,m_dc为空
}int CTrend::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
m_dc=new CClientDC(this);//调用无效,m_dc为空
return 0;
}
我知道,可以在 OnPaint 中得到 ClientDC,但是,我的DC还有别的用处,所以,只能在这个控件的整个生命周期内,初始化一次,请问,我该在什么地方得到正确的 ClientDC 呢????注意:只能在这个控件的整个生命周期内,正确得初始化一次
//我在头文件中定义了一个成员变量 CClientDC* m_dc; 用来代表这个控件自己的 ClientDC
//以便在绘制控件的过程中使用它
class CTrend : public CStatic
{
public:
CTrend();
Init();
public:
CDC m_MemDC;
CClientDC* m_dc;
CRect m_rctThis;
public:
virtual ~CTrend();protected:
//{{AFX_MSG(CTrend)
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
源文件在这里//cpp文件
CTrend::CTrend()
{
m_dc=new CClientDC(this);//调用无效,m_dc为空
}CTrend::~CTrend()
{
}
BEGIN_MESSAGE_MAP(CTrend, CStatic)
//{{AFX_MSG_MAP(CTrend)
ON_WM_SIZE()
ON_WM_CREATE() //}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTrend message handlersvoid CTrend::OnSize(UINT nType, int cx, int cy)
{
CStatic::OnSize(nType, cx, cy);
m_dc=new CClientDC(this);//调用无效,m_dc为空
}int CTrend::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
m_dc=new CClientDC(this);//调用无效,m_dc为空
return 0;
}
我知道,可以在 OnPaint 中得到 ClientDC,但是,我的DC还有别的用处,所以,只能在这个控件的整个生命周期内,初始化一次,请问,我该在什么地方得到正确的 ClientDC 呢????注意:只能在这个控件的整个生命周期内,正确得初始化一次
你只是new出来当然没用
getdc得出来
void CTrend::Init()
{
m_dc=new CClientDC(this);
}//然后,在对话框的 OnInitDialog 中调用
void CMyDialog::OnInitDialog()
{
CTrend t1;
t1.Init(); //可以得到正确的 m_dc。。
}所以说 1楼 兄弟的说法不对啊用 new 也是可以得到 dc 的。大家说要在 Create 之后调用,我在上面的代码中,在 OnCreate 里面调用了 m_dc=new CClientDC(this);但是,返回值确实为空啊
CTrend t1;//在对话框源文件中
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC100, t1);//在这里把 t1 和对话框模板中的控件对应起来
}
那么,我在 CTrend 类中重载的 OnCreate 函数怎么没有被执行呢??
只好使用自己添加的 Init() 函数了