工作區中的繪圖

您可以使用 BeginPaintEndPaint 函式來準備並完成工作區中的繪圖。 BeginPaint 會傳回用於在工作區中繪製之顯示裝置內容的控制碼; EndPaint 會結束繪製要求,並釋放裝置內容。

在下列範例中,視窗程式會在工作區中寫入訊息 「Hello, Windows!」 。 為了確保第一次建立視窗時會顯示字串, WinMain 函式會在建立並顯示視窗之後立即呼叫 UpdateWindow 。 這會導致 WM_PAINT 訊息立即傳送至視窗程式。

LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    PAINTSTRUCT ps; 
    HDC hdc; 
 
    switch (message) 
    { 
        case WM_PAINT: 
            hdc = BeginPaint(hwnd, &ps); 
            TextOut(hdc, 0, 0, "Hello, Windows!", 15); 
            EndPaint(hwnd, &ps); 
            return 0L; 

        // Process other messages.   
    } 
} 
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    HWND hwnd; 
 
    hwnd = CreateWindowEx( 
        // parameters  
        ); 
 
    ShowWindow(hwnd, SW_SHOW); 
    UpdateWindow(hwnd); 
 
    return msg.wParam; 
}