天天看点

用按钮打开对话框选取图片按大小显示在控件中

打开按钮相应消息  

void CLoadBmpDlg::OnBtnOpenBmp()   

{  

    // TODO: Add your control notification handler code here  

////选取图片文件  

    CFileDialog file_open_dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"BMP Files(*.bmp)|*.bmp|All Files(*.*)|*.*||");  

    if(file_open_dlg.DoModal() != IDOK)  

    {  

        return;  

    }  

    m_pFilePath = file_open_dlg.GetPathName();  

//  ((CStatic*)GetDlgItem(IDC_STC_BMP))->SetBitmap(::LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BITMAP)));     

//  HBITMAP hsource = (HBITMAP)LoadImage(AfxGetInstanceHandle(), m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);        

//  HBITMAP hbmp = ((CStatic*)GetDlgItem(IDC_STC_BMP))-> SetBitmap(hsource);   

    Invalidate( FALSE );  

}  

///OnPaint函数

void CLoadBmpDlg::OnPaint()  

    //////////////////////////////////////////////////////////////  

    CPaintDC bmpDC(GetDlgItem( IDC_STC_BMP ));  

    CBitmap SourceImage;  

    HBITMAP OldBitmap;  

    CDC mDC;  

    // SourceImage.LoadBitmap(image_name);  

    HANDLE filehandle = (HBITMAP)LoadImage( NULL, m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );  

    if ( filehandle != NULL )  

        BITMAP bmpInfo;  

        SourceImage.Attach( filehandle );  

        SourceImage.GetBitmap( &bmpInfo );  

        CDC dcCompatible;  

        dcCompatible.CreateCompatibleDC(&bmpDC);  

        OldBitmap = (HBITMAP)dcCompatible.SelectObject(&SourceImage);  

        CRect rec;  

        ((CStatic *)GetDlgItem(IDC_STC_BMP))->GetClientRect(&rec);  

        //      dc.BitBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, 0, 0, SRCCOPY);  

                int n = bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);  

//        

//      CBrush brush;   

//      brush.CreatePatternBrush(&SourceImage);       

//      bmpDC.FillRect(rec, &brush);          

//      bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);  

//      brush.DeleteObject();   

        dcCompatible.SelectObject(&OldBitmap);  

        SourceImage.Detach();  

继续阅读