I have a question for those expert GDI people.

Im 99% done my custom control example, but i would like to know what (in your opinion) is better:

    BACKGROUND
    [*]My control is a child window design.
    [*]I have 5 graphical states to display on the custom control.
    [*]The control itself is typically no bigger than say 30x30 (but user defined).
    [*]The display states are being back buffered, and bitblt copied to the DC for the WM_PAINT.

    WHICH IS BETTER
    a) Using parent DC (CS_PARENTDC) when defining the class (what i think is better, but would like to hear pros/cons)
    b) Using a private DC (CS_OWNDC)

    WHICH IS BETTER
    a) Creating 5 private DC's and predrawing the 5 static display images on bitmaps for each. Then simply bltblt the proper DC when needed. Means i keep the DC's/Bitmaps created for the life of the control.
    b) Create 1 private DC, and draw and copy images needed in real-time, as the drawing state demands. Thus using it only as a generic back buffer. (Overhead is drawing 3D boarders and copying icon images)

    Im currently using 5 private DC's that are predrawn and left static. However, im wondering if the savings in GDI draw API times is worth the overhead of hanging on to 5 small DC's for each control instance.

    Remember, this is a custom control, and there could be 10 or more instances. Meaning 50 or more private DC's in total!

    Hope you can shed some good reasoning on this, thanx!
    :alright:
    NaN
Posted on 2002-04-21 22:56:56 by NaN
Nevermind the DC question. I realized Im making a very big and dumb error here :rolleyes: .

However, i would like to know if this is ok (in the sense of not making memory leaks).
     ; Create Needed DC Stuff

mov DC, $invoke( GetDC, _hWnd ) ; Build Back Buffer Bitmaps
mov DCUP, $invoke( CreateCompatibleDC, DC ) ; Make the new DC
mov hBitUp, $invoke( CreateCompatibleBitmap, DC, WinData.lx, WinData.ly )
invoke ReleaseDC, _hWnd, DC ; Release original DC (done with this)
mov Brush, $invoke( CreateSolidBrush, $invoke( GetSysColor, COLOR_BTNFACE ) )
invoke SelectObject, DCUP, hBitUp
invoke SelectObject, DCUP, Brush
[b] invoke DeleteObject, eax[/b]

Basically, can i axe the Brush that comes with the private DC right away, and later only axe my 'Brush' followed by 'hBitUp' and then 'UPDC' ???

The API ref, does say that a private DC doesnt have a bitmap within it, so im not saving the return when the bitmap is selected into it, but it doesnt say anything about pens/brushes, so im assuming there is "something" there and i dont care for it. This is why im killing it right off the bat. Is windows going to get all screwed up from this?


Thanx!
:alright:
NaN
Posted on 2002-04-21 23:55:01 by NaN
Common! :)

Someone's gotta know if its alright to destroy returns from a DC before you destroy the DC itslef.....

Im concerned about memory leaks here and i dont know if i should 'back up' whatever it had when i destroy the DC.

(Forget the top post ~ i figured it out on my own :grin: )

Thanx!
:alright:
NaN
Posted on 2002-04-22 16:40:18 by NaN
Nan,
in all the GDI samples/tuts i have seen, they *never* mentioned destroying the default brushes immediately after inserting your own, they always save the original brush then restore it just before destroying the DC. They do say that if you do not restore the original brushes etc then you will get memory leaks, so there are obviously real objects on the ends of those handles. OTTOMH, Win9x and WinNT handle this issue differently; on one system the DC is returned to you with a copy of the handle to a *global* pen/brush etc, while in the other (NT i think) every DC gets new objects. I do know that if you do a DeleteObject on a bull**** handle then nothing will happen, you can safely ignore the error returned. So, my guess is that if you try to destroy the handle to one of those global objects, Win ignores it because there are still outstanding handles in other processes.

I really would recommend you do a trawl thru the microsoft.public.win32.programmer.gdi newsgroup, there is a lot of in-depth hardcore knowledge in there, and many noted people (like Feng Yuan) answer questions there. Worth a look.
Posted on 2002-04-22 16:57:24 by sluggy
i do it like this
GetDC
CreateCompatibel DC
....

after that i delete all Objects
and destroy the bitmaps and dc
at least i release the window dc
Posted on 2002-04-22 16:58:37 by adapix
Geese, take an afternoon nap and the guy goes ballistic. ;-)



As far as I understand them, the rules for handling GDI stuff are identical to kindergarten play etiquette.
If you borrow a toy, put it back when done.
If you make a mess, clean it up before you leave.


Applying these concepts to GDI means I always save the default objects it has when created, restore them when I'm done, and then delete/release the DC.

For a control such as yours, I wouldn't think it be too hoggish to create two DCs, one for the back buffer, one for the 'blit from' image, and hang onto them. In fact, I'd keep the bitmap on the back buffer as I painted it for simple re-paint ops. I'd also hang onto any frequently used bitmaps, and select them as needed.

If defining a bitmap as infrequent isn't obvious, define it as frequent.
Posted on 2002-04-22 19:04:42 by Ernie