MFC位图自适应Picture Control控件大小

试了n个方法,查了n多资料。终于弄出来了~~~~

void CTestDlg::AddPitcure()
{

	CBitmap bitmap;  
    //加载指定位图资源 Bmp图片ID  
    bitmap.LoadBitmap(IDB_BITMAP_MAIN);    
    //获取对话框上的句柄 图片控件ID  
    CStatic *p=(CStatic *)GetDlgItem(IDC_MAINMode);    
    //设置静态控件窗口风格为位图居中显示   
    p->ModifyStyle(0xf,SS_BITMAP|SS_CENTERIMAGE);   
    //将图片设置到Picture控件上  
    p->SetBitmap(bitmap);   

	BITMAP bmpInfo; 
    bitmap.GetBitmap(&bmpInfo); 
    CDC dcMemory; 
    CDC* pDC=GetDlgItem(IDC_MAINMode)->GetDC(); 
    dcMemory.CreateCompatibleDC(pDC); 
    CBitmap* pOldBitmap = dcMemory.SelectObject(&bitmap); 
    CRect rect; 
    m_Picture.GetClientRect(&rect);
    int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2; 
    int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2; 
    //pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY);  
    pDC->SetStretchBltMode(COLORONCOLOR); 
    pDC->StretchBlt(0, 0,rect.Width(),rect.Height(),&dcMemory,0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight,SRCCOPY); 
    dcMemory.SelectObject(pOldBitmap); 
    ReleaseDC(pDC); 
}

AddPitcure()放在OnPaint消息函数里面,且前面需加UpdateWindow();,不然添加的位图会显示不全。

你可能感兴趣的:(VS2010工程知识,MFC)