MFC对话框背景

来源:互联网 发布:什么是雅思网络课程 编辑:程序博客网 时间:2024/05/16 14:31
在OnPaint 的else 中加入以下代码

CPaintDC dc(this);//窗口DC
CBitmap m_bmp;//位图变量
m_bmp.LoadBitmap(IDB_BITMAP5);
CDC memdc; //临时DC
memdc.CreateCompatibleDC(&dc);//创建临时DC
memdc.SelectObject(&m_bmp);//选中位图对象
int width, height;//定义位图宽度和高度
BITMAP bmp;
m_bmp.GetBitmap(&bmp);//获取位图信息
width = bmp.bmWidth;
height = bmp.bmHeight;
CRect rect;
this->GetClientRect(&rect);//获取窗体大小
//将位图绘制在窗体上做背景
dc.StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&memdc,0,0,width,height,SRCCOPY);


举例(两张图片)

void CAUTOCUBEDlg::OnPaint()
{
            if (IsIconic())
            {
                        CPaintDC dc(this); // 用于绘制的设备上下文

                        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

                        // 使图标在工作区矩形中居中
                        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;

                        // 绘制图标
                        dc.DrawIcon(x, y, m_hIcon);
            }
            else
            {
                        CPaintDC dc(this); // 用于绘制的设备上下文
                        CBitmap m_bmp;//位图变量
                        CBitmap m_bmp2;//位图变量
                        m_bmp.LoadBitmap(IDB_BITMAP5);
                        m_bmp2.LoadBitmap(IDB_BITMAP8);
                        CDC memdc,memdc2; //临时DC
                        memdc.CreateCompatibleDC(&dc);//创建临时DC
                        memdc2.CreateCompatibleDC(&dc);//创建临时DC
                        memdc.SelectObject(&m_bmp);//选中位图对象
                        memdc2.SelectObject(&m_bmp2);//选中位图对象
                        int width, height, width2, height2;//定义位图宽度和高度
                        BITMAP bmp,bmp2;
                        m_bmp.GetBitmap(&bmp);//获取位图信息
                        m_bmp2.GetBitmap(&bmp2);//获取位图信息
                        width                = bmp.bmWidth;
                        height              = bmp.bmHeight;
                        width2  = bmp2.bmWidth;
                        height2            = bmp2.bmHeight;
                        CRect rect,rect2;
                        this->GetClientRect(&rect);//获取窗体大小
//将位图绘制在窗体上做背景
//设置图像的大小位置                      dc.StretchBlt(rect.left,rect.top,rect.Width(),160,&memdc,0,0,width,height,SRCCOPY);                        dc.StretchBlt(rect.left,160,rect.Width(),rect.Height()-160,&memdc2,0,0,width2,height2,SRCCOPY);
 }


}

效果图:
原创粉丝点击