下面图老师小编要向大家介绍下在对话框中动态显示位图,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!
【 tulaoshi.com - 编程语言 】
在对话框中显示位图分为静态和动态两种方法。静态法就是用VC6.0的资源编辑器,首先在资源视图中引入一张所要显示的位图,然后在对话框中放置一个Picture控件,在其属性对话框的Type下拉框中选择Bitmap,在Image框中输入所引入位图的ID,编译后对话框中就会显示所引入的位图。这种方法有一个明显的缺点,就是位图必须先画好并且程序运行时此位图禁止改变。基于此,下文以建立一个适时显示当前时间的程序为例,来介绍如何在对话框中动态显示位图。
首先建立一个基于对话框的程序,名为MyDialog。
清除掉对话框中的TODO:字样,把“确定”和“取消”两个按钮移到对话框的下边,调整对话框为到合适大小。
打开类视图,右键单击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()消息函数:
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)void CMyDialogDlg::OnTimer(UINT nIDEvent)
{
  // TOD 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(); 
// TOD Add your message handler code here 
KillTimer(1); 
} 
添加完所有代码后,编译连接就可以欣赏自己的杰作了。下面是程序执行结果。
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)来源:http://www.tulaoshi.com/n/20160219/1605751.html