int SetDIBitsToDevice( HDC hdc , int XDest , int YDest, DWORD dwWidth , DWORD dwHeight , int XSrc , int YSrc , UINT uStartScan , UINT cScanLines , CONST VOID *lpvBits , CONST BITMAPINFO *lpbmi , UINT fuColorUse );hdc には、DIB をコピーするデバイスコンテキストを指定します
#include <windows.h> BITMAPFILEHEADER bmpFileHeader; BITMAPINFO bmpInfo; BYTE * bPixelBits; LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) { HDC hdc; PAINTSTRUCT ps; switch (msg) { case WM_DESTROY: free(bPixelBits); PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hWnd , &ps); SetDIBitsToDevice( hdc , 0 , 0 , bmpInfo.bmiHeader.biWidth , bmpInfo.bmiHeader.biHeight , 0 , 0 , 0 , bmpInfo.bmiHeader.biHeight , bPixelBits , &bmpInfo , DIB_RGB_COLORS ); EndPaint(hWnd , &ps); return 0; } return DefWindowProc(hWnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS winc; HANDLE hFile; DWORD dwBytes; hFile = CreateFile(lpCmdLine , GENERIC_READ , 0 , NULL , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , NULL); if (hFile == INVALID_HANDLE_VALUE) return 1; ReadFile(hFile , &bmpFileHeader , sizeof (BITMAPFILEHEADER) , &dwBytes , NULL); if (bmpFileHeader.bfType != 0x4D42) { MessageBox(NULL , TEXT("This is not a bitmap file") , NULL , MB_OK); return 1; } ReadFile(hFile , &bmpInfo , sizeof (BITMAPINFOHEADER) , &dwBytes , NULL); bPixelBits = (BYTE *) malloc (bmpFileHeader.bfSize - bmpFileHeader.bfOffBits); ReadFile(hFile ,bPixelBits , bmpFileHeader.bfSize - bmpFileHeader.bfOffBits , &dwBytes , NULL); CloseHandle(hFile); winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return 1; hWnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hWnd == NULL) return 1; while (GetMessage(&msg , NULL , 0 , 0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }さて、このプログラムはコマンドラインで指定した Windows 版の24ビット DIB を
SetDIBitsToDevice( hdc , 0 , 0 , bmpInfo.bmiHeader.biWidth , bmpInfo.bmiHeader.biHeight / 2 , 0 , bmpInfo.bmiHeader.biHeight / 2 , 0 , bmpInfo.bmiHeader.biHeight , bPixelBits , &bmpInfo , DIB_RGB_COLORS );この関数は、コピーする DIB の Y 座標の始点をイメージの中央に指定しています
#include <windows.h> HANDLE hFile; DWORD dwBytes; BITMAPFILEHEADER bmpFileHeader; BITMAPINFO bmpInfo; LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) { HDC hdc; PAINTSTRUCT ps; int iScan = 1; static BYTE * bPixelBits; const int WIDTH = 4 * ((bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biBitCount + 31)/ 32); switch (msg) { case WM_DESTROY: CloseHandle(hFile); free(bPixelBits); PostQuitMessage(0); return 0; case WM_CREATE: bPixelBits = (BYTE *) malloc (WIDTH); return 0; case WM_PAINT: hdc = BeginPaint(hWnd , &ps); for (; iScan <= bmpInfo.bmiHeader.biHeight ; iScan++) { ReadFile(hFile , bPixelBits , WIDTH , &dwBytes , NULL); SetDIBitsToDevice( hdc , 0 , 0 , bmpInfo.bmiHeader.biWidth , bmpInfo.bmiHeader.biHeight , 0 , 0 , iScan , 1 , bPixelBits , &bmpInfo , DIB_RGB_COLORS ); } SetFilePointer( hFile , sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) , NULL , FILE_BEGIN ); EndPaint(hWnd , &ps); return 0; } return DefWindowProc(hWnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS winc; hFile = CreateFile(lpCmdLine , GENERIC_READ , 0 , NULL , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , NULL); if (hFile == INVALID_HANDLE_VALUE) return 1; ReadFile(hFile , &bmpFileHeader , sizeof (BITMAPFILEHEADER) , &dwBytes , NULL); if (bmpFileHeader.bfType != 0x4D42) { MessageBox(NULL , TEXT("This is not a bitmap file") , NULL , MB_OK); return 1; } ReadFile(hFile , &bmpInfo , sizeof (BITMAPINFOHEADER) , &dwBytes , NULL); winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return 1; hWnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , bmpInfo.bmiHeader.biWidth , bmpInfo.bmiHeader.biHeight + GetSystemMetrics(SM_CYCAPTION) , NULL , NULL , hInstance , NULL ); if (hWnd == NULL) return 1; while (GetMessage(&msg , NULL , 0 , 0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }このプログラムは、先ほどのプログラム同様に
int SetDIBitsToDevice( HDC hdc , int XDest , int YDest, DWORD dwWidth , DWORD dwHeight , int XSrc , int YSrc , UINT uStartScan , UINT cScanLines , CONST VOID *lpvBits , CONST BITMAPINFO *lpbmi , UINT fuColorUse );DIB を指定したデバイスコンテキストにコピーします
定数 | 解説 |
---|---|
DIB_PAL_COLORS | カラーテーブルに論理パレットに対する 16 ビットインデックスが格納されている |
DIB_RGB_COLORS | カラーテーブルに RGB 値が格納されている |