#include
2. In the SampleDlg.h file add
CString m_csPath;
3. In the OnPaint function
void CSampleDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect1;
CWnd *pWnd=NULL;
pWnd = GetDlgItem(IDC_STATIC_PIC);
if( NULL != pWnd && NULL != pWnd->GetSafeHwnd() )
{
pWnd->GetWindowRect( rect1 );
}
ScreenToClient( rect1 );
HDC hDC = dc.GetSafeHdc();
CImage img;
if( !m_csPath.IsEmpty() )
{
img.Load( m_csPath );
if( !img.IsNull() )
{
int nWidth1 = img.GetWidth();
int nHeight1 = img.GetHeight();
SetStretchBltMode(hDC, HALFTONE);
img.StretchBlt( hDC, rect1.left, rect1.top, rect1.Width(), rect1.Height(), 0, 0, nWidth1, nHeight1, SRCCOPY );
}
}
}
In the button click event
void CSampleDlg::OnBnClickedButton1()
{
TCHAR szFilters[]= _T("Image Files (*.bmp,*.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*||");
CFileDialog objFileDialog( TRUE,0, 0,
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
CString csPath;
objFileDialog.m_ofn.lpstrTitle = _T("Select the image");
if( objFileDialog.DoModal() == IDOK)
{
m_csPath = objFileDialog.GetPathName();
if( !m_csPath.IsEmpty() )
{
CImage img;
img.Load( m_csPath );
if( !img.IsNull() )
{
//Invalidate();
CRect rect;
GetDlgItem( IDC_STATIC_PIC )->GetWindowRect( &rect );
ScreenToClient( rect );
InvalidateRect( rect, FALSE);
}
}
}
}
No comments:
Post a Comment