当前位置: 代码迷 >> WinCE >> 如何给对话框加个背景图片
  详细解决方案

如何给对话框加个背景图片

热度:53   发布时间:2016-04-28 13:09:24.0
怎么给对话框加个背景图片啊
用VC的对话框做了一个显示界面,但是VC自带的功能太少了,能不能给背景加一点图片什么,让界面好看一点

------解决方案--------------------
使用IPICTURE
用法:
(1)
dlg.cpp
#include "olectl.h"
#include "ole2.h"

(2)dlg.h public定义
IPicture* pPicture;
IStream* pStream;
HGLOBAL hMem;
(3)添加或修改一下函数
BOOL dlg::LoadAnImage(TCHAR* FileName)
{
// Use IPicture stuff to use JPG / GIF files
CFile file;
if( !file.Open( FileName, CFile::modeRead) )
return FALSE;

DWORD m_nFileLen;
m_nFileLen = file.GetLength();

hMem = ::GlobalAlloc( GMEM_MOVEABLE, m_nFileLen );
LPVOID lpBuf = ::GlobalLock( hMem );
if( file.Read( lpBuf, m_nFileLen ) != m_nFileLen )
return FALSE;
file.Close();

::GlobalUnlock( hMem );

if ( CreateStreamOnHGlobal( hMem, TRUE, &pStream ) !=S_OK )
return FALSE;

if ( OleLoadPicture( pStream, m_nFileLen, TRUE, IID_IPicture, ( LPVOID * )&pPicture ) !=S_OK )
return FALSE;

return TRUE;
}

dlg::~CiPictureTestDlg()
{
pPicture->Release();
pStream->Release();
GlobalFree(hMem);

}

bool CiPictureTestDlg::DrawPicture(CDC* pdc, IPicture* pic, const CRect& rcBounds)
{
CDC MemDC; //定义内存DC
MemDC.CreateCompatibleDC(NULL); //注意不要写成.CreateCompatibleDC(pdc);
MemDC.SetBkMode(TRANSPARENT); //设置透明,Textout写字没有白色的背景块
//得到图片的宽度和高度
SIZE sizeInHiMetric;
pic->get_Width(&sizeInHiMetric.cx); //获取加载进pic的图像宽度和高度
pic->get_Height(&sizeInHiMetric.cy);
CBitmap bmpFace; //创建bitmap
bmpFace.CreateCompatibleBitmap(pdc, rcBounds.Width(), rcBounds.Height()); 
//将这幅图片选入内存DC,原来memdc不起作用就是因为,bmp图片没有选入到dc中.郁闷 
CBitmap* pOldBmp = MemDC.SelectObject(&bmpFace); 
//把整个图片 {0,cy,cx,-cy} .拷贝到 memdc 的rcBounds 区域 
pic->Render(MemDC.GetSafeHdc(), rcBounds.left, rcBounds.top, rcBounds.right, rcBounds.bottom, 
0, sizeInHiMetric.cy, sizeInHiMetric.cx, -sizeInHiMetric.cy, &rcBounds);
MemDC.SetTextColor(RGB(0,0,0));

showtext.Format(TEXT("程序测试"));
MemDC.TextOut(15,20,showtext); 
//拷贝memdc (0,0,rcbounds.width,rcbounds.height) 区域到 pdc
pdc->BitBlt(0,0,rcBounds.Width(),rcBounds.Height(),&MemDC,0,0,SRCCOPY); //拷贝 memdc - > dc
MemDC.SelectObject(pOldBmp);
bmpFace.DeleteObject();
MemDC.DeleteDC();
//pdc->BitBlt(0,0,rcBounds.Width(),rcBounds.Height(),&MemDC,0,0,SRCCOPY);
return true;
}

void dlg::OnPaint()
{
CPaintDC dc(this);

int x=0,y=0;
CRect rect;
GetClientRect(&rect);
CDC *pDC = CDC::FromHandle(dc.GetSafeHdc());
DrawPicture(pDC,pPicture,&rect);
CDialog::OnPaint();
}

BOOL dlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
RECT rect;
GetClientRect(&rect);
DrawPicture(pDC,pPicture,rect);
return TRUE;//
//return CDialog::OnEraseBkgnd(pDC);
}

(4)OnInitDialog函数中调用
LoadAnImage(L"3.JPG");//例子,可以将路径改成绝对路径