可以简单的设置rgb/w/h的值。
static void changeBackground(HWND hwnd) { /* RECT rect; GetClientRect(hwnd, &rect); int cxClient = rect.right; int cyClient = rect.bottom; */ HDC hdc = GetDC(hwnd);//获取当前的显示设备上下文 int w = 200; int h = 100; char* rgb = (char*)malloc(3*w*h); for (int i=0; i<w*h; i++) { rgb[i*3+0] = 0x12; rgb[i*3+1] = 0x78; rgb[i*3+2] = 0x34; } HDC hdcsource = CreateCompatibleDC(NULL);//创建存放图象的显示缓冲 HBITMAP bitmap = CreateCompatibleBitmap(hdc, w, h); SelectObject(hdcsource, bitmap); //将位图资源装入显示缓冲 SetStretchBltMode(hdcsource, COLORONCOLOR); BITMAPINFO bmi; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = w; bmi.bmiHeader.biHeight = h; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biBitCount = 24; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biClrUsed = 0; bmi.bmiHeader.biClrImportant = 0; bmi.bmiHeader.biSizeImage = 0; StretchDIBits(hdcsource, 0, 0, w, h, 0, 0, w, h, rgb, &bmi,DIB_RGB_COLORS, SRCCOPY); BitBlt(hdc,0,0,w,h, hdcsource,0,0,SRCCOPY);//将图象显示缓冲的内容直接显示到屏幕 DeleteObject(bitmap); DeleteDC(hdcsource); ReleaseDC(hwnd, hdc); free(rgb); }