News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

GDI+ HDc's

Started by K_F, February 09, 2016, 07:54:47 PM

Previous topic - Next topic

K_F

I think I might be having a moment here  :biggrin:

Is there anyway to hang onto an hDC when drawing in GDI.
I was wondering if one could eliminate all the re-creation and destroying of DC's Handles and Objects, every time one has to update the drawing areas.
Seems like such a waste of code space and time.
:dazzled:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

hutch--

Plonk them into GLOBAL values, create them at the start and deallocate them on exit.

dedndave

i suppose there is a trade-off here
you may reduce the code and even speed it up a little
but, you are also using system resources when you hang on to a DC

don't get me wrong - i do this myself sometimes
but, it's good to understand the downside

there are a limited number of DC's that may be created
granted, it's a fairly large number
also - creating a DC consumes memory

K_F

Maybe I didn't explain myself properly  ;)

You BeginPaint with a window handle and it returns a device context (also in the PaintStruct), which one uses to paint.
You EndPaint with the window handle and the PaintStuct, which contains the device context.

If I understand it correctly,... only after EndPaint does all the painting happen...
From what i understand is that you lose the Device Context here.. forcing you to repeat the whole context allocation process.

Question.. is the Device Context handle really lost here or can one reuse it (keep it in a global variable)?
Reuse it with the same window handle.. that is!
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

dedndave

a lot of "what if's" i don't know the answers to - lol

but, i can tell you 2 things.....

1) BeginPaint/EndPaint do more than assign a DC
they also manage the update region, and probably do other things behind the scene

2) if you want to use a DC that way, use GetDC (or GetWindowDC if you want the entire window)
BeginPaint and EndPaint should only be used in response to a WM_PAINT message
i would imagine the DC handle is only valid for that time

the DC i was refering to above was a memory DC, not a physical device DC

K_F

Quote from: dedndave on February 09, 2016, 11:00:37 PM
2) if you want to use a DC that way, use GetDC (or GetWindowDC if you want the entire window)
BeginPaint and EndPaint should only be used in response to a WM_PAINT message
i would imagine the DC handle is only valid for that time

Thanks... this what I think it is :t
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

MichaelW

Well Microsoft, here's another nice mess you've gotten us into.

K_F

Nice... thank you sire  :t
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'