登录站点

用户名

密码

在对话框中动态显示位图[ 2007-4-22 21:55:00 | By: caopengly ]

已有 455 次阅读  2009-09-24 10:08   标签位图  对话框  caopengly  动态 
在对话框中显示位图分为静态和动态两种方法。静态法就是用VC6.0的资源编辑器,首先在资源视图中引入一张所要显示的位图,然后在对话框中放置一个Picture控件,在其属性对话框的Type下拉框中选择Bitmap,在Image框中输入所引入位图的ID,编译后对话框中就会显示所引入的位图。这种方法有
一个明显的缺点,就是位图必须先画好并且程序运行时此位图禁止改变。基于此,下文以建立一个适时显示当前时间的程序为例,来介绍如何在对话框中动态显示位图。  
 
    首先建立一个基于对话框的程序,名为MyDialog。 

打开类视图,右键单击CMyDialogDlg,添加如下成员变量:  
protected:  
CRect m_bitmapRect; //位图在对话框中位置 
 
添加画图成员函数:  
 
protected:  
BOOL DrawBitmap(CString strTime,CRect* bitmapRect);  //strTime为表示时间的字符串,bitmapRect指明在窗口的什么区域画图。  
 
使用ClassWizard为CMyDialogDlg加入WM_TIMER的消息响应函数OnTimer(), 和WM_DESTROY的消息响应函数OnDestroy()。  

 
在CMyDialogDlg::OnInitDialog()种添加如下代码:  
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;  

 
编写画图代码。  
BOOL CMyDialogDlg::DrawBitmap(CString str,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.DrawText(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;  

 
 
处理OnTimer()消息函数:  
void CMyDialogDlg::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);  
}  
 
 
处理OnDestroy()消息函数:  
void CMyDlg::OnDestroy()  
{  
CDialog::OnDestroy();  
// TODO: Add your message handler code here  
KillTimer(1);  
}

上一篇: [FPGA与EDA]转帖:我们的CPU [ 2007-9-11 11:48:00 | By: caopengly ] 下一篇: 嵌入式开发的单步调试技术[ 2007-4-16 20:00:00 | By: caopengly ]

分享 举报