Win32: Does a window have the same HDC throughout its entire life cycle?

Can DC be used outside the paint cycle? Is my window constant time constant?

I am trying to figure out how long my Device Context (DC) control has been valid.

I know I can call:

GetDC(hWnd);

to get the device context of my control window, but is it allowed?

When Windows sends me a WM_PAINT message, I have to call BeginPaint / EndPaint to correctly confirm that I drew it, and internally clear the invalid area:

BeginPaint(hWnd, {out}paintStruct);
try
   //Do my painting
finally
   EndPaint(hWnd, paintStruct);
end;

But calling BeginPaint also returns me DC inside the PAINTSTRUCT structure. This is the DC that I have to draw.

- , , DC, BeginPaint(), DC, GetDC().

, , Desktop Composition, DC, BeginPaint?

, , DC :

  • dc = GetDC (hWnd);

  • BeginPaint (& PAINTSTRUCT);

, , , Borland Delphi, .

WM_PAINT Delphi , wParam DC . MSDN , wParam WM_PAINT .

GDI + Graphics HDC, GDI +, .

WM_PAINT GDI + . nieve :

WM_PAINT:
{
   PAINTSTRUCT ps;
   BeginPaint(m_hwnd, ps);
   Graphics g = new Graphics(ps.hdc);
   g.DrawImage(m_someBitmap, 0, 0);
   g.Destroy();
   EndPaint(h_hwnd, ps);
}

GDI , CachedBitmap. :

WM_PAINT:
{
   PAINTSTRUCT ps;
   BeginPaint(m_hwnd, ps);

   Graphics g = new Graphics(ps.hdc);
   CachedBitmap bm = new CachedBitmap(m_someBitmap, g);
   g.DrawCachedBitmap(m_bm, 0, 0);
   bm.Destroy();
   g.Destroy();
   EndPaint(h_hwnd, ps);
}

CachedBitmap , :

m_graphics = new Graphics(GetDC(m_hwnd));
m_cachedBitmap = new CachedBitmap(b_someBitmap, m_graphcis);

:

WM_PAINT:
{
   PAINTSTRUCT ps;
   BeginPaint(m_hwnd, ps);
   m_graphics.DrawCachedBitmap(m_cachedBitmap, 0, 0);
   EndPaint(h_hwnd, ps);
}        

, , DC, , DC , . , :

  • /

MSDN, , DC , .

: , . , .

+5
3

, DC , GetDC BeginPaint. , DC. ( , , , , .)

. , , DC.

Windows, , , WM_DISPLAYCHANGE WM_PALETTECHANGED. . , .

, . - , DC, . , WM_THEMECHANGED.

+5

, , ( ) , , - CS_OWNDC .

, .

Edit

MSDN:

- , . , .

, . , .

, , CS_OWNDC . - , . , .

Windows 95/98/Me: CS_OWNDC , , 64K GDI .

, CS_OWNDC:

#include <windows.h>

static TCHAR ClassName[] = TEXT("BitmapWindow");
static TCHAR WindowTitle[] = TEXT("Bitmap Window");

HDC m_hDC;
HWND m_hWnd;

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

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

            if (ps.hdc == m_hDC)
                MessageBox(NULL, L"ps.hdc == m_hDC", WindowTitle, MB_OK);
            else
                MessageBox(NULL, L"ps.hdc != m_hDC", WindowTitle, MB_OK);

            if (ps.hdc == GetDC(hWnd))
                MessageBox(NULL, L"ps.hdc == GetDC(hWnd)", WindowTitle, MB_OK);
            else
                MessageBox(NULL, L"ps.hdc != GetDC(hWnd)", WindowTitle, MB_OK);

            RECT r;
            SetRect(&r, 10, 10, 50, 50);
            FillRect(m_hDC, &r, (HBRUSH) GetStockObject( BLACK_BRUSH ));

            EndPaint(hWnd, &ps);
            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{   
    WNDCLASSEX wcex;

    wcex.cbClsExtra = 0;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.cbWndExtra = 0;
    wcex.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
    wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
    wcex.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wcex.hIconSm = NULL;
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = WndProc;
    wcex.lpszClassName = ClassName;
    wcex.lpszMenuName = NULL;
    wcex.style = CS_OWNDC;

    if (!RegisterClassEx(&wcex))
        return 0;

    DWORD dwExStyle = 0;
    DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;

    m_hWnd = CreateWindowEx(dwExStyle, ClassName, WindowTitle, dwStyle, 0, 0, 300, 300, NULL, NULL, hInstance, NULL);

    if (!m_hWnd)
        return 0;

    m_hDC = GetDC(m_hWnd);

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

    return msg.wParam;
}

CS_OWNDC CS_CLASSDC, :

. , . . , .

CachedBitmap.

CachedBitmap, Graphics . , Graphics, , DrawCachedBitmap , . .

, CS_OWNDC - , .

Edit

, , DC / CS_OWNDC, , , DC (Window 7 64-bit Ultimate) ( OS... ).

Edit2

GetUpdateRect, , WM_PAINT. .

+5

, . . , . , GetDC - BeginPaint , , dc, . ReleaseDC ( EndPaint), . Windows 3.1 , , GetDC. dc .

, WM_PAINT dc, BeginPaint, , .


, gdiplus. , ... dc, dc - dc, dc.


, GetDC , HDC, . , , , .., DC, DC, GetDC BeginPaint.

HDC, , , , HDC, , . , dc, , hdc blit.

, LEAST WM_DISPLAYCHANGE, DC .

+2

All Articles