本文为本人实际开发中遇到的实际问题,然后经过一番努力后(在网上找相关资料,问高手等方式)终于将问题解决了,为了备忘,所以有此文的存在。
当然,希望能给被同样问题困扰的朋友带来帮助,代码随便拿去用(如果有用!)。
方法一:
比较简单,局限性很大(不能在同一次操作中对拉伸后的位图继续画线等操作)。
CRect rectMain;
GetClientRect(&rectMain);
//创建位图对象
CBitmap bitmap;
if (!bitmap.m_hObject)
{
bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_BITMAP1));
}
//定义位图变量
HBITMAP hBmp, pOldbkbmp;
BITMAP bmpInfo;
bitmap.GetObject(sizeof(bmpInfo), &bmpInfo);//将位图对象的图片放入到位图变量中
CDC dcBuffer;
dcBuffer.CreateCompatibleDC(&dc);//创建系统兼容DC
pOldbkbmp = (HBITMAP)dcBuffer.SelectObject(bitmap);//将位图变量选入到内存环境
dc.StretchBlt(rectMain.left, rectMain.top, rectMain.Width(), rectMain.Height(), &MemeDc, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
方法二:
CRect rectMain;
GetClientRect(&rectMain);
//创建位图对象
CBitmap bitmap;
if (!bitmap.m_hObject)
{
bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_BITMAP1));
}
//定义位图变量
HBITMAP hBmp, pOldbkbmp;
BITMAP bmpInfo;
bitmap.GetObject(sizeof(bmpInfo), &bmpInfo);//将位图对象的图片放入到位图变量中
CDC dcBuffer;
dcBuffer.CreateCompatibleDC(&dc);//创建系统兼容DC
pOldbkbmp = (HBITMAP)dcBuffer.SelectObject(bitmap);//将位图变量选入到内存环境
ResizeBitmap(bitmap, rectMain.Width() - bmpInfo.bmWidth, rectMain.Height() - bmpInfo.bmHeight, hBmp);
pOldbkbmp = (HBITMAP)dcBuffer.SelectObject(hBmp);
//在拉伸后位图上画线
CPen penTemp;
penTemp.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
dcBuffer.SelectObject(&penTemp);
dcBuffer.MoveTo(rectMain.left + 10, rectMain.top + 10);
dcBuffer.LineTo(rectMain.left + 200, rectMain.top + 10);
dc.BitBlt(rectMain.left, rectMain.top, rectMain.Width(), rectMain.Height(), &dcBuffer, 0, 0, SRCCOPY);
dcBuffer.SelectObject(pOldbkbmp);
dcBuffer.DeleteDC();
HBITMAP CMyStatusBar::ResizeBitmap(HBITMAP hbitmap, int addwidth, int addheight, HBITMAP &hBmp)
{
LPSTR lpBmpData = NULL;
BITMAP Bitmap;
GetObject(hbitmap, sizeof(BITMAP), &Bitmap);
long width = Bitmap.bmWidth;
long height = Bitmap.bmHeight;
//prepare the bitmap attributes
BITMAPINFOHEADERbi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = ((Bitmap.bmWidth * 32 + 31)& ~31) / 8 * Bitmap.bmHeight;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
HANDLE hDib;
hDib = GlobalAlloc(GHND, bi.biSizeImage + 40);
LPBITMAPINFOHEADERlpbi = NULL;
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
HDC hDC;
hDC = ::GetDC(NULL);
GetDIBits(hDC, hbitmap, 0, (UINT)Bitmap.bmHeight, (LPSTR)lpbi + 40, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
lpBmpData = (char*)lpbi;
BITMAPINFOHEADER bij;
bij.biSize = sizeof(BITMAPINFOHEADER);
bij.biWidth = Bitmap.bmWidth + addwidth;
bij.biHeight = Bitmap.bmHeight + addheight;
bij.biPlanes = 1;
bij.biBitCount = 32;
bij.biCompression = BI_RGB;
bij.biSizeImage = ((bij.biWidth * 32 + 31)& ~31) / 8 * bij.biHeight;
bij.biXPelsPerMeter = 0;
bij.biYPelsPerMeter = 0;
bij.biClrUsed = 0;
bij.biClrImportant = 0;
HANDLE hDibj;
hDibj = GlobalAlloc(GHND, bij.biSizeImage + 40);
LPBITMAPINFOHEADERlpbij;
lpbij = (LPBITMAPINFOHEADER)GlobalLock(hDibj);
*lpbij = bij;
CDC * pDC = CWnd::GetDesktopWindow()->GetDC();
hBmp = CreateDIBSection(pDC->GetSafeHdc(), (BITMAPINFO *)lpbij, DIB_RGB_COLORS, (void**)(lpbij), NULL, 0);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap *pSave = dcMem.SelectObject(CBitmap::FromHandle(hBmp));
::SetStretchBltMode(dcMem.GetSafeHdc(), COLORONCOLOR);
StretchDIBits(dcMem.GetSafeHdc(), 0, 0, bij.biWidth, bij.biHeight,
0, 0, Bitmap.bmWidth, Bitmap.bmHeight,
(lpBmpData + 40), (BITMAPINFO*)lpbi, DIB_RGB_COLORS, SRCCOPY);
dcMem.SelectObject(pSave);
hDibj = GlobalFree(hDibj);
hDib = GlobalFree(hDib);
CWnd::GetDesktopWindow()->ReleaseDC(pDC);
return hBmp;
}