您的位置:首页 > 其它

使用查找对话框后,另一对话框的位图背景加载失败

2010-09-05 13:38 295 查看
问题:

工程中有多个对话框

我在一个对话框中 使用查找对话框设置位图后

打开另一对话框后,发现另一对话框加载位图失败

出现断言错误

CBitmap::GetBitmap中 { ASSERT(m_hObject != NULL);

原因:

HBITMAP hBmp=(HBITMAP)::LoadImage(NULL,L"res//7.jpg",IMAGE_BITMAP,rect.Width(),rect.Height(),LR_LOADFROMFILE);

CBitmap *p_bit=CBitmap::FromHandle(hBmp);

因为这个对话框使用了相对目录,这样呢,当另一个对话框使用了查找对话框后,当前路径发生了变化,而当前路径下没有所要打开的资源,所以就出现了打开位图的错误

解决:

将路径再次定位到资源所在的地方即可:

void WeiKuangKe::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect(&rect);
CString currentDirectory;

GetModuleFileName(NULL,currentDirectory.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
currentDirectory.ReleaseBuffer();
int pos=currentDirectory.ReverseFind('//');
currentDirectory=currentDirectory.Left(pos);

currentDirectory+=L"//res//13.bmp";

HBITMAP hBmp=(HBITMAP)::LoadImage(NULL,currentDirectory,IMAGE_BITMAP,rect.Width(),rect.Height(),LR_LOADFROMFILE);
CBitmap *p_bit=CBitmap::FromHandle(hBmp);
BITMAP bitmap;
p_bit->GetBitmap(&bitmap);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);

CBitmap *pbmpOld=dcMem.SelectObject(p_bit);

dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
dcMem.SelectObject(pbmpOld);

dcMem.DeleteDC();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: