Thread: Question About Line Drawing with Messaging

  1. #1
    Unregistered
    Guest

    Question Question About Line Drawing with Messaging

    Hi, I'm new to windows programming, and I'm trying to make it draw lines to wherever the user clicks. Right now I have this, and it works, but I think there must be a better way to send the message to WM_PAINT than passing -1 as the wParam.

    -----------

    switch (message)
    {
    case WM_PAINT:
    BeginPaint(hWnd, &ps);
    hdc = GetDC(hWnd);
    if(wParam == -1)
    LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
    EndPaint(hWnd, &ps);
    break;

    case WM_LBUTTONDOWN:
    PostMessage(hWnd,WM_PAINT,-1,lParam);
    break;

    case WM_DESTROY:
    PostQuitMessage(0);
    break;

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

    -------

    Thanks!

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    first watch the GetXX() and CreateXX() functions as they allocate GDI memory.

    All GetXX() must have a ReleaseXX()
    All CreateXX() must have a DeleteXX()

    Use
    Code:
    hdc=BeginPaint(hwnd,&ps);//or use the one in the ps struct
    if(wParam == -1) 
       LineTo(hdc, LOWORD(lParam), HIWORD(lParam)); 
    EndPaint(hwnd,&ps);
    Does this have a problem drawing the line in the right spot if the window is not full screen?
    Does the line stay there always?

    Where is the call to to set the pen pos before LineTo() (especially on the first drawing of the line)
    MoveToEx(hdc,XCood,YCood,(LPPOINT)NULL);


    To send a paint msg you can use
    InvalidateRect() or UpdateWindow() or RedrawWindow()

    You should create a compatible DC, bitmap ect and draw to it. Then BitBlt() this HDC to your 'hdc' in response to a WM_PAINT.
    Use GetUpdateRect() before your WM_PAINT to find the area that needs redrawing.

    (try a search here as there is lots of code on paint functions posted)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Unregistered
    Guest
    Ok thanks. But that doesn't mean I need DeleteWindowEx() because of my CreateWindowEx() does it?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    No, The DestroyWindow() should cover that.

    Only for the GDI functions inc Create* / Get

    (Font, brush, pen, bitmap and DC.)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a line
    By Homunculus in forum Windows Programming
    Replies: 6
    Last Post: 03-19-2006, 04:32 PM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Drawing a line on the screen
    By Leano in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 09:24 PM