Announcement

Collapse
No announcement yet.

SelectObject..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SelectObject..

    Just to make sure - is the following ok? (in WM_PAINT)
    Code:
      LOCAL ps AS PaintStruct, hBrush AS LONG, hPen AS LONG
     
      CALL BeginPaint(hWnd, ps)
         hBrush = CreateSolidBrush(colorBrush)      'hBrush is new brush
         hPen   = CreatePen(style, width, colorPen) 'same with pen
         hBrush = SelectObject(ps.hDC, hBrush)      'select new brush - original brush is returned to hBrush
         hPen   = SelectObject(ps.hDC, hPen)
     
         'Draw something
     
         hBrush = SelectObject(ps.hDC, hBrush)      'select original back - new brush is returned to hBrush
         hPen   = SelectObject(ps.hDC, hPen)        'same with pen
         DeleteObject hBrush                        'now we can delete the new objects
         DeleteObject hPen
      EndPaint hWnd, ps
    Reason for asking: many samples uses return value of BeginPaint in
    separate LONG variable, hDC = BeginPaint, but ps.hDC is the same. Must
    be possible to use that one directly and save 4 bytes, in other words?

    Also, many samples uses separate variables to hold old pens and brushes,
    but SelectObject returns previously selected, so that should not be
    needed? More bytes to save there. Above sample shows no leaks and works
    fine for me, but since its GDI mystery stuff, I feel I have to ask..


    ------------------

  • #2
    That is completely acceptable. The convention is to use the return value of BeginPaint simply so the value can be tested to ensure it is non-zero (in keeping with API functions returning error values), but you could test the ps.hDC parameter to the same end. ALmost no-one ever does test that the hDC is non-zero, since failure is rare (except say, in the extreme condition when there are no GDI resources available).

    On a side topic, I usually combine the DeleteObject and SelectObject calls, just to 'compact' the code slightly:
    Code:
    CALL DeleteObject(SelectObject(ps.hDC, hOldGDIObject))
    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Ah yes - thank you, Lance. Sometimes one needs hBrushOld variable,
      like for FillRect and such, but in many cases one can do without.
      Just wanted to make sure I was on the right track.

      BTW, for those of you who use memDC's to draw in - just found the
      ScrollDc call to be an excellent way to scroll the screen. Faster
      than BitBlt and combined with only redrawing the scrolled part,
      things like lists and editors can be made to "fly", even with
      complicated drawing operations..

      Funny thing - searching through these forums, nothing on ScrollDc
      and still it is such an excellent call for scroolling operations.
      Soon be one good example of it, when my new Virtual Gridlist is done..


      ------------------

      Comment


      • #4
        Borje --
        Why ScrollWindow (Ex) do not satisfy you ?

        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          Since one still has to draw new area, I have found it easier to do all in
          memDC via ScrollDC, draw in new area and then bitblt all to screen. In new
          Virtual Gridlist, I even use two memDC's, one for list and one for selection,
          to make everything perfectly smooth. Still scrolls just as fast as standard
          listbox, but with many more possibilities and absolutely no flicker.
          (standard listbox scrolling looks terrible, IMO, plus when using arrow keys,
          flickering selection sometimes jump out of sight - maybe amateur wrote it..)

          ScrollWindow is probably just as good, but I prefer other way.
          ------------------


          [This message has been edited by Borje Hagsten (edited April 28, 2001).]

          Comment


          • #6
            Borje,
            ...ScrollDc call to be an excellent way to scroll the screen. Faster than BitBlt...
            This seems to be a paradox. The pixel mover in Windows is the mighty BitBlt.
            ScrollWindowEx uses it and since ScrollDC is similar to ScrollWindowEx I suspect it
            also uses BitBlt internally.
            I never did like ScrollWindowEx or ScrollDC; too many parameters for the simple
            environment of a code editor.

            ------------------
            Dominic Mitchell

            [This message has been edited by Dominic Mitchell (edited April 28, 2001).]
            Dominic Mitchell
            Phoenix Visual Designer
            http://www.phnxthunder.com

            Comment


            • #7
              scrollwindowex is enough simple - see, for example, http://www.powerbasic.com/support/pb...ad.php?t=22901 http://www.powerbasic.com/support/pb...ad.php?t=22912

              there is even built-in smooth effect for 98/2000 (sw_smoothscroll).
              but better do not use this flag and stimulate scroll by timer.

              ------------------
              e-mail: [email protected]

              Comment

              Working...
              X