MFC图片浏览器,在Static控件上显示图片,并使调整图片使其适应窗口大小

最近在用MFC写了一个图片浏览器,老实说,被图像大小调整磨得筋疲力竭,好在最终还是解决了。这里贴部分显图代码,留给大家参考参考

        //函数UpdateDrawRect,目的是更新实际绘图区域
        //m_img_rect是指图像的原始大小矩形
    	//m_Draw_rect是指实际绘图时用到的矩形大小
    	//m_PicCtl_rect是指可用于绘图的静态控件的大小矩形
    int img_width = m_Img_rect.Width();
	int img_height = m_Img_rect.Height();
	int actual_width = m_PicCtl_rect.Width();
	int actual_height =m_PicCtl_rect.Height();

	float fscaleimgHW = static_cast<float>(img_height) / static_cast<float>(img_width);
	float fscaleimgWH = static_cast<float>(img_width) / static_cast<float>(img_height);

	int cur_W = 0, cur_H = 0;
	float fscale = 1.0;
	if (img_width > actual_width&&img_height <= actual_height)//图像宽度大于客户区且图像高小于等于客户区
	{
		cur_W = actual_width;
		cur_H = static_cast<int>(cur_W*fscaleimgHW);
	
		m_Draw_rect = CRect((actual_width - cur_W) / 2,
			(actual_height - cur_H) / 2,
			(actual_width + cur_W) / 2,
			(actual_height + cur_H) / 2);
	}
	else if (img_width <= actual_width&&img_height > actual_height)//图像宽度小于等于客户区且图像高于客户区
	{
		cur_H = actual_height;
		cur_W = static_cast<int>(cur_H*fscaleimgWH);
		m_Draw_rect = CRect((actual_width - cur_W) / 2,
			(actual_height - cur_H) / 2,
			(actual_width + cur_W) / 2,
			(actual_height + cur_H) / 2);
	}
	else if (img_width <= actual_width&&img_height < actual_height)//图像宽度小于等于客户区且图像高小于客户区
	{
		m_Draw_rect = CRect((actual_width - img_width) / 2,
			(actual_height - img_height) / 2,
			(actual_width + img_width) / 2,
			(actual_height + img_height) / 2);
	}
	else//图像宽大于客户区且图像高大于客户区
	{
		
		float fscaleCWH = static_cast<float>(m_PicCtl_rect.Width()) / static_cast<float>(m_PicCtl_rect.Height());
		float fscaleIWH = static_cast<float>(m_Img_rect.Width()) / static_cast<float>(m_Img_rect.Height());
		bool Choose = (fscaleCWH - fscaleIWH) >0 ? false : true;
		if (Choose)
		{

			fscaleimgHW-fscaleimgWH>0 ?cur_H = actual_height:cur_W=actual_width;
			fscaleimgHW-fscaleimgWH>0 ?cur_W =static_cast<int>(cur_H*fscaleimgWH):cur_H=static_cast<int>(cur_W*fscaleimgHW);
		}
		else
		{
			fscaleimgHW-fscaleimgWH>0 ?cur_W=actual_width:cur_H = actual_height;
			fscaleimgHW-fscaleimgWH>0 ?cur_H=static_cast<int>(cur_W*fscaleimgHW): cur_W = static_cast<int>(cur_H*fscaleimgWH);
		}
		m_Draw_rect = CRect((actual_width - cur_W) / 2,
			(actual_height - cur_H) / 2,
			(actual_width + cur_W) / 2,
			(actual_height + cur_H) / 2);
	}

更新完Draw_rect,在OnPaint中加入以下代码,即可在STATIC控件上显示图片了:

        //m_image是一个CImage类变量,确保它已经load了一张图片
        //m_PicCtl是一个静态控件的控制变量
        //使用GetDlgItem方法以获得这个变量
        CDC memDC;
		CBitmap membitmap;
		CDC * pDC = m_PicCtl->GetDC();
		memDC.CreateCompatibleDC(pDC);
		memDC.SetStretchBltMode(HALFTONE);
		membitmap.CreateCompatibleBitmap(pDC, m_Client_rect.Width(), m_Client_rect.Height());
		memDC.SelectObject(&membitmap);
		memDC.FillSolidRect(&m_PicCtl_rect, RGB(245, 245, 245));
		m_image.Draw(memDC, m_Draw_rect, m_Img_rect);
		pDC->BitBlt(0, 0, m_PicCtl_rect.Width(), m_PicCtl_rect.Height(), &memDC, 0, 0, SRCCOPY);

		ReleaseDC(pDC);
		memDC.DeleteDC();
		membitmap.DeleteObject();

你可能感兴趣的:(MFC图片浏览器,在Static控件上显示图片,并使调整图片使其适应窗口大小)