IDC_RADIO控件绑定变量

在MFC中,默认情况下IDC_RADIO控件无法直接绑定控件变量。若要实现变量绑定,需通过向导并设置控件属性中的group为true。

MFC中默认IDC_RADIO无法绑定控件变量,如果需要通过向导绑定变量的话,需要对控件做如下设置:

将属性中的group设置为true

#include "pch.h" #include "RecursiveSphereDemo.h" #include "LeftPortion.h" #include "RecursiveSphereDemoDoc.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CLeftPortion IMPLEMENT_DYNCREATE(CLeftPortion, CFormView) CLeftPortion::CLeftPortion() : CFormView(CLeftPortion::IDD) { // TODO: 在此添加构造代码 } CLeftPortion::~CLeftPortion() { } void CLeftPortion::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); DDX_Control(pDX, IDC_SLIDER_RADIUS, m_sliderRadius); DDX_Control(pDX, IDC_SLIDER_LEVEL, m_sliderSubdivision); // 对应资源中的 LEVEL DDX_Control(pDX, IDC_RADIO_WIREFRAME, m_radioWireframe); DDX_Control(pDX, IDC_RADIO_SURFACE, m_radioSurface); DDX_Control(pDX, IDC_RADIO_LIGHT_OFF, m_radioLightOff); DDX_Control(pDX, IDC_RADIO_LIGHT_ON, m_radioLightOn); DDX_Control(pDX, IDC_SLIDER_TRANS_X, m_sliderTranslateX); // 对应资源中的 TRANS_X DDX_Control(pDX, IDC_SLIDER_TRANS_Y, m_sliderTranslateY); // 对应资源中的 TRANS_Y DDX_Control(pDX, IDC_SLIDER_TRANS_Z, m_sliderTranslateZ); // 对应资源中的 TRANS_Z DDX_Control(pDX, IDC_EDIT_TRANSLATE_X, m_editTranslateX); DDX_Control(pDX, IDC_EDIT_TRANSLATE_Y, m_editTranslateY); DDX_Control(pDX, IDC_EDIT_TRANSLATE_Z, m_editTranslateZ); DDX_Control(pDX, IDC_SLIDER_ROT_X, m_sliderRotateX); // 对应资源中的 ROT_X DDX_Control(pDX, IDC_SLIDER_ROT_Y, m_sliderRotateY); // 对应资源中的 ROT_Y DDX_Control(pDX, IDC_SLIDER_ROT_Z, m_sliderRotateZ); // 对应资源中的 ROT_Z DDX_Control(pDX, IDC_EDIT_ROTATE_X, m_editRotateX); DDX_Control(pDX, IDC_EDIT_ROTATE_Y, m_editRotateY); DDX_Control(pDX, IDC_EDIT_ROTATE_Z, m_editRotateZ); } BEGIN_MESSAGE_MAP(CLeftPortion, CFormView) ON_WM_HSCROLL() ON_BN_CLICKED(IDC_RADIO_WIREFRAME, &CLeftPortion::OnBnClickedRadioWireframe) ON_BN_CLICKED(IDC_RADIO_SURFACE, &CLeftPortion::OnBnClickedRadioSurface) ON_BN_CLICKED(IDC_RADIO_LIGHT_OFF, &CLeftPortion::OnBnClickedRadioLightOff) ON_BN_CLICKED(IDC_RADIO_LIGHT_ON, &CLeftPortion::OnBnClickedRadioLightOn) ON_EN_CHANGE(IDC_EDIT_TRANSLATE_X, &CLeftPortion::OnEnChangeEditTranslateX) ON_EN_CHANGE(IDC_EDIT_TRANSLATE_Y, &CLeftPortion::OnEnChangeEditTranslateY) ON_EN_CHANGE(IDC_EDIT_TRANSLATE_Z, &CLeftPortion::OnEnChangeEditTranslateZ) ON_EN_CHANGE(IDC_EDIT_ROTATE_X, &CLeftPortion::OnEnChangeEditRotateX) ON_EN_CHANGE(IDC_EDIT_ROTATE_Y, &CLeftPortion::OnEnChangeEditRotateY) ON_EN_CHANGE(IDC_EDIT_ROTATE_Z, &CLeftPortion::OnEnChangeEditRotateZ) END_MESSAGE_MAP() BOOL CLeftPortion::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或样式 return CFormView::PreCreateWindow(cs); } void CLeftPortion::OnInitialUpdate() { CFormView::OnInitialUpdate(); // 获取文档指针 CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; // 设置滑块范围和初始值 // 球体控制 m_sliderRadius.SetRange(1, 100); m_sliderRadius.SetPos((int)(pDoc->m_fRadius * 100)); m_sliderSubdivision.SetRange(0, 4); // 细分级别范围(0-4) m_sliderSubdivision.SetPos(pDoc->m_nSubdivision); // 平移变换 m_sliderTranslateX.SetRange(-100, 100); m_sliderTranslateX.SetPos((int)(pDoc->m_fTranslateX * 100)); m_sliderTranslateY.SetRange(-100, 100); m_sliderTranslateY.SetPos((int)(pDoc->m_fTranslateY * 100)); m_sliderTranslateZ.SetRange(-100, 100); m_sliderTranslateZ.SetPos((int)(pDoc->m_fTranslateZ * 100)); // 旋转变换 m_sliderRotateX.SetRange(-180, 180); m_sliderRotateX.SetPos((int)pDoc->m_fRotateX); m_sliderRotateY.SetRange(-180, 180); m_sliderRotateY.SetPos((int)pDoc->m_fRotateY); m_sliderRotateZ.SetRange(-180, 180); m_sliderRotateZ.SetPos((int)pDoc->m_fRotateZ); // 设置单选按钮状态 m_radioWireframe.SetCheck(pDoc->m_bWireframe ? BST_CHECKED : BST_UNCHECKED); m_radioSurface.SetCheck(!pDoc->m_bWireframe ? BST_CHECKED : BST_UNCHECKED); m_radioLightOff.SetCheck(!pDoc->m_bLighting ? BST_CHECKED : BST_UNCHECKED); m_radioLightOn.SetCheck(pDoc->m_bLighting ? BST_CHECKED : BST_UNCHECKED); // 更新UI显示 UpdateUI(); } void CLeftPortion::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; if (pScrollBar == &m_sliderRadius) { pDoc->m_fRadius = (float)m_sliderRadius.GetPos() / 100.0f; pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderSubdivision) { pDoc->m_nSubdivision = m_sliderSubdivision.GetPos(); pDoc->m_nTriangleCount = 8 * (int)pow(4.0, pDoc->m_nSubdivision); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderTranslateX) { pDoc->m_fTranslateX = (float)m_sliderTranslateX.GetPos() / 100.0f; CString str; str.Format("%.2f", pDoc->m_fTranslateX); m_editTranslateX.SetWindowText(str); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderTranslateY) { pDoc->m_fTranslateY = (float)m_sliderTranslateY.GetPos() / 100.0f; CString str; str.Format("%.2f", pDoc->m_fTranslateY); m_editTranslateY.SetWindowText(str); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderTranslateZ) { pDoc->m_fTranslateZ = (float)m_sliderTranslateZ.GetPos() / 100.0f; CString str; str.Format("%.2f", pDoc->m_fTranslateZ); m_editTranslateZ.SetWindowText(str); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderRotateX) { pDoc->m_fRotateX = (float)m_sliderRotateX.GetPos(); CString str; str.Format("%.0f", pDoc->m_fRotateX); m_editRotateX.SetWindowText(str); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderRotateY) { pDoc->m_fRotateY = (float)m_sliderRotateY.GetPos(); CString str; str.Format("%.0f", pDoc->m_fRotateY); m_editRotateY.SetWindowText(str); pDoc->UpdateAllViews(NULL); } else if (pScrollBar == &m_sliderRotateZ) { pDoc->m_fRotateZ = (float)m_sliderRotateZ.GetPos(); CString str; str.Format("%.0f", pDoc->m_fRotateZ); m_editRotateZ.SetWindowText(str); pDoc->UpdateAllViews(NULL); } CFormView::OnHScroll(nSBCode, nPos, pScrollBar); } void CLeftPortion::OnBnClickedRadioWireframe() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (pDoc) { pDoc->m_bWireframe = TRUE; pDoc->UpdateAllViews(NULL); } } void CLeftPortion::OnBnClickedRadioSurface() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (pDoc) { pDoc->m_bWireframe = FALSE; pDoc->UpdateAllViews(NULL); } } void CLeftPortion::OnBnClickedRadioLightOff() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (pDoc) { pDoc->m_bLighting = FALSE; pDoc->UpdateAllViews(NULL); } } void CLeftPortion::OnBnClickedRadioLightOn() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (pDoc) { pDoc->m_bLighting = TRUE; pDoc->UpdateAllViews(NULL); } } void CLeftPortion::OnEnChangeEditTranslateX() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editTranslateX.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -1.0f && fValue <= 1.0f) { pDoc->m_fTranslateX = fValue; m_sliderTranslateX.SetPos((int)(fValue * 100)); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::OnEnChangeEditTranslateY() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editTranslateY.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -1.0f && fValue <= 1.0f) { pDoc->m_fTranslateY = fValue; m_sliderTranslateY.SetPos((int)(fValue * 100)); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::OnEnChangeEditTranslateZ() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editTranslateZ.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -1.0f && fValue <= 1.0f) { pDoc->m_fTranslateZ = fValue; m_sliderTranslateZ.SetPos((int)(fValue * 100)); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::OnEnChangeEditRotateX() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editRotateX.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -180.0f && fValue <= 180.0f) { pDoc->m_fRotateX = fValue; m_sliderRotateX.SetPos((int)fValue); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::OnEnChangeEditRotateY() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editRotateY.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -180.0f && fValue <= 180.0f) { pDoc->m_fRotateY = fValue; m_sliderRotateY.SetPos((int)fValue); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::OnEnChangeEditRotateZ() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; CString str; m_editRotateZ.GetWindowText(str); float fValue; if (str.GetLength() > 0 && !str.IsEmpty()) { fValue = (float)_tstof(str); if (fValue >= -180.0f && fValue <= 180.0f) { pDoc->m_fRotateZ = fValue; m_sliderRotateZ.SetPos((int)fValue); pDoc->UpdateAllViews(NULL); } } } void CLeftPortion::UpdateUI() { CRecursiveSphereDemoDoc* pDoc = GetDocument(); if (!pDoc) return; // 更新编辑框 CString str; str.Format("%.2f", pDoc->m_fTranslateX); m_editTranslateX.SetWindowText(str); str.Format("%.2f", pDoc->m_fTranslateY); m_editTranslateY.SetWindowText(str); str.Format("%.2f", pDoc->m_fTranslateZ); m_editTranslateZ.SetWindowText(str); str.Format("%.0f", pDoc->m_fRotateX); m_editRotateX.SetWindowText(str); str.Format("%.0f", pDoc->m_fRotateY); m_editRotateY.SetWindowText(str); str.Format("%.0f", pDoc->m_fRotateZ); m_editRotateZ.SetWindowText(str); } CRecursiveSphereDemoDoc* CLeftPortion::GetDocument() const { return reinterpret_cast<CRecursiveSphereDemoDoc*>(m_pDocument); } 修改代码错误给出正确的完整代码
06-21
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值