GDI+ and GDI (tiling and stretching) PNG background image texture method

Article source: http://blog.csdn.net/cddchina/article/details/25394143

Author: cddchina

In order to reduce the size of the client, consider changing some background images to tiled display.

GDI+ is a new interface provided by Microsoft in operating systems after Windows 2000. It is based on object-oriented, while GDI is based on functions.

It is recommended that you use GDI+ to draw pictures. GDI+ is easy to use. The following is how GDI+ and GDI are used.

Instructions for using 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);//The current position of the dialog box

 

        Graphics  graphics(dc.m_hDC);

        ImageAttributes  imAtt;

        imAtt.SetWrapMode(WrapModeTile); //Stretch the picture

        // stretch the image

        Bitmap backgroundtop(L".\\classroom\\class_toolbar_top.png"); //Load PNG image

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

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

 

        ////////////// tile background image

       // Bitmap backgroundmiddle(L".\\room\\BgBlackboard.png");//Load PNG image

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

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

        CDialog::OnPaint();

    }

}

 

GDI uses:

GDI is more complicated to operate.

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"); //Load PNG image

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

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

        MemDC.CreateCompatibleDC(pDC); // Display picture

        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();

    }

}

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564709&siteId=291194637