1. Win32 Resources

Here's a question for you Windows wizards out there. I'm suspect I'm
handling this situation incorrectly. As I understand it, there are three
steps to changing resources (brush, pen, font, etc.).

1. Create a new resource. For example, I can create a new pen to place in a
window with CreatePen. This returns the handle of the new pen.

2. Replace the old resource with the new resource. For example, I replace
the old pen with the new pen with SelectObject. This returns the handle of
the old pen. The old pen still exists.

3. Destroy the old resource, if it's not a stock resource. If the old pen is
not a stock resources, I destroy it with DeleteResource. Now the old pen has
been destroyed. Destroying a stock resource (which everyone expects to
exist) is a Bad Thing.

This is how Win32Lib handles resources. When Win32Lib shuts down, it runs
DestroyResource and DestroyCursor to destroy all the allocated resources.

I suspect I'm doing this incorrectly for the screen. Since the screen is not
private to Win32Lib, it tries to put it back in the state it was before it
was messed with, using SaveDC and RestoreDC. Here's the Win32 routines that
are called when Win32Lib grabs the screen and creates a new pen for it, and
then releases the screen

   hDC = GetDC( Screen ) -- get the handle of the screen
   SaveDC( hDC )  -- save the state
   hNewPen = CreatePen( hDC ... ) -- create a new pen
   hOldPen = SelectObject( hDC, hNewPen ) -- replace the pen
   DestroyObject( hOldPen ) -- destroy the prior pen

   ... draw routines go here ...

   RestoreDC( hDC ) -- restore the screen state
   ReleaseDC( hDC ) -- release the screen handle
   DestroyObject( hNewPen ) -- destroy the new pen

My *suspicion* is that:

1. I should not have destroyed the pen, because RestoreDC relies on that
handle existing.
2. I don't have to call SelectObject to restore the old pen, since that's
what RestoreDC does.

The documentation I have is unclear, only saying that saving the state
copies "data describing selected objects and graphic modes" . Can someone
clarify this for me?

Thanks!

-- David Cuny

new topic     » topic index » view message » categorize

2. Re: Win32 Resources

On Mon, 7 Feb 2000 13:47:09 -0800, Cuny, David at DSS <David.Cuny at DSS.CA.GOV>
wrote:

>Here's a question for you Windows wizards out there. I'm suspect I'm
>handling this situation incorrectly. As I understand it, there are three
>steps to changing resources (brush, pen, font, etc.).


  David:
     I don't know if this helps or not because I have trouble
     understanding everything you are doing in your win32 library.

     According to a book I have.
     Be sure are NOT using GetDC and ReleaseDC functions in place of
     any BeginPaint and EndPaint functions. TheBeginPaint and EndPaint
     function perform special functions in addition to returning a
     handle to DC. For instance BeginPaint sends WM_ERASEBKGND that
     erases the invalid area, gets the update area, and validates
     any previous invalid areas and so on. This can causes funny things
     to happen if the DC functions are intermixed. Maybe this also might
     have something to do with fonts changing ?
  Bernie

new topic     » goto parent     » topic index » view message » categorize

3. Re: Win32 Resources

Bernie Ryan wrote:

> Be sure are NOT using GetDC and ReleaseDC
> functions in place of any BeginPaint and
> EndPaint functions.

This shouldn't be a problem. Win32Lib keeps a list called 'paintDC' that
tracks if an window already has a DC open, instead of using GetDC.

> Maybe this also might have something to do
> with fonts changing?

I'm guessing that Win32Lib is, in fact, zapping font handles. So Windows
then falls back on using a stock font, which look pretty ugly. I *think*
this is what's happening, but I could be wrong.

Thanks!

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu