先用MFC ClassWizard给你的工程对话框类加上函数DrawInTitle(),然后添加代码,如下:
BOOL CMmDlg::DrawOnTitle()
{
HDC hdc=::GetWindowDC(m_hWnd);
CDC* pdc=CDC::FromHandle(hdc);
CRect rect;
this->GetWindowRect(&rect);
rect.bottom = rect.top+GetSystemMetrics(SM_CYSIZE)+3;
IPicture *pPic;
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
if (file.Open("f://02.bmp",CFile::modeRead)&&file.GetStatus("f://02.bmp",fstatus)&&((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth); //hmWidth等于图片的实际宽度,单位未知,
pPic->get_Height(&hmHeight); //hmHeight等于图片的实际高度
//GetDlgItem(IDC_STATIC_PIC)->GetWindowRect(rect);
if(FAILED(pPic->Render(*pdc,0,0,(DWORD)(rect.right-rect.left),(DWORD)(rect.bottom-rect.top),0,hmHeight,hmWidth,-hmHeight,NULL)))
{
//AfxMessageBox("Failed To Render The picture!");
pPic->Release();
file.Close();
ReleaseDC(pdc);
return 0;
}
else
{
pPic->Release();
file.Close();
ReleaseDC(pdc);
return TRUE;
}
}
}
else
{
//AfxMessageBox("Error Loading Picture From Stream!");
file.Close();
return FALSE;
}
}
}
else
{
//AfxMessageBox("Can't Open Image File!");
return FALSE;
}
}
上面代码中用f://02.bmp作为图画上去,先最好把该文件加工成标题栏差不多大小和比例,以免显示时产生变形。这里用.bmp、.jpg或.gif格式的图片都可以。
接着再用MFC ClassWizard给你的对话框工程添加WM_CTLCOLOR消息,并在它的处理函数中加上一行代码:
HBRUSH CMmDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(!DrawOnTitle()) AfxMessageBox("绘图失败"); //加入这行
// TODO: Return a different brush if the default is not desired
return hbr;
}