Invalidate()和UpdateAllViews()

本文详细介绍了Invalidate()和UpdateAllViews()两个函数在Windows编程中的作用及使用场景。Invalidate()用于触发窗口重绘,而UpdateAllViews()则用于在文档-视图结构中更新所有相关视图。

Invalidate()是让程序重画窗口,使系统向其发WM_PAINT消息,使的程序的OnPaint被调用重画客户区。
UpdateAllViews()是在DOC/VIEW结构中,当一个视图的数据改变后,通知所有视图作相应的改变。UpdateAllViews()是文档与视之间的联系,调用从它会使程序与此文档相关的所有视的UpdateView被调用,至于是否重画以及怎么画是由各视的UpdateView来决定的。
Invalidate()是Cwnd的成员函数,与DOC-VIEW无关;
UpdateAllViews()是CDocument的成员函数,具体体现DOC-VIEW的精神。
Invalidate()函数产生一条WM_PAINT消息,并送入windows消息队列中,是窗口产生重画。而UpdateAllViews 并不进入windows消息队列中,直接产生重画。
如果仅重画当前窗口用 this->Invalidate();如果通知所有和当前文档相关的窗口重画用GetDocument()->UpdateAllViews()(在View中)或this->UpdateAllViews()(在Doc中)。
还有一个函数:ONERASEBKGND()画背景用的,先于OnPaint()运行。

转载于:https://www.cnblogs.com/cwbo-win/articles/3380558.html

#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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值