FillRect不会绘制任何东西

时间:2010-05-02 17:56:23

标签: winapi

我正在尝试在Win32中创建一个简单的窗口并在其中绘制一个矩形,但由于某种原因,FillRect对我不起作用。这是我的来源:

#include <windows.h>
#include "resource.h"

RECT rect;

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static PAINTSTRUCT ps;
    static HDC hDC;

    switch(msg)
    {
        case WM_PAINT:
            hDC = BeginPaint(hWnd, &ps);

            FillRect(hDC, &rect, (HBRUSH)(COLOR_WINDOW+1));

            EndPaint(hWnd, &ps);
        break;

        case WM_CLOSE:
            DestroyWindow(hWnd);
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
        break;

        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hWnd;
    MSG msg;
    rect.left = 0;
    rect.right = 0;
    rect.top = 100;
    rect.bottom = 100;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_ACTIVEBORDER+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MAIN_MENU);
    wc.lpszClassName = "Main";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL, "Main", "Main", WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 240, 360, NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

我的代码中是否有遗漏或错误的内容?提前致谢

1 个答案:

答案 0 :(得分:3)

你的代码很好。问题是你有 rect.left = 0和rect.right = 0,rect.top和rect.bottom也是一样的。所以你的矩形是0大小。

试试这个

rect.left = 0; 
rect.right = 100; 
rect.top = 0; 
rect.bottom = 100;