在对话框中设置背景的三种方法 .

简介: 方法一: 在OnPaint中StretchBlt 具体是:注释掉CDialog::OnPaint()或放到结尾(原因何在呢?),并加入贴图代码 [cpp] view plaincopyprint? void CqqqqqDlg::OnPaint()  {      ...

方法一:

在OnPaint中StretchBlt

具体是:注释掉CDialog::OnPaint()或放到结尾(原因何在呢?),并加入贴图代码

  1. void CqqqqqDlg::OnPaint()  
  2. {  
  3.     if (IsIconic())  
  4.     {  
  5.         CPaintDC dc(this); // device context for painting   
  6.   
  7.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);  
  8.   
  9.         // Center icon in client rectangle   
  10.         int cxIcon = GetSystemMetrics(SM_CXICON);  
  11.         int cyIcon = GetSystemMetrics(SM_CYICON);  
  12.         CRect rect;  
  13.         GetClientRect(&rect);  
  14.         int x = (rect.Width() - cxIcon + 1) / 2;  
  15.         int y = (rect.Height() - cyIcon + 1) / 2;  
  16.   
  17.         // Draw the icon   
  18.         dc.DrawIcon(x, y, m_hIcon);  
  19.     }  
  20.     else  
  21.     {  
  22.         //CDialog::OnPaint();//<span style="color:#6600cc;">注释此句,如果不注释的话,就放到结尾,原因何在呢?   
  23. </span>       //贴背景图片   
  24.         CPaintDC dc(this);  
  25.         CBitmap bmpBk;  
  26.         bmpBk.LoadBitmapW(IDB_BITMAP_tempbk);  
  27.         //m_bmpBK.LoadBitmapW(IDB_BMPBK);   
  28.         BITMAP bmpSize;  
  29.         bmpBk.GetBitmap(&bmpSize);//获取背景图片尺寸   
  30.   
  31.         CRect rect;  
  32.         GetClientRect(&rect);//获取客户区尺寸   
  33.   
  34.         CDC dcMem;  
  35.         dcMem.CreateCompatibleDC(&dc);  
  36.         dcMem.SelectObject(&bmpBk);  
  37.         dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//将背景图片拉伸或者压缩copy至客户区   
  38.         //贴背景图片   
  39.     }  
  40. }  
void CqqqqqDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { //CDialog::OnPaint();//<span style="color:#6600cc;">注释此句,如果不注释的话,就放到结尾,原因何在呢? </span> //贴背景图片 CPaintDC dc(this); CBitmap bmpBk; bmpBk.LoadBitmapW(IDB_BITMAP_tempbk); //m_bmpBK.LoadBitmapW(IDB_BMPBK); BITMAP bmpSize; bmpBk.GetBitmap(&bmpSize);//获取背景图片尺寸 CRect rect; GetClientRect(&rect);//获取客户区尺寸 CDC dcMem; dcMem.CreateCompatibleDC(&dc); dcMem.SelectObject(&bmpBk); dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//将背景图片拉伸或者压缩copy至客户区 //贴背景图片 } }


执行效果截图如下:

方法二:

在OnEraseBkgnd中StretchBlt

具体是:注释掉return CDialog::OnEraseBkgnd(pDC);直接返回true(为什么不能返回这个要返回true呢?),代码如下:

  1. BOOL CqqqqqDlg::OnEraseBkgnd(CDC* pDC)  
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.     //贴背景图片   
  5.     CBitmap bmpBk;  
  6.     bmpBk.LoadBitmapW(IDB_BITMAP_tempbk);  
  7.     //m_bmpBK.LoadBitmapW(IDB_BMPBK);   
  8.     BITMAP bmpSize;  
  9.     bmpBk.GetBitmap(&bmpSize);//获取背景图片尺寸   
  10.   
  11.     CRect rect;  
  12.     GetClientRect(&rect);//获取客户区尺寸   
  13.   
  14.     CDC dcMem;  
  15.     dcMem.CreateCompatibleDC(pDC);  
  16.     dcMem.SelectObject(&bmpBk);  
  17.     pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//将背景图片拉伸或者压缩copy至客户区   
  18.     //贴背景图片   
  19.     return true;  
  20.     //return CDialog::OnEraseBkgnd(pDC);   
  21. }  
BOOL CqqqqqDlg::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default //贴背景图片 CBitmap bmpBk; bmpBk.LoadBitmapW(IDB_BITMAP_tempbk); //m_bmpBK.LoadBitmapW(IDB_BMPBK); BITMAP bmpSize; bmpBk.GetBitmap(&bmpSize);//获取背景图片尺寸 CRect rect; GetClientRect(&rect);//获取客户区尺寸 CDC dcMem; dcMem.CreateCompatibleDC(pDC); dcMem.SelectObject(&bmpBk); pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//将背景图片拉伸或者压缩copy至客户区 //贴背景图片 return true; //return CDialog::OnEraseBkgnd(pDC); }


效果与方法一相同,图片就不贴了,参见上图。

而且这里还有一个很有意思的现象,若在OnEraseBkgnd贴图,在OnPaint()函数中不调用基类的OnPaint,即注释掉CDialog::OnPaint(),则将界面隐藏后再显示出来则控件全没了,只有对话框以及背景。如下图:

 原因见《在OnPaint中必须调用一次BeginPaint和EndPaint,且也只能调用一次。

 

方法三:

在OnCtlColor中返回带有背景位图的画刷

 具体是:

1、在头文件中定一个背景刷

  1. public:  
  2.     CBrush   m_brushBk;  
public: CBrush m_brushBk;


2、在OnInitDialog中加入以下句

  1. // TODO: Add extra initialization here   
  2. CBitmap bmp;  
  3. bmp.LoadBitmap(IDB_BITMAP_tempbk);   
  4. m_brushBk.CreatePatternBrush(&bmp);   
  5. //m_brushBk.CreateSolidBrush(RGB(0,255,0)); //用纯色作为背景   
  6.    bmp.DeleteObject();      
// TODO: Add extra initialization here CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP_tempbk); m_brushBk.CreatePatternBrush(&bmp); //m_brushBk.CreateSolidBrush(RGB(0,255,0)); //用纯色作为背景 bmp.DeleteObject();



 

3、在OnCtlColor函数中返回背景画刷

  1. HBRUSH CXXXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.   
  5.     // TODO:  Change any attributes of the DC here   
  6.   
  7.     // TODO:  Return a different brush if the default is not desired   
  8.     if(pWnd==this//this代表当前对话框窗口   
  9.     {   
  10.         return   m_brushBk;   
  11.     }   
  12.   
  13.     return hbr;  
  14.   
  15. }  
HBRUSH CXXXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here // TODO: Return a different brush if the default is not desired if(pWnd==this) //this代表当前对话框窗口 { return m_brushBk; } return hbr; }

效果如下图所示:

注意这个函数里面的if判断,这个pWnd参数很关键。

我们看看,如果没有这个if判断,直接返回m_brushBk;会是什么结果呢,代码如下:

  1. HBRUSH CqqqqqDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.   
  5.     // TODO:  Change any attributes of the DC here   
  6.   
  7.     // TODO:  Return a different brush if the default is not desired   
  8.   
  9.     return  m_brushBk;  
  10.   
  11. }  
HBRUSH CqqqqqDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here // TODO: Return a different brush if the default is not desired return m_brushBk; }


截图如下:
 

 

看图说话,不解释,你懂的。

 这种方法的缺点是不具备StretchBlt函数的图片自动适应对话框(目标矩形)大小的功能。

目录
相关文章
|
API C# Windows
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
Winform中实现无边框窗体只需要设置FormBorderStyle = FormBorderStyle.None,但是无边框下我们需要保留移动窗体、拖拽调整大小、自定义美观好看的标题栏等...
2595 0
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
|
4月前
|
Windows
MFC学习之路(9)之如何使控件大小随着对话框大小自动调整
MFC学习之路(9)之如何使控件大小随着对话框大小自动调整
44 0
文字点击展开再次点击隐藏
文字点击展开再次点击隐藏
|
8月前
3D模型工具栏-背景切换
3D模型工具栏-背景切换
|
9月前
|
前端开发 定位技术
百度地图开发自定义信息窗口openInfoWindow样式的解决方案
百度地图开发自定义信息窗口openInfoWindow样式的解决方案
794 0
|
10月前
|
程序员 Windows
【windows编程之对话框】对话框原理,对话框的创建
【windows编程之对话框】对话框原理,对话框的创建
|
10月前
|
前端开发 Python
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
487 0
|
Java Android开发 Windows
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
|
搜索推荐
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
503 0
PyQt5 技巧篇-参数控制窗体右上角只显示关闭按钮实例演示
PyQt5 技术篇 - 按钮隐藏并保留位置,pyqt5设置按钮的可见度,设置按钮透明度
PyQt5 技术篇 - 按钮隐藏并保留位置,pyqt5设置按钮的可见度,设置按钮透明度
449 0
PyQt5 技术篇 - 按钮隐藏并保留位置,pyqt5设置按钮的可见度,设置按钮透明度