Thread: brush handle in FillRect function - why does this work?

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks, I appreciate all the info.

    I can find almost all that you mentioned at msdn. I can't find anything about "member", though.
    Where can I find more info on that?

    This question came up because some examples for using the GDI from msdn and other tutorials,
    use the DC for the client area and do the drawing directly onto it.

    Should every graphics operation happen only in a memory DC then?
    Are there any cases where you can draw using the DC for the client area?

  2. #17
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Member variables are used to provide encapsulation in C++ and C#.

    Classes have 'member' variables; variables that are local to the class and are accessable in any of the classes methods / functions. [not available in C as C has no classes]
    Member variables have 'global' scope within the class but are not accessable outside an instance of the class (a class variable) unless marked as 'public'.

    [google 'member variable' or 'encapsulation' for more detail]

    Each instance of the class has its own copy of the variable (unless the member is declared as 'static').

    To answer your next question. 'Why use this feature?'
    google 'inheritance in c++'

    >>Are there any cases where you can draw using the DC for the client area?

    Yes and no.
    It all depends on the requirements of the application. If you read my posts carefully you will notice I mention your 'current paint'. This is because simple drawing (as in your example code), or drawing that does not need to be presistent, can be done in the WM_PAINT.

    >>This question came up because some examples for using the GDI from msdn and other tutorials,
    use the DC for the client area and do the drawing directly onto it.

    These are simple examples used to show basic concepts, not actual implementations used in more complex applications.
    "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. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Doing all the drawing in a memory DC does make sense. I will be converting the program over
    to using this method at some point.

    I am still not clear on the client area HDC becoming invalid. I see how a rectangle in the client area
    becomes invalidated. But why is the handle to the client area no longer valid?

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I assume you are refering to my comment;

    >>You only invalidate the minimun rect required (not the entire client area).<<

    By 'invalidate' I mean 'mark the rectangle for painting' (ie set it to be redrawn in the paint).

    You make an area 'invalid' by calling InvalidateRect(). This generates a WM_PAINT msg for the given window (HWND) and for the given area (RECT).

    You can use the whole client rect or, better, the minimum rect that includes all the changes.

    When you call BeginPaint() this invalidated RECT is copied into the PAINTSTRUCT rcPaint RECT struct.

    You then use the rcPaint RECT in your drawing (ie in the BitBlt())
    "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

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    No, I think I understand that.

    I was referring to:

    "Your global HDC will be invalid in many circumstances (ie if the window changes shape / size)."

    The HDC in this case was the handle to the client area of my window.

    In other words, does the original handle returned by:

    ShowWindow(hWnd, nCmdShow);
    hDc = GetDC(hWnd);

    become invalid when the first WM_PAINT message is received? Or else when?

    I am wondering at what point I can no longer use the handle returned by GetDC(hWnd),
    and what to do if I need a handle to that window outside of BeginPaint ... EndPaint.

    For example, won't I need my global hDc to release the window device context as in:

    ReleaseDC(hWnd, hDc); ?
    Last edited by megafiddle; 09-06-2011 at 11:52 AM.

  6. #21
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    A HDC is a handle to a Device Context.

    A handle is a void pointer.

    A pointer is only valid if the memory pointed to has not been freed, re-alloced etc.

    Why would you need to GetDC() inside a WM_PAINT?

    I think you need to read exactly what BeginPaint() and GetDC() do.
    "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

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I don't need that inside a WM_PANT. I am using the one returned by BeginPaint
    as you recommended.

    I am talking about outside a WM_PAINT. For example when terminating the program.

    If my hDc can become invalid, how will ReleaseDC(hWnd, hDc); ever work?

    (just added this)

    If you are saying that I shouln't have to do this:

    hDc = GetDC(hWnd);

    And therefore wouldn't have to do this either:

    ReleaseDC(hWnd, hDc);

    then that makes sense.


    And would that mean that sooner or later, this will fail:

    hDc = GetDC(hWnd);
    ...
    ...
    ...
    ReleaseDC(hWnd, hDc);

    ?
    Last edited by megafiddle; 09-07-2011 at 01:03 PM.

  8. #23
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I have explained this in many threads over the last decade...which is why I told you to search my previous posts.


    When you get a WM_CREATE or WM_INITDLG msg you use GetDC() to get a pointer to the main screen's DC.
    You create compatible DC and bitmap, saving the 1x1 monochrome bitmap originally in the created DC but 'pushed' out by you selecting in the created bitmap.
    You ReleaseDC()
    This means your GetDC() is guaranteed to retain scope, as it's scope is very limited (and not global for the life of the app as you are trying to do).

    You use the compatible DC anywhere you need to do drawing and then generate a paint msg.

    You BitBlt() the compatible DC to the PAINTSTRUCT DC in a WM_PAINT

    You SelectObject() the orig bitmap into the created DC, then delete the bitmap and DC, when you get a WM_CLOSE.

    Search for code on what to do when you get a WM_SIZE.
    "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

  9. #24
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks again.

    I do intend to incorporate your suggestions and recommendations.

    I have been trying to search, but the searcher is turning up nothing.

    Anyway I have attached my source code in the C programming section, "large number variables...",
    although i haven't added any of the things you recommended yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to loop FillRect
    By Anddos in forum Windows Programming
    Replies: 2
    Last Post: 03-27-2008, 11:49 AM
  2. Brush color
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 04-14-2007, 06:51 AM
  3. BRUSH PEN Color
    By arjunajay in forum Windows Programming
    Replies: 2
    Last Post: 08-27-2006, 09:06 PM
  4. Ask about FillRect()
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 07-26-2002, 03:36 AM