GDI+ 与 GDI (平铺与拉伸)PNG背景图贴图片的方法

GDI+ 与 GDI (平铺与拉伸)PNG背景图贴图片的方法

GDI+是微软在Windows2000以后的操作系统中提供的新接口,它是基于面向对象的,而GDI是基于函数的。

建议大家使用GDI+画图,GDI+使用简单,下面是GDI+与GDI使用的方式。

GDI+使用说明:

void CDlgClassroom::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

    {

        CPaintDC  dc(this);

        CRect  rect;

        GetClientRect(&rect);//对话框当前位置

 

        Graphics  graphics(dc.m_hDC);

        ImageAttributes  imAtt;

        imAtt.SetWrapMode(WrapModeTile);    //拉伸图片

        //拉伸图片

        Bitmap  backgroundtop(L".\\classroom\\class_toolbar_top.png");   //加载PNG图片

        graphics.DrawImage(&backgroundtop,Rect(0,0,rect.Width(),34),

            0, 0,backgroundtop.GetWidth(),backgroundtop.GetHeight(),UnitPixel,&imAtt);

 

        ///////////////平铺背景图

       // Bitmap backgroundmiddle(L".\\room\\BgBlackboard.png");//加载PNG图片

       // TextureBrush brush(&backgroundmiddle, WrapModeTile/*FlipXY*/ );

       // graphics.FillRectangle(&brush, RectF(0.0f, 0.0f,rect.Width(),rect.Height()));

        CDialog::OnPaint();

    }

}

 

GDI使用:

GDI操作起来就比较繁琐。

voidCDlgClassroom::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);

        intx = (rect.Width() - cxIcon + 1) / 2;

        inty = (rect.Height() - cyIcon + 1) / 2;

 

        // Draw the icon

        dc.DrawIcon(x, y, m_hIcon);

    }

    else

    {

        CPaintDC  dc(this);

        CRect  rect;

        GetClientRect(&rect);//对话框当前位置

 

        //平铺图片

        CDC   MemDC;

        CBitmap  cbitmap;

        CBitmap* pOldBmp = NULL; 

        HBITMAP  hBitmap;

        CDC * pDC = GetDC();

        //顶部背景图

        Bitmap  backgroundtop(L".\\classroom\\class_toolbar_top.png");   //加载PNG图片

        backgroundtop.GetHBITMAP(Color(0,0,0),&hBitmap);            // Bitmap To HBITMAP

        CBitmap* cBitmap = CBitmap::FromHandle(hBitmap);            // HBITMAP To CBitmap *

        MemDC.CreateCompatibleDC(pDC);                              // 显示图片

        pOldBmp = MemDC.SelectObject(cBitmap);

        dc.StretchBlt(0,0,rect.Width(),34, &MemDC, 0, 0,backgroundtop.GetWidth() ,backgroundtop.GetHeight(),SRCCOPY);

        MemDC.SelectObject(pOldBmp);//releaseDC

        MemDC.DeleteDC(); 

        cBitmap->DeleteObject();

        CDialog::OnPaint();

    }

}

 

你可能感兴趣的:(GDI+ 与 GDI (平铺与拉伸)PNG背景图贴图片的方法)