您的位置:首页 > 其它

关于WTL中显示图片问题

2010-06-07 18:04 633 查看
CBitmap 就可以了
另外参考的帖子-----------------------
基于MFC控件STATIC显示图片数据
1. 在 OnInitDialog 中加入
GetDlgItem(IDC_MY_PIC)->ModifyStyle ( SS_TYPEMASK, SS_OWNERDRAW );
2. 在butto 按钮中加入以下,可以显示多中类型的图片资源
void CDlgpicDlg::OnTest()
{
// TODO: Add your command handler code here
CFileDialog fileDlg(TRUE,NULL,NULL,
OFN_ALLOWMULTISELECT,
_T("Picture Files (*.bmp *.ico *.jpg)|*bmp;*.ico;*jpg|All Files (*.*)|*.*||"),
AfxGetMainWnd());
CString pathName;
if(fileDlg.DoModal ()==IDOK)
{
POSITION mPos=fileDlg.GetStartPosition();
while(mPos!=NULL)
{
pathName=fileDlg.GetNextPathName(mPos);
if(m_pict.LoadPicture(pathName.GetBuffer(pathName.GetLength())))
{
// 防止有透明图片,消除已有的图片信息
CPaintDC dc(this); // device context for painting
CRect rc;
GetDlgItem(IDC_MY_PIC)->GetWindowRect(&rc);
dc.FillSolidRect(rc, RGB(0, 0, 0));
Invalidate();
}
}
}
}
3.给父窗口增加ON_WM_DRAWITEM消息映射函数 在OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)消息映射函数里面绘制控件
void CDlgpicDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if(nIDCtl == IDC_MY_PIC)
{
if ( m_pict.m_pic)
{

RECT rect;
TRACE("--hell-- test---");
HWND hwnd = ::GetDlgItem(this->GetSafeHwnd(), IDC_MY_PIC);

HDC hDC = ::GetDC(::GetDlgItem(this->GetSafeHwnd(),IDC_MY_PIC));
//Get the DC for the CPicture Box
::GetClientRect(::GetDlgItem(this->GetSafeHwnd(), IDC_MY_PIC), &rect);
//Get dimensions of it
m_pict.DrawPicture(hDC, 0, 0, rect.right - rect.left ,rect.bottom - rect.top );
::ReleaseDC(::GetDlgItem(this->GetSafeHwnd(),IDC_MY_PIC), hDC);

}
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
总结: 如果在static中需要显示图片,在ONPAINT 中绘画 没有效果,需要这么画:
1)给static控件增加SS_OWNERDRAW属性 GetDlgItem(IDC_MY_PIC)->ModifyStyle ( SS_TYPEMASK, SS_OWNERDRAW );
2)给父窗口增加ON_WM_DRAWITEM消息映射函数
3)在OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)消息映射函数里面绘制控件
例如:下面给对话框里的IDC_STATIC_DRAW控件画个蓝色的背景
void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl == IDC_STATIC_DRAW)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
pDC->FillSolidRect(10, 10, lpDrawItemStruct->rcItem.right - 20, lpDrawItemStruct->rcItem.bottom - 20, RGB(0, 0, 255));
return;
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
另外,还可以这么画
从CStatic继承一个新类,然后增加WM_PAINT消息处理,在OnPaint里面画
例如:
class CMyStatic : public CStatic
{
...
afx_msg void OnPaint();
};
void CMyStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rc;
GetClientRect(rc);
dc.FillSolidRect(rc, RGB(0, 0, 255));
}

CFileDialog dlg( TRUE,NULL,NULL,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY,_T("image file (*.jpg;*.bmp)/0*.jpg;*.bmp/0All Files (*.*)/0*.*||"));
CStatic *pStaBmp = new CStatic;
if( dlg.DoModal() == IDOK )
{

LPTSTR strFilePath = dlg.m_ofn.lpstrFile;
if (pStaBmp)
{
CBitmap hbmp;
HBITMAP hbitmap;
HINSTANCE hIns = _Module.GetResourceInstance();
HWND hWnd = pStaBmp->Create(m_hWnd, rcCell, NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP);
hbitmap=(HBITMAP)::LoadImage(hIns,strFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); if(hbitmap==NULL)
return FALSE;
hbmp.Attach(hbitmap);
pStaBmp->ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE);

pStaBmp->SetBitmap(hbitmap);

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