2010-05-02 144 views
1

我想在Win32中創建一個簡單的窗口並在其中繪製一個矩形,但由於某種原因,FillRect不適合我。這裏是我的來源: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; 
} 

是否有我失蹤或在我的代碼中做錯了?在此先感謝

回答

3

您的代碼是罰款。問題是你有 rect.left = 0和rect.right = 0,rect.top和rect.bottom也是一樣的。所以你的矩形是0號。

試試這個

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