鍍金池/ 問(wèn)答/C++  Office/ 用bitblt繪制實(shí)心矩形成了點(diǎn)圖

用bitblt繪制實(shí)心矩形成了點(diǎn)圖

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("figure");
    MSG msg;
    HWND hwnd;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        return 0;
    }

    hwnd = CreateWindow(szAppName, TEXT("Figure"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    HDC mdc;
    HBITMAP bmap;
    HBRUSH hbrush;
    int w, h;

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_PAINT:
        hdc = GetDC(hwnd);

        RECT rect;
        GetClientRect(hwnd, &rect);
        w = rect.right - rect.left;
        h = rect.bottom - rect.top;

        mdc = CreateCompatibleDC(hdc);
        bmap = CreateCompatibleBitmap(mdc, w, h);

        SelectObject(mdc, bmap);
        hbrush = CreateSolidBrush(RGB(240, 240, 240));
        FillRect(mdc, &rect, hbrush);

        BitBlt(hdc, 0, 0, w, h, mdc, 0, 0, SRCCOPY);

        DeleteObject(hbrush);
        DeleteObject(bmap);
        ReleaseDC(hwnd, mdc);
        ReleaseDC(hwnd, hdc);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

圖片描述

回答
編輯回答
懶豬

Remarks

The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter. This bitmap can be selected into any memory device context that is compatible with the original device.
Because memory device contexts allow both color and monochrome bitmaps, the format of the bitmap returned by the CreateCompatibleBitmap function differs when the specified device context is a memory device context. However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format and uses the same color palette as the specified device context.
Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context

MSDN 里說(shuō)了CreateCompatibleBitmap創(chuàng)建的是monochrome(單色的)

2018年8月15日 23:56