ソフトウェア開発 Win32プログラミング

市松模様のDIBを作成する

戻る


市松模様のDIBを作成するには次のような関数を使うとよいだろう。

#include <windows.h>

HBITMAP Create24BppCheckerDIB(INT cx, INT cy)
{
    HBITMAP hbm;
    BITMAP bm;
    BITMAPINFO bi;
    LPVOID pvBits;
    LPBYTE pbBits;
 
    ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
    bi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth        = cx;
    bi.bmiHeader.biHeight       = cy;
    bi.bmiHeader.biPlanes       = 1;
    bi.bmiHeader.biBitCount     = 24;
    hbm = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
    if (hbm == NULL)
        return NULL;
 
    GetObject(hbm, sizeof(BITMAP), &bm);
 
    pbBits = (LPBYTE)pvBits;
    for(INT y = 0; y < cy; y++)
    {
        for(INT x = 0; x < cx; x++)
        {
            if (((x >> 3) & 1) ^ (((bm.bmHeight - y - 1) >> 3) & 1))
                pbBits[x * 3 + 0 + y * bm.bmWidthBytes] =
                pbBits[x * 3 + 1 + y * bm.bmWidthBytes] =
                pbBits[x * 3 + 2 + y * bm.bmWidthBytes] = 0x88;
            else
                pbBits[x * 3 + 0 + y * bm.bmWidthBytes] =
                pbBits[x * 3 + 1 + y * bm.bmWidthBytes] =
                pbBits[x * 3 + 2 + y * bm.bmWidthBytes] = 0xFF;
        }
    }
    return hbm;
}
 
HBITMAP Create32BppCheckerDIB(INT cx, INT cy)
{
    HBITMAP hbm;
    BITMAP bm;
    BITMAPINFO bi;
    LPVOID pvBits;
    LPBYTE pbBits;
 
    ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
    bi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth        = cx;
    bi.bmiHeader.biHeight       = cy;
    bi.bmiHeader.biPlanes       = 1;
    bi.bmiHeader.biBitCount     = 32;
    hbm = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
    if (hbm == NULL)
        return NULL;
 
    GetObject(hbm, sizeof(BITMAP), &bm);
 
    pbBits = (LPBYTE)pvBits;
    for(INT y = 0; y < cy; y++)
    {
        for(INT x = 0; x < cx; x++)
        {
            if (((x >> 3) & 1) ^ (((bm.bmHeight - y - 1) >> 3) & 1))
                pbBits[x * 4 + 0 + y * bm.bmWidthBytes] =
                pbBits[x * 4 + 1 + y * bm.bmWidthBytes] =
                pbBits[x * 4 + 2 + y * bm.bmWidthBytes] = 0x88;
            else
                pbBits[x * 4 + 0 + y * bm.bmWidthBytes] =
                pbBits[x * 4 + 1 + y * bm.bmWidthBytes] =
                pbBits[x * 4 + 2 + y * bm.bmWidthBytes] = 0xFF;
            pbBits[x * 4 + 3 + y * bm.bmWidthBytes] = 0xFF;
        }
    }
    return hbm;
}

ソース: checker.zip


戻る

©片山博文MZ
katayama.hirofumi.mz@gmail.com

inserted by FC2 system