2

Usually, to draw a line we draw it in WM_PAINT

LRESULT CALLBACK Display::DisplayWindowProc(HWND hWnd,UINT msg,WPARAM wParamm,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;

    switch(msg)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd,&ps);
        MoveToEx(hdc,0,0,0);
        LineTo(hdc,100,100);
        EndPaint(hWnd,&ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc( hWnd, msg, wParamm, lParam);
}

But , i want to draw line whenever i want, simple example:

int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR cmdLine,
int showCmd
)
{
    //Do Other Things
    Display dislpay;
    display.DrawLine();
    //Do Other Things
}

My Program is Object Oriented and i display things in Display class and i was wondering if i can do a draw line in a function like DrawLine() in Display Class.

1 Answers1

2

You can create an off-screen DC, and select an appropriately sized bitmap, and use that to draw whenever you want. Then on WM_PAINT you blit from the off-screen DC into the windows DC.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @ErfanAhmadi I haven't done that in many years, but it goes something like this: Create a DC, create a bitmap, in the DC select the bitmap, use the DC to draw, and in `WM_PAINT` call e.g. [`BitBlit`](https://msdn.microsoft.com/en-us/library/dd183370%28v=vs.85%29.aspx) to copy from the off-screen DC to the window DC. SEarch [MSDN](https://msdn.microsoft.com/en-us/library/ms123401.aspx) for references. – Some programmer dude Jun 30 '15 at 07:22
  • But in my case i don't use bitmaps. i don't know what it has to do with bitmaps. – Erfan Ahmadi Jun 30 '15 at 07:24
  • @ErfanAhmadi A DC is just a context, it collects all the things needed to draw, pens, colors, and what to draw on, which is where the bitmap comes in. You need a bitmap or you have nothing to draw on. A window have an on-screen bitmap that you can draw on. Think of it like this, drawing a line is simply setting some bits in memory, but you need some memory for the actual bits, and that is what a bitmap provides. You select a bitmap (or pen or drawing brush) using [`SelectObject`](https://msdn.microsoft.com/en-us/library/dd162957%28v=vs.85%29.aspx). Please follow the links and read. – Some programmer dude Jun 30 '15 at 07:29