[C] GDI: how to erase material drawn at an entire screen DC

This is a discussion on [C] GDI: how to erase material drawn at an entire screen DC within the Windows Programming forums, part of the Platform Specific Boards category; salutations, we are capturing the device context of the entire screen by means of GetDC(): Code: HDC hdc; hdc = ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49

    [C] GDI: how to erase material drawn at an entire screen DC

    salutations,

    we are capturing the device context of the entire screen by means of GetDC():
    Code:
    HDC hdc;
    hdc = GetDC(NULL);
    and releasing it in the end, of course:
    Code:
    ReleaseDC(NULL, hdc);
    we can write strings to the screen like this:
    Code:
    TextOut(hdc, 0, 0, "teste", 5);
    this is very interesting, because we don't have to specify a window to draw on - it just draws above all windows. so, putting it in a WM_TIMER event and using GetCursorPos, we can have a label follow the cursor while it moves.
    now, here is the problem: this text can only be removed by invalidating the rect in which it was drawn - and there may be more than one HWND below the text, since this is the screen DC.
    so, how to erase material drawn in the screen DC (i. e., without having to know in which HWND(s) it was drawn)? we have seen in the Internet techniques such as using FillRect(), but that won't work, because the screen DC is not necessarily "empty".
    we could also do InvalidateRect() on every open HWND, but it doesn't seem the best way.
    any ideas?
    thank you in advance.
    NOTE: we are learning GDI and Win32 API programming by ourselves, only with the help of the internet and with no book and no "formal" course. and we couldn't find a solution to this specific doubt anywhere else.
    Last edited by pc2-brazil; 01-23-2009 at 03:41 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    You could create an overlay DC and draw to that instead of the screen DC.
    [a DC the same size as the screen and running top of the Z-order]

    Another way to try is to save the image under the text (before you draw the text) and copy this image back when you draw the next text (after the mouse moves again). This may have some issues if the background changes inbetween

    paste old image over previous text,
    save new image
    draw new text


    You will need;
    a mem DC to hold the image (HBITMAP).
    CreateCompatibleDC()
    Create at start up
    Destroy at close
    A bitmap
    CreateCompatibleBitmap()
    Create at start up
    Destroy at close
    Calculate the text area
    DrawText() with DT_CALCRECT

    BitBlt() to copy the image
    "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
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    salutations,

    thank you for the response. we used the second technique you described. it is very interesting, but it doesn't actually work, because the program has a dialog open that shows changing variables.
    we don't know what you mean by "overlay DC". could you give some example of it?
    thank you in advance.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Did you originally try?

    InvalidateRect(NULL, TextRect, TRUE)

    EDIT:
    the InvalidateRect() with NULL HWND should send a WM_PAINT to all windows below the area.
    The TRUE will tell them all to redraw their backgounds.
    Limiting the area to only the text size will mean the prefomance hit is not too great.

    something like
    //find new text area
    //draw new text
    //InvalidateRect() previous text area
    //save new text area to clear next time

    I have done similar (4-5 years ago), but confined to my windows, so I was able to draw the background, position text and finally mouse cursor.

    I used three DCs, with one DC for just the mouse cursor (whole image, mouse/lines and background).

    If I get time (no one's trains fall off the tracks...) I will pull that project out and find out how I did it.

    Meantime....

    Have you looked at setting the background to transparent?

    SetBkMode(hDC,TRANSPARENT)
    or
    TransparentBlt()

    This might require a custom cursor, created on the fly. This can be complicated as you must supply a cursor and a mask (outline) for each new cursor.

    Quote Originally Posted by pc2-brazil View Post
    we used the second technique you described. it is very interesting, but it doesn't actually work, because the program has a dialog open that shows changing variables.
    I did mention it would not work well with a rapidly changing screen....

    Quote Originally Posted by pc2-brazil View Post
    we don't know what you mean by "overlay DC". could you give some example of it?
    An overlay is a copy of the screen DC. but will suffer from the same issues.
    Last edited by novacain; 01-24-2009 at 06:37 PM.
    "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. [C] drawing: drag GDI shapes
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 10-05-2008, 02:06 PM
  2. Screen DC vs. Printer DC
    By Kennedy in forum Windows Programming
    Replies: 2
    Last Post: 10-19-2006, 02:38 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 03:56 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21