我在一个对话框中动态显示时间,代码如下,他可以编译通过,但是当我拖动对话筐的时候,有错误发生,我看了半天,也没有发现问题在哪里,请问那位大侠可以帮忙解决?我的代码如下:
//头函数中一个保护变量和一个画图函数
protected:
BOOL DrawBitmap(CString strTime,CRect* bitmapRect);
CRect m_bitmapRect;//初始化对话框函数
BOOL CPdhDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetTimer(1,100,NULL); //设置定时器
CRect rect;
GetClientRect(&rect); //获得客户区大小
ScreenToClient(&rect); //将屏幕坐标转化为客户坐标
int width=180,height=45; //要创建的位图的宽度和高度
m_bitmapRect.left=rect.right-25-width; //位图右边界距对话框的右边界25像素
m_bitmapRect.top=rect.top+35; //位图上边界距对话框的上边界35像素
m_bitmapRect.right=m_bitmapRect.left+width;
m_bitmapRect.bottom=m_bitmapRect.top+height;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}//定时器
void CPdhDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime time;
time=CTime::GetCurrentTime(); //获得当前时间
CString strTime=time.Format("%H:%M:%S"); //将时间转化为字符串
BOOL res=DrawBitmap(strTime,&m_bitmapRect); //画图显示时间
if(res!=TRUE)
{
MessageBox("Error");
}
CDialog::OnTimer(nIDEvent);
}//画位图
BOOL CPdhDlg::DrawBitmap(CString strTime, CRect *bitmapRect)
{
CBitmap bitmap, *pOldBitmap;
CFont font, *pOldFont;
CDC SourceDC,*pDC;
BOOL result;
pDC=GetDC(); //获得当前窗口的设备描述表
if(pDC==NULL)
{
KillTimer(1);
MessageBox("系统资源不足,请关闭程序");
return FALSE;
} SourceDC.CreateCompatibleDC(pDC); //建立与显示设备兼容的位图 bitmap.CreateCompatibleBitmap(pDC,bitmapRect->Width(),bitmapRect->Height());
font.CreatePointFont(200,"Arial",pDC); //创建点阵字体
pOldBitmap=SourceDC.SelectObject(&bitmap); //将位图选入内存场境
pOldFont=SourceDC.SelectObject(&font); //将字体选入内存场境
SourceDC.SetBkMode(OPAQUE); //设置位图背景模式为不透明
SourceDC.SetBkColor(RGB(0,0,0)); //设置位图背景颜色为黑色
SourceDC.FillSolidRect(0,0,bitmapRect->Width(),bitmapRect->Height(),RGB(0,0,0)); //填充位图
SourceDC.SetTextColor(RGB(255,0,0)); //设置文字显示颜色
//在位图中显示文字
RECT rect;
rect.left=0;
rect.top=0;
rect.right=bitmapRect->Width();
rect.bottom=bitmapRect->Height();
SourceDC.DrawTex(strTime,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE); //在距位图四周2像素处画一3D矩形框,使其具有3D视觉效果.
rect.left=bitmapRect->left-2;
rect.top=bitmapRect->top-2;
rect.right=bitmapRect->right+2;
rect.bottom=bitmapRect->bottom+2;
pDC->Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255)); //在对话框上显示位图
result=pDC->BitBlt(bitmapRect->left,bitmapRect->top,bitmapRect->Width (),bitmapRect->Height(),&SourceDC,0,0,SRCCOPY); //撤销资源
SourceDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldFont);
bitmap.DeleteObject();
font.DeleteObject();
SourceDC.DeleteDC();
pDC->DeleteDC(); //返回操作结果
return result;
}//
void CPdhDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}
//头函数中一个保护变量和一个画图函数
protected:
BOOL DrawBitmap(CString strTime,CRect* bitmapRect);
CRect m_bitmapRect;//初始化对话框函数
BOOL CPdhDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetTimer(1,100,NULL); //设置定时器
CRect rect;
GetClientRect(&rect); //获得客户区大小
ScreenToClient(&rect); //将屏幕坐标转化为客户坐标
int width=180,height=45; //要创建的位图的宽度和高度
m_bitmapRect.left=rect.right-25-width; //位图右边界距对话框的右边界25像素
m_bitmapRect.top=rect.top+35; //位图上边界距对话框的上边界35像素
m_bitmapRect.right=m_bitmapRect.left+width;
m_bitmapRect.bottom=m_bitmapRect.top+height;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}//定时器
void CPdhDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime time;
time=CTime::GetCurrentTime(); //获得当前时间
CString strTime=time.Format("%H:%M:%S"); //将时间转化为字符串
BOOL res=DrawBitmap(strTime,&m_bitmapRect); //画图显示时间
if(res!=TRUE)
{
MessageBox("Error");
}
CDialog::OnTimer(nIDEvent);
}//画位图
BOOL CPdhDlg::DrawBitmap(CString strTime, CRect *bitmapRect)
{
CBitmap bitmap, *pOldBitmap;
CFont font, *pOldFont;
CDC SourceDC,*pDC;
BOOL result;
pDC=GetDC(); //获得当前窗口的设备描述表
if(pDC==NULL)
{
KillTimer(1);
MessageBox("系统资源不足,请关闭程序");
return FALSE;
} SourceDC.CreateCompatibleDC(pDC); //建立与显示设备兼容的位图 bitmap.CreateCompatibleBitmap(pDC,bitmapRect->Width(),bitmapRect->Height());
font.CreatePointFont(200,"Arial",pDC); //创建点阵字体
pOldBitmap=SourceDC.SelectObject(&bitmap); //将位图选入内存场境
pOldFont=SourceDC.SelectObject(&font); //将字体选入内存场境
SourceDC.SetBkMode(OPAQUE); //设置位图背景模式为不透明
SourceDC.SetBkColor(RGB(0,0,0)); //设置位图背景颜色为黑色
SourceDC.FillSolidRect(0,0,bitmapRect->Width(),bitmapRect->Height(),RGB(0,0,0)); //填充位图
SourceDC.SetTextColor(RGB(255,0,0)); //设置文字显示颜色
//在位图中显示文字
RECT rect;
rect.left=0;
rect.top=0;
rect.right=bitmapRect->Width();
rect.bottom=bitmapRect->Height();
SourceDC.DrawTex(strTime,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE); //在距位图四周2像素处画一3D矩形框,使其具有3D视觉效果.
rect.left=bitmapRect->left-2;
rect.top=bitmapRect->top-2;
rect.right=bitmapRect->right+2;
rect.bottom=bitmapRect->bottom+2;
pDC->Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255)); //在对话框上显示位图
result=pDC->BitBlt(bitmapRect->left,bitmapRect->top,bitmapRect->Width (),bitmapRect->Height(),&SourceDC,0,0,SRCCOPY); //撤销资源
SourceDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldFont);
bitmap.DeleteObject();
font.DeleteObject();
SourceDC.DeleteDC();
pDC->DeleteDC(); //返回操作结果
return result;
}//
void CPdhDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}
解决方案 »
- 菜单响应事件问题。
- VC中如何在add member variable 中在variable type中加入用户自己定义的类
- 天天看你叫着啥普及历史之类的,为你的网站打广告,今天咋歇起来了?
- 一个关于sendto的问题
- 预览窗口:应该选择哪个控件?
- 100分--关于多语种的问题
- 求《COM本质论》中文版电子书下载地址,谢!
- CScrollView的问题:假如设置其大小为200*200,为何这个区域外还能作图?
- 那位知道有没有现成的东西可以将数据以表格形式显示?
- 我想做个计划任务程序,不知怎样做更简介些?
- 问题一个很简单的问题, 给出正确答案马上结分,Online
- 100分求助,简单的CSOCKET问题
然后就是file:winhand.cpp line:199,可能在那里有错误。但不知道什么错。
GetClientRect(&rect);得到的是客户区坐标,不用转换。
暂时没有发现问题,有空再看。拖动时出错的话,应该有相应的代码,比如line:199是哪一句?
你把部分代码改一下试试://定时器
void CPdhDlg::OnTimer(UINT nIDEvent)
{
Invalidate(false);
CDialog::OnTimer(nIDEvent);
}///////////////////////////////////////////////////////////////
//
// 请将下面 DrawBitmap() 中的 RECT 改成 m_bitmapRect
//
////////////////////////////////////////////////////////////////画位图
BOOL CPdhDlg::DrawBitmap(void)
{
CBitmap bitmap, *pOldBitmap;
CFont font, *pOldFont;
CDC SourceDC,*pDC;
BOOL result; CTime time;
time=CTime::GetCurrentTime(); //获得当前时间
CString strTime=time.Format("%H:%M:%S"); //将时间转化为字符串
pDC=GetDC(); //获得当前窗口的设备描述表
if(pDC==NULL)
{
KillTimer(1);
MessageBox("系统资源不足,请关闭程序");
return FALSE;
} SourceDC.CreateCompatibleDC(pDC); //建立与显示设备兼容的位图 bitmap.CreateCompatibleBitmap(pDC,bitmapRect->Width(),bitmapRect->Height());
font.CreatePointFont(200,"Arial",pDC); //创建点阵字体
pOldBitmap=SourceDC.SelectObject(&bitmap); //将位图选入内存场境
pOldFont=SourceDC.SelectObject(&font); //将字体选入内存场境
SourceDC.SetBkMode(OPAQUE); //设置位图背景模式为不透明
SourceDC.SetBkColor(RGB(0,0,0)); //设置位图背景颜色为黑色
SourceDC.FillSolidRect(0,0,bitmapRect->Width(),bitmapRect->Height(),RGB(0,0,0)); //填充位图
SourceDC.SetTextColor(RGB(255,0,0)); //设置文字显示颜色
//在位图中显示文字
RECT rect;
rect.left=0;
rect.top=0;
rect.right=bitmapRect->Width();
rect.bottom=bitmapRect->Height();
SourceDC.DrawTex(strTime,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE); //在距位图四周2像素处画一3D矩形框,使其具有3D视觉效果.
rect.left=bitmapRect->left-2;
rect.top=bitmapRect->top-2;
rect.right=bitmapRect->right+2;
rect.bottom=bitmapRect->bottom+2;
pDC->Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255)); //在对话框上显示位图
result=pDC->BitBlt(bitmapRect->left,bitmapRect->top,bitmapRect->Width (),bitmapRect->Height(),&SourceDC,0,0,SRCCOPY); //撤销资源
SourceDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldFont);
bitmap.DeleteObject();
font.DeleteObject();
SourceDC.DeleteDC();
pDC->DeleteDC(); //返回操作结果
return result;
}//
void CPdhDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}