首页 > 代码库 > 用按钮打开对话框选取图片按大小显示在控件中

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

[cpp] view plaincopy
 
  1. 打开按钮相应消息  
[cpp] view plaincopy
 
  1. void CLoadBmpDlg::OnBtnOpenBmp()   
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4. ////选取图片文件  
[cpp] view plaincopy
 
  1.     CFileDialog file_open_dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"BMP Files(*.bmp)|*.bmp|All Files(*.*)|*.*||");  
  2.     if(file_open_dlg.DoModal() != IDOK)  
  3.     {  
  4.         return;  
  5.     }  
  6.     m_pFilePath = file_open_dlg.GetPathName();  
  7. //  ((CStatic*)GetDlgItem(IDC_STC_BMP))->SetBitmap(::LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BITMAP)));     
  8. //  HBITMAP hsource = (HBITMAP)LoadImage(AfxGetInstanceHandle(), m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);        
  9. //  HBITMAP hbmp = ((CStatic*)GetDlgItem(IDC_STC_BMP))-> SetBitmap(hsource);   
  10.     Invalidate( FALSE );  
  11. }  


///OnPaint函数

[cpp] view plaincopy
 
  1. void CLoadBmpDlg::OnPaint()  
[cpp] view plaincopy
 
  1. {  
[cpp] view plaincopy
 
    1.     //////////////////////////////////////////////////////////////  
    2.     CPaintDC bmpDC(GetDlgItem( IDC_STC_BMP ));  
    3.     CBitmap SourceImage;  
    4.     HBITMAP OldBitmap;  
    5.     CDC mDC;  
    6.     // SourceImage.LoadBitmap(image_name);  
    7.     HANDLE filehandle = (HBITMAP)LoadImage( NULL, m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );  
    8.     if ( filehandle != NULL )  
    9.     {  
    10.         BITMAP bmpInfo;  
    11.         SourceImage.Attach( filehandle );  
    12.         SourceImage.GetBitmap( &bmpInfo );  
    13.         CDC dcCompatible;  
    14.         dcCompatible.CreateCompatibleDC(&bmpDC);  
    15.         OldBitmap = (HBITMAP)dcCompatible.SelectObject(&SourceImage);  
    16.         CRect rec;  
    17.         ((CStatic *)GetDlgItem(IDC_STC_BMP))->GetClientRect(&rec);  
    18.         //      dc.BitBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, 0, 0, SRCCOPY);  
    19.                 int n = bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);  
    20. //        
    21. //      CBrush brush;   
    22. //      brush.CreatePatternBrush(&SourceImage);       
    23. //      bmpDC.FillRect(rec, &brush);          
    24. //      bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);  
    25. //      brush.DeleteObject();   
    26.         dcCompatible.SelectObject(&OldBitmap);  
    27.         SourceImage.Detach();  
    28.     }  
    29. }