如果一个Custom Control控件没有和一个自定义的窗口类进行关联,这将导致你的对话框创建失败,无法现实出来。

要使用Custom Control,我们必须先写一个自定义的窗口类,例如CPieChartCtrl,派生自CWnd,

然后,在程序开始对这个窗口类进行注册,

 

BOOL CPieChartCtrl::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();

if (!(::GetClassInfo(hInst, BITMAPVIEWER_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style. = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = PIE_CHART_CTRL ;

if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}

return TRUE;
}

最后在对话框的上面放一个Custom Control控件,例如:
ID:IDC_CUSTOM1
属性:class:PIE_CHART_CTRL //这是你注册的自定义窗口类
Style.:0X50010000
ExStyle.:0X0
在对话框的OnInitDialog()里m_wndChart.SubclassDlgItem(IDC_CUSTOM1, this);

对话框头文件里
CPieChartCtrl m_wndChart;

这样,就可以开始使用Custom Control了。

posted on 2011-03-15 12:56  maxweii  阅读(759)  评论(0编辑  收藏  举报