我下载了一个自绘silder源码,IDC_SLIDER_SHARE2是silder控件ID。 原来的源码对IDC_SLIDER_SHARE2进行了关联处理
DDX_Control(pDX, IDC_SLIDER_SHARE2, m_silder);
现在我把它修改了一下,用((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2)),并没用到m_silder代码如下:
void CCoolSliderDlg::BuildShareSlider()
{
ASSERT( m_hHandCur != NULL ); ASSERT( m_lpActive == NULL );
ASSERT( m_lpNormal == NULL );
ASSERT( m_lpThumb == NULL ); m_lpActive = new CBitItem(IDB_BITMAP_ACTIVE, 0, 0);
m_lpNormal = new CBitItem(IDB_BITMAP_NORMAL, 0, 0);
m_lpThumb = new CBitItem(IDB_BITMAP_THUMB, 6, 12); ((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetFlipCursor(m_hHandCur);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->BuildThumbItem(m_lpThumb);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->BuildBackItem(m_lpNormal, m_lpActive);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetTopOffset(3);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetRange(0, 100);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetLineSize(0);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetPos(60);
}
奇怪的是,上面代码中没有用到关联指针,为什么我取消控件的关联。程序就崩溃?谢谢!!
DDX_Control(pDX, IDC_SLIDER_SHARE2, m_silder);
现在我把它修改了一下,用((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2)),并没用到m_silder代码如下:
void CCoolSliderDlg::BuildShareSlider()
{
ASSERT( m_hHandCur != NULL ); ASSERT( m_lpActive == NULL );
ASSERT( m_lpNormal == NULL );
ASSERT( m_lpThumb == NULL ); m_lpActive = new CBitItem(IDB_BITMAP_ACTIVE, 0, 0);
m_lpNormal = new CBitItem(IDB_BITMAP_NORMAL, 0, 0);
m_lpThumb = new CBitItem(IDB_BITMAP_THUMB, 6, 12); ((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetFlipCursor(m_hHandCur);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->BuildThumbItem(m_lpThumb);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->BuildBackItem(m_lpNormal, m_lpActive);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetTopOffset(3);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetRange(0, 100);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetLineSize(0);
((CBitSlider*)GetDlgItem(IDC_SLIDER_SHARE2))->SetPos(60);
}
奇怪的是,上面代码中没有用到关联指针,为什么我取消控件的关联。程序就崩溃?谢谢!!
去掉后DDX_Control,slider控件关联的类只是CSliderCtrl类,你把它强制转换成CBitSlider类,出问题的原因就是你把父类强制转换成子类,面向对象中,把子类转换成父类没有什么问题,但是把父类转换成子类后,只能调用父类的方法,否则会出错。