![]() |
#2
OP_MoMo2013-01-30 08:39
没错啊,这个OnEraseBkgnd函数里面只有一句return TRUE,你看看我的
只有本站会员才能查看附件,请 登录 void CDDemoDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); CDC *pDC = GetDC(); CRect rect; GetClientRect (&rect); //创建一个内存中的显示设备 CDC nDC; nDC.CreateCompatibleDC(NULL); //创建一个内存中的图像 CBitmap nBitmap,*OldBitMap; nBitmap.CreateCompatibleBitmap(pDC, rect.Width(),rect.Height()); //指定内存显示设备在内存中的图像上画图 OldBitMap=nDC.SelectObject(&nBitmap); nDC.FillSolidRect(rect,RGB(255,255,255)); CString cstrText; SYSTEMTIME st; ::GetLocalTime(&st); if(st.wHour<10) { cstrText=_T("0"); cstrText.Format(cstrText+_T("%d:"),st.wHour); } else cstrText.Format(_T("%d:"),st.wHour); if(st.wMinute<10) cstrText.Format(cstrText+_T("0")+_T("%d:"),st.wMinute); else cstrText.Format(cstrText+_T("%d:"),st.wMinute); if(st.wSecond<10) cstrText.Format(cstrText+_T("0")+_T("%d"),st.wSecond); else cstrText.Format(cstrText+_T("%d"),st.wSecond); nDC.TextOut(50,50,cstrText); pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&nDC, 0, 0, SRCCOPY); //释放相关资源 nDC.SelectObject(OldBitMap); nBitmap.DeleteObject(); nDC.DeleteDC(); ReleaseDC(pDC); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CDDemoDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CDDemoDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(nIDEvent = 1) { this->Invalidate(TRUE); } CDialog::OnTimer(nIDEvent); } BOOL CDDemoDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 //return CDialog::OnEraseBkgnd(pDC); return TRUE; } |
我是这个做的:
① 建立基于对话框的MFC应用程序;
② 在OnInitDialog里面输入如下代码:
SetTime(1,50,NULL);
③ 在OnTimer里面加入:
Invalidate(TRUE)
④ 删除OnPaint()里面的原始代码,写入双缓冲程序,主要实现时间的动态显示:
CPaintDC dc(this); // device context for painting
CDC *pDC = GetDC();
CRect rect;
GetClientRect (&rect);
//创建一个内存中的显示设备
CDC nDC;
nDC.CreateCompatibleDC(NULL);
//创建一个内存中的图像
CBitmap nBitmap,*OldBitMap;
nBitmap.CreateCompatibleBitmap(pDC, rect.Width(),rect.Height());
//指定内存显示设备在内存中的图像上画图
OldBitMap=nDC.SelectObject(&nBitmap);
//显示时间
CString str;
CTime tm; tm=CTime::GetCurrentTime();
str=tm.Format("当前时间:%X");
nDC.TextOutW(50,50,str);
pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&nDC, 0, 0, SRCCOPY);
//释放相关资源
nDC.SelectObject(OldBitMap);
nBitmap.DeleteObject();
nDC.DeleteDC();
ReleaseDC(pDC);
⑤ 添加OnEraseBkgnd函数,将其重载为return TRUE;
哪位大神帮忙看一下啊,指导一下哪里写错了。
运行出来会闪烁,郁闷了,就是按照网上的方法做的啊。