No announcement yet.

Pens and brushes..

  • Filter
  • Time
  • Show
Clear All
new posts

  • Pens and brushes..

    While playing around with the progressbar I recently made, I noticed
    a drop in GDI resources for each created control. No leak, just a drop
    while the control was loaded. Tracked it down to the fact I was storing
    an array of pens during the control's life-time. Stupid thing to do, so
    I will post a change in the code soon, with some other improvements
    added to the control.

    Anyway, this lead me to a small test. Seems like pens and brushes are
    a big factor, when it comes to GDI resources. In case your program
    stores many of them, do check and see if you can do it other way, like
    create and delete them "on the fly".

    (I noticed EZ_GUI made software stores a bunch of brushes - may be
    worth looking into..)

    Win NT and 2000 may handle this better, but the following little
    test steals almost 15% of my GDI resources in Windows 98, and of
    course returns them when the array of pens are deleted:
      REDIM hPen(255) AS LONG
      FOR I = 0 TO 255
         hPen(I) = CreatePen(%PS_SOLID, 1, RGB(I, I, I))
         'hPen(I) = CreateSolidBrush(RGB(I, I, I)) '<- even worse on resources..
      MSGBOX "255 pens created, check your GDI resources!"
      FOR I = 0 TO 255
         DeleteObject hPen(I)
      MSGBOX "255 pens deleted, check your GDI resources again!"


  • #2
    255, way to much, you should see a drop on create is my guess.

    Since you are using a memdc i recommend destroying pen by pen.
    It's not slow.
    I do it in all my functions, create brush, use it, destroy it..
    And pens of course.

    GDI is a difficult thing.



    • #3

      also had insufficient GDI Resource problem with my controls.
      Maybe may Font/Brush cache can help. Pls look at the source code




      • #4

        In some instances it is impossible to keep the GDI resources down,
        but the key is to try to conserve them as much as possible.

        Loading a number of Bitmaps and Icons can use a lot of resources as well.

        While, EZGUI 1.0 is quite effecient with resources (it only creates
        32 default brushes at startup and uses System Fonts for default fonts),
        EZGUI 2.0 does use a lot more Resources (it supports 3D brushes for

        If a large number of controls are used in an EZGUI app it is actually
        more efficient since it uses Brushes and Fonts from a pool rather than
        create a custom Font or Brush for each control. You can also Free
        any GDI resources in an EZGUI app, if needed.

        EZGUI doesn't create any Pens. It only creates Fonts and Brushes.

        I will take a look at making EZGUI 2.0 more GDI Resource Efficient
        as you suggested.

        Chris Boss
        Computer Workshop
        Developer of "EZGUI"


        • #5
          I believe you created the handles for standard qbcolors?
          Windows has already created these brushes by default
          See GetSysColorBrush..



          • #6
            thanks for the input, fellows. ralph, interesting idea with memory cache,
            i'll have a closer look at it. also will have a look at the sample code
            semen just posted and see if i can understand his way of getting gradient
            colors. looks more advanced than what i currently use..

            in the meantime - i have updated the progressbar sample with several new
            features and changed code, so pens are created and deleted "on the fly".
            better way, since windows handles these basic things so badly, imho..

            updated test program at:

            updated progessbar control include file at:



            • #7
              Borje --
              All is enough simple.
              "Internals" of memory hDC (exactly memory) are "internals" of bitmap, which is selected in it.
              If to build 32-bit DIB section and to select it into hMemDC, array of pixels looks
              Dim Pixels(pWidth - 1, 0 : pHeight - 1) As Long At Varptr(bm.bmBits) and you can operate with it directly.

              Some notes:
              1) lines are located from bottom to top (index 0 is last, bottom line)
              2) colors - this is COLORREF (reverse order of bytes comparing with RGB).
              3) I used &H020000000 + ... to stimulate selection from system pallete in 256-colors mode
              (it will be clean color; of course, not gradient).

              E-MAIL: [email protected]