zoukankan      html  css  js  c++  java
  • 在对话框中设置背景的三种方法 .

    方法一:

    在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. }  


    执行效果截图如下:

    方法二:

    在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. }  


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

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

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

    方法三:

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

     具体是:

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

    1. public:  
    2.     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();      



     

    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. }  

    效果如下图所示:

    注意这个函数里面的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. }  


    截图如下:
     

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

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

  • 相关阅读:
    Java:类与继承(隐藏和覆盖的问题)
    Java中的static关键字解析
    面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)
    面向对象要点(this关键字)
    急须知道postman RSA加密的方式
    RSA加密原理
    postman获取变量
    Mysql通过cmd访问
    一个简单的postman实例
    sum(coalesce(adjust_value,prediction_value))
  • 原文地址:https://www.cnblogs.com/lidabo/p/2576172.html
Copyright © 2011-2022 走看看