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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值