int cHeight = cRect.bottom - cRect.top; int cTop = cRect.top; int y = yClient+cTop; while(y<0) y += iHeight; int imageY = y % iHeight; int uOffsetY = cTop; int uHeight = iHeight-imageY; if(uHeight > cHeight) uHeight = cHeight; while(uHeight > 0) { //Y axis loop int cWidth = cRect.right - cRect.left; int cLeft = cRect.left; int x = xClient+cLeft; while(x < 0) x += iWidth; int imageX = x % iWidth; int uOffsetX = cLeft; int uWidth = iWidth - imageX; if(uWidth > cWidth) uWidth = cWidth; while(uWidth > 0) { //X axis loop ::BitBlt(dc, uOffsetX, uOffsetY, uWidth, uHeight, tempDC, imageX, imageY, SRCCOPY); cWidth -= uWidth; if(cWidth <= 0) break; cLeft += uWidth; x = xClient+cLeft; while(x < 0) x += iWidth; imageX = x % iWidth; uOffsetX = cLeft; uWidth = iWidth - imageX; if(uWidth > cWidth) uWidth = cWidth; } cHeight -= uHeight; if(cHeight <= 0) break; cTop += uHeight; y = yClient+cTop; while(y < 0) y += iHeight; imageY = y % iHeight; uOffsetY = cTop; uHeight = iHeight-imageY; if(uHeight > cHeight) uHeight = cHeight; } ::SelectObject(tempDC, iBitmap); ::DeleteDC(tempDC); }
bitblt我知道,但是那是从dc拷到dc, 我不懂的是怎么从内存数据-》位图-》dc
那就用这个函数,使用前需要填充BITMAPINFO结构 int SetDIBitsToDevice( HDC hdc, // handle to DC int XDest, // x-coord of destination upper-left corner int YDest, // y-coord of destination upper-left corner DWORD dwWidth, // source rectangle width DWORD dwHeight, // source rectangle height int XSrc, // x-coord of source lower-left corner int YSrc, // y-coord of source lower-left corner UINT uStartScan, // first scan line in array UINT cScanLines, // number of scan lines CONST VOID *lpvBits, // array of DIB bits CONST BITMAPINFO *lpbmi, // bitmap information UINT fuColorUse // RGB or palette indexes );
//Code come from FreeCL 2.00 for skinning
void DrawBitmap(HDC dc, HBITMAP bitmap, const RECT& cRect, int xClient = 0, int yClient = 0)
{
BITMAP bInfo;
memset(&bInfo, 0, sizeof(bInfo));
::GetObject(bitmap, sizeof(bInfo), &bInfo);
int iWidth = (int)(bInfo.bmWidth);
int iHeight = (int)(bInfo.bmHeight);//iWidth & iHeight is width & height of Image
HDC tempDC = ::CreateCompatibleDC(dc);
HGDIOBJ iBitmap = ::SelectObject(tempDC, (HGDIOBJ)bitmap);
int cHeight = cRect.bottom - cRect.top;
int cTop = cRect.top;
int y = yClient+cTop;
while(y<0)
y += iHeight;
int imageY = y % iHeight;
int uOffsetY = cTop;
int uHeight = iHeight-imageY;
if(uHeight > cHeight)
uHeight = cHeight;
while(uHeight > 0)
{ //Y axis loop
int cWidth = cRect.right - cRect.left;
int cLeft = cRect.left;
int x = xClient+cLeft;
while(x < 0)
x += iWidth;
int imageX = x % iWidth;
int uOffsetX = cLeft;
int uWidth = iWidth - imageX;
if(uWidth > cWidth)
uWidth = cWidth;
while(uWidth > 0)
{ //X axis loop
::BitBlt(dc, uOffsetX, uOffsetY, uWidth, uHeight, tempDC, imageX, imageY, SRCCOPY); cWidth -= uWidth;
if(cWidth <= 0)
break;
cLeft += uWidth;
x = xClient+cLeft;
while(x < 0)
x += iWidth;
imageX = x % iWidth;
uOffsetX = cLeft;
uWidth = iWidth - imageX;
if(uWidth > cWidth)
uWidth = cWidth;
} cHeight -= uHeight;
if(cHeight <= 0)
break;
cTop += uHeight;
y = yClient+cTop;
while(y < 0)
y += iHeight;
imageY = y % iHeight;
uOffsetY = cTop;
uHeight = iHeight-imageY;
if(uHeight > cHeight)
uHeight = cHeight;
} ::SelectObject(tempDC, iBitmap);
::DeleteDC(tempDC);
}
我不懂的是怎么从内存数据-》位图-》dc
int SetDIBitsToDevice(
HDC hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
DWORD dwWidth, // source rectangle width
DWORD dwHeight, // source rectangle height
int XSrc, // x-coord of source lower-left corner
int YSrc, // y-coord of source lower-left corner
UINT uStartScan, // first scan line in array
UINT cScanLines, // number of scan lines
CONST VOID *lpvBits, // array of DIB bits
CONST BITMAPINFO *lpbmi, // bitmap information
UINT fuColorUse // RGB or palette indexes
);
static HBITMAP rgb_2_bitmap(unsigned char* src, HDC dc)
{
int w,h;
BITMAPINFOHEADER bmih;
BITMAPINFO bmi; h = sys_height();
w = sys_width(); memset(&bmih,0,sizeof(BITMAPINFOHEADER));
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = w;
bmih.biHeight = h;
bmih.biPlanes = 1;
bmih.biSizeImage = h*w*3;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB; memset(&bmi,0,sizeof(BITMAPINFO));
bmi.bmiHeader = bmih; return CreateDIBitmap(dc, &bmih, CBM_INIT, bmpdata, &bmi, DIB_RGB_COLORS);
}static void draw(void)
{
HDC dc;
HBITMAP bitmap;
HDC memDC; int w, h;
unsigned char* tmp; h = sys_height();
w = sys_width(); dc = GetDC(hwnd);
memDC = CreateCompatibleDC (dc);
bitmap = rgb_2_bitmap(tmp, dc);
SelectObject (memDC, bitmap);
memset(bmpdata, 0x75, 4096); h = BitBlt(dc, 0, 0, w, h, memDC, 0, 0, SRCCOPY);
DeleteDC(memDC);
DeleteObject(bitmap);
ReleaseDC(hwnd, dc);
}
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = w * h *3;
SetDIBitsToDevice(dc, 0, 0, w, h, 0, h-1, 0, h, buf, &bi, DIB_RGB_COLORS);