borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Not a new topic probably)Next Topic (GRAM-SCHMODT ORTHOGONALIZATION) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Reduce flickering
Poster Message
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907

icon Reduce flickering

Hello All. Graphic question:

got an Image control img1, an ImageList control iml1 with six images stored in, a timer control and a StdPicture variable array.
I load all the iml1 pictures into the StdPicture array items, then in the tmr1_Timer event I switch one or another in the Picture property of img1.

But every now and then a nasty flicker apeears, no matter how fast is the Interval property of tmr1. I tried with 50, 100, 200, 500 milliseconds, and even 1000, 2000 and 5000. Always appears sometimes a small img1 flash before changing image.

How can I reduce, or stop it? The form has Autoredraw=True, but seems to have no influence on it. I need an Image control because of its Stretch property (by the way, is there anybody who was anyhow able to make the StdPicture Render method working to resize an image? ), but also with a PictureBox the behaviour is the same. If I Paint directly over the form, the whole form flashes.

Every help is largely appreciated, and thanked in advance.

____________________________
Real Programmer can count up to 1024 on his fingers

02-02-2004 at 03:07 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Reduce flickering

How about having an image control array. Load the images into the image controls and then display them one at a time.??

Steve  

02-02-2004 at 04:00 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Reduce flickering

I had 5 minutes to have a play.
Have go at this put one image control on a form with an index of 0. Add an imagelist with a few pictures in it. And then the following code

Private display_counter As Integer

Private Sub Form_Load()

    display_counter = 0
    
    Image1(0).Visible = False
    
    For i = 1 To ImageList1.ListImages.Count
    
        Load Image1(i)
        Image1(i).Picture = ImageList1.ListImages(i).Picture

    Next i
    
End Sub

Private Sub Timer1_Timer()

    Dim img As Image
    
    For Each img In Image1
        img.Visible = False
    Next
    
    Image1(display_counter).Visible = True
    
    display_counter = display_counter + 1
    
    If display_counter > ImageList1.ListImages.Count Then
        display_counter = 1
    End If

End Sub



Oh, and a timer with an interval of 1 second.

Steve


[Edited by steve_w on 02-02-2004 at 05:08 PM GMT]

02-02-2004 at 05:07 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

How about this, no flickering on my comp. By the way, AutoRedraw property doesnt have any effect on your problem here.

Private Sub Timer1_Timer()
Static x As Integer

    x = x + 1
    If x > ImageList1.ListImages.Count Then x = 1
    Image1.Picture = ImageList1.ListImages(x).Picture
End Sub


____________________________
If you find the answer helpful, please mark this topic as solved.

02-02-2004 at 10:10 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

As for you Render method question, I have been doing a research on it on the Net this evening, and strangely didn't found an example of how it is used. Most of the people that mentioned it in their articles didnt manage to make it work. Basically, there is a much easier method - PaintPicture method that Microsoft suggest to be used instead of Render method. But, since you were also asking questions about Render method on your post about streching picture to form background, I decided to do some of my own research to see if I can help you, and here is some explanantion about StdPicture Render method.

From MSDN:

The Render method syntax has these parts:

Part Description

object Required. Anobject expression that evaluates to an object in the Applies To list.

hdc Required. The handle to the destination object's device context.

xdest Required. The x-coordinate of upper left corner of the drawing region in the destination object. This coordinate is in the scale units of the destination object.

ydest Required. The y-coordinate of upper left corner of the drawing region in the destination object. This coordinate is in the scale units of the destination object.

destwid Required. The width of drawing region in the destination object, expressed in the scale units of the destination object.

desthgt Required. The height of drawing region in the destination object, expressed in the scale units of the destination object.

xsrc Required. The x-coordinate of upper left corner of the drawing region in the source object. This coordinate is in HIMETRIC units.

ysrc Required. The y-coordinate of upper left corner of the drawing region in the source object. This coordinate is in HIMETRIC units.

srcwid Required. The width of drawing region in the source object, expressed in HIMETRIC units.

srchgt Required. The height of drawing region in the source object, expressed in HIMETRIC units.

wbounds Required. The world bounds of ametafile. This argument should be passed a value of Null unless drawing to a metafile, in which case the argument is passed a user-defined type corresponding to a RECTL structure.

Last parameter is not our concern here, since we are not working with metafiles.

Considering destination object (ex PictureBox) and its left, top, width and height parameters, MSDN says it should be expressed in scale units of the destination object. It didnt work in my case, and I have come to the conclusion that it should be expressed in PIXELS. So, first thing you should do is set Pciture's ScaleMode to 3-Pixel.

As for the source object (stdPicture), its left, top, width and height should be expressed in HIMETRIC units. StdPicture's Width and height property are by default expressed in HIMETRIC units, so there is no need for converting. But if you use this code:

pic.Render Picture1.hDC, 0, 0, _
        Picture1.ScaleWidth, Picture1.ScaleHeight, _
        0, 0, pic.Width, pic.Height, 0


you will get picture that is flipped vertically. This happens beause coordinate system that use HIMETRIC values has 0,0 position in the left-bottom point. Positive x is to the right; positive y is up.

So to manage this you need to change the direction that Render methid should read source target. The idea is to "tell" Render method to read the source target in the direction left-top, right-bottom.

Private Sub Command1_Click()
Dim pic As StdPicture

    Set pic = LoadPicture("D:\Goran\My Pictures\T-Shirt.jpg")
    
    pic.Render Picture1.hDC, 0, 0, _
        Picture1.ScaleWidth, Picture1.ScaleHeight, _
        0, pic.Height, pic.Width, -pic.Height, 0
End Sub


In order this example to work you need to have PictureBox's ScaleMode set to 3-Pixel.

I hope that this explanation was clear and useful to you Yronium (and stopped you from   ,   ) and to all the others that were interested in Render method.

    

____________________________
If you find the answer helpful, please mark this topic as solved.

03-02-2004 at 12:36 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Reduce flickering

Private Sub tmr1_Timer()
    ' numlogo is a module level variable, set = 1 on Form_Load event
    imgLogo.Picture = iml1.ListImages.Item(numLogo).Picture
    If numLogo < 6 Then
        numLogo = numLogo + 1
    Else
        numLogo = 1
    End If
End Sub

This is my code, Goran. I get the terrible flickering I described above, even running the compiled executable in a PIII 800Mhz pc.

The alternative I tried out, but without any flickering reduction is as following:
Option Explicit

Dim numLogo As Integer
Dim picLogo(5) as StdPicture

Private Sub Form_Load()
    Dim i As Integer
    ' load the pictures into a variable's array
    For i = 0 To 5
        Set picLogo(i) = iml1.ListImages.Item(i + 1).Picture
    Next i
    numLogo = 1
End Sub

Private Sub tmr1_Timer()
    imgLogo.Picture = picLogo(numLogo - 1)
    If numLogo < 6 Then
        numLogo = numLogo + 1
    Else
        numLogo = 1
    End If
End Sub

Maybe it matter that the image control is very small, so therefore I notice more the flicker effect, but I experienced it even after putting another larger image control. And I tried hard keeping the code as simpler as possible. But I still get a flick every three, four or five timer ticks. (It's not always the same picture, but they all do it at random)
The flick I mean is: my six pictures are .gif files, having a transparent background. The imgLogo control show its backcolor on the backgroud of the picture, but after three or four pictures it quickly flashes a rectangle - its real shape, like it needed to redraw itself every now and then. When running faster, it's a very nasty effect.

Steve, have you got only one single Image1 control? In your code there's something I miss, you put only the Image1(0) item, isn't it?
Anyway, I tried it out and it works, but still got flicks. I also tried to increase anyway the numLogo variable, and then check it with the If condition, as you both did, like
    numLogo = numLogo + 1
    If numLogo > iml1.ListImages.Count Then numLogo = 1

but no changes (....ugh...)
I made all the tests with 1000 milliseconds on timer's Interval property.

____________________________
Real Programmer can count up to 1024 on his fingers

03-02-2004 at 12:37 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Reduce flickering

(About Render method
Goran, you made a very huge work, and your explanation is far clearer than MSDN ones. I will try your example at once and print this topic to study it. I am very thankful to you, and if I was your boss, I will increase your pay for sure  

I hope that I will be able to resize my StdPicture images now. Like actual app: I have my six pics, say 480x96 pixels, but I want to store them in a StdPicture array as, say, 100x30 pixels, and I was never able to do it. I hope it work.

I have read all the MSDN stuff (very few) about StdPicture and Render method, and I read that they suggest to use PaintPicture instead, but still had this problem: how resize them before, in order to paint or just load a smaller image? And I read about the parameter types, but I must confess I'm not deep with graphic measurement units, so it never worked to me. Again, you made a very huge and important study on it. Thank you again so much!!)

____________________________
Real Programmer can count up to 1024 on his fingers

03-02-2004 at 12:53 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

Dont miss my previous post, cause we have made posts at the same time. I have Duron 750Mhz, 256DDR, and no flickering.

I have put a image control on the form, with

Width=3000
Height=2200
Strecth=True

Imagelist control, with 10 pictures size 800x600 (approximate size is 200Kbs), and a Timer control with Interval set to 1000, 500, and 100 respectively, and no flickering occured.

[Edited by Goran on 03-02-2004 at 02:03 AM GMT]

____________________________
If you find the answer helpful, please mark this topic as solved.

03-02-2004 at 12:56 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Reduce flickering

I'm working on a PII 350Mhz 384 MbRAM, and a laptop Celeron 450Mhz 128 Mb Ram, but also tested it today on a machine in my office, with a PIII 800Mhz.

My image control is:
Width = 2295
Height = 385
Stretch = True

Imagelist control with 6 pictures size 360x70 (6,38 kb each)

Maybe could be a glasses lenses dirt problem...

PS. no fear, I won't miss any post of yours

____________________________
Real Programmer can count up to 1024 on his fingers

03-02-2004 at 01:10 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Reduce flickering

Hi Yronium


There is only one image(0) control at design time, but I load copies of it for the number of images in the imagelist control. I was thinking that if the images were loaded into the controls during the formload there would have been less of a delay in the displaying of them. Sorry could not duplicate your flickering though.

Good luck any way.

Steve  

[Edited by steve_w on 03-02-2004 at 10:08 AM GMT]

03-02-2004 at 10:07 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Reduce flickering

Thanks Steve, I only didn't know that the Load Image1(1), Load Image1(2)... instruction would automatically add some new Image1 controls. I learnt something new today...

But unfortunately it doesn't match my question anyway.
As you both didn't experience any flicker, I'm thinkin the problem could be related to GIF format of the pictures. I'll try to change it (I'd need to deal with BitBlt/TransparentBlt because I need a transparent background in my six pictures).
I'll write down again.

____________________________
Real Programmer can count up to 1024 on his fingers

03-02-2004 at 02:08 PM
View Profile Send Email to User Show All Posts | Quote Reply
vbgen
Level: Moderator

Registered: 10-10-2002
Posts: 876
icon Re: Reduce flickering

sweethearts...

to solve this problem, you would have need for subclassing..

please go to http://www.vbaccelerator.com/

you will find what you need there on flickering.

it is a cool thing... but it would be better if you guys try to understand the code first. API's are hard to conquer, but once you do, it'll be easy.

good luck, mates.  

____________________________
Been busy trying to take a second degree <--it's not working out...

03-02-2004 at 05:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

vbgen, what do u mean by subclassing, what idea are you reffering to? (every image is repainted only once)

____________________________
If you find the answer helpful, please mark this topic as solved.

04-02-2004 at 08:17 PM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Reduce flickering

Subclassing in this context refers to intercepting the messages sent to the window or control. Windows communicates to programs, windows and controls via messages. These are Long integers that represent a particular state or action. With VB you normally don't worry about these messages because VB translates these into Events for you. There are times when VB doesn't have that one event that you want and it is then that you have to roll up your sleeves and intercept the message and do what you want.

I'm yet to see the code that vbgen has directed us to but I suppose it hooks (intercepts messages for) the image control and does something to prevent the flicker.

For a quick example on subclassing you can go here http://www.andreavb.com/forum/news/viewtopic.php?TopicID=1309 and see how to subclass a form to get it's menu related messages. (It has reminded me of something I have to do and put up on the downloads section)

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

04-02-2004 at 09:43 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

fabulous, I didnt mean what subclassing means, I am well intruduced in it ( in my posts there are some subclassing codes) , my question was what idea did he had in mind when he reffered to subclassing.

quote:
I'm yet to see the code that vbgen has directed us to but I suppose it hooks (intercepts messages for) the image control and does something to prevent the flicker.


this something to prevent flicker is the thing I was reffering to. Flickering occurs when u have multiple painting of the control, so to prevent it you can lock the window from updating while while invalidating and then redraw it.
(the example for this would be subclassing the text box to set transparent background - flickering would occur while scrolling multiline textbox)

This is why I asked vbgen what idea did he had in mind when he mentioned subclassing, because every image that is loaded is drawn only once (unles there is a overcast over the image), and timing betwen loading images is long enough - 1 sec (I have even tested this example with 0,1 sec and no flickering occured), so I was wondering what is he trying to achieve with subclassing?

[Edited by Goran on 05-02-2004 at 12:30 AM GMT]

____________________________
If you find the answer helpful, please mark this topic as solved.
04-02-2004 at 11:22 PM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Reduce flickering

Sorry about that. (feeling sheepish). I should have known better. I will check out what this code does and build a similar project to see why these things are happening. So sorry.

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

05-02-2004 at 02:48 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

No hard feelings, fabulous, I was not offended by any of your words.    

____________________________
If you find the answer helpful, please mark this topic as solved.

05-02-2004 at 02:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
vbgen
Level: Moderator

Registered: 10-10-2002
Posts: 876
icon Re: Reduce flickering

hello, boyz... no fighting.. that's good...

*sighs, starting to think of the other boards where i play peacemaker*

anyway, there's a redrawing function going on inside vb.. and it handles it poorly...

so basically.. wait... can't explain it.. haven't installed vb here in this laptop yet... sorry! *bonks herself on the head*

well, what i can say is that it intercepts the msgs for drawing the images, then the code will do it properly for you..

noticeably... in C programs... not much flickering occurs, because you program at the base level... you do the code yourself..

i'll try to get vb here and explain, unless fabio gets to do it already.  

____________________________
Been busy trying to take a second degree <--it's not working out...

05-02-2004 at 06:15 PM
View Profile Send Email to User Show All Posts | Quote Reply
vbgen
Level: Moderator

Registered: 10-10-2002
Posts: 876
icon Re: Reduce flickering

here it is...

http://www.vbaccelerator.com/home/VB/Code/Libraries/Graphics_and_GDI/Flicker_Free_API_Drawing/article.asp

and the code of the class module... *sorry, don't have vb yet in this pc*

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cDrawSample"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Type POINTAPI
   x As Long
   y As Long
End Type
Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal xOffset As Long, ByVal yOffset As Long) As Long
Private Declare Function GetSysColorBrush Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Const PS_SOLID = 0
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Enum EDrawTextFormat
   DT_BOTTOM = &H8
   DT_CALCRECT = &H400
   DT_CENTER = &H1
   DT_EXPANDTABS = &H40
   DT_EXTERNALLEADING = &H200
   DT_INTERNAL = &H1000
   DT_LEFT = &H0
   DT_NOCLIP = &H100
   DT_NOPREFIX = &H800
   DT_RIGHT = &H2
   DT_SINGLELINE = &H20
   DT_TABSTOP = &H80
   DT_TOP = &H0
   DT_VCENTER = &H4
   DT_WORDBREAK = &H10
   DT_EDITCONTROL = &H2000&
   DT_PATH_ELLIPSIS = &H4000&
   DT_END_ELLIPSIS = &H8000&
   DT_MODIFYSTRING = &H10000
   DT_RTLREADING = &H20000
   DT_WORD_ELLIPSIS = &H40000
End Enum
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Private Const OPAQUE = 2
Private Const TRANSPARENT = 1
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Private Type DrawObjectData
   iDirX As Integer
   iDirY As Integer
   foreColor As Long
   backColor As Long
   sCaption As String
   tP As POINTAPI
   lSize As Long
End Type
Private m_tDrawObject() As DrawObjectData
Private m_iDrawObjectCount As Long
Private m_font As IFont

Private m_bUseMemDc As Boolean
Private m_cMemDc As pcMemDC
Private m_tR As RECT


Public Property Get UseMemDc() As Boolean
   UseMemDc = m_bUseMemDc
End Property
Public Property Let UseMemDc(ByVal value As Boolean)
   m_bUseMemDc = value
End Property
Public Property Get Left() As Long
   Left = m_tR.Left
End Property
Public Property Let Left(ByVal value As Long)
   m_tR.Left = value
End Property
Public Property Get Top() As Long
   Top = m_tR.Top
End Property
Public Property Let Top(ByVal value As Long)
   m_tR.Top = value
End Property
Public Property Get Width() As Long
   Width = m_cMemDc.Width
End Property
Public Property Let Width(ByVal value As Long)
   m_cMemDc.Width = value
End Property
Public Property Get Height() As Long
   Height = m_cMemDc.Height
End Property
Public Property Let Height(ByVal value As Long)
   m_cMemDc.Height = value
End Property
Public Property Get Font() As IFont
   Set Font = m_font
End Property
Public Property Let Font(value As IFont)
   pSetFont value
End Property
Public Property Set Font(value As IFont)
   pSetFont value
End Property
Private Sub pSetFont(theFont As IFont)
   theFont.Clone m_font
End Sub


Public Sub Draw(ByVal lOutputDc As Long)
Dim tDrawR As RECT
Dim lhDC As Long

   ' set up the drawing rectangle:
   m_tR.Right = m_tR.Left + m_cMemDc.Width
   m_tR.Bottom = m_tR.Top + m_cMemDc.Height
  
   ' Get where we're drawing to:
   Dim lOffsetX As Long
   Dim lOffsetY As Long
   LSet tDrawR = m_tR
   If (m_bUseMemDc) Then
      lhDC = m_cMemDc.hdc
      OffsetRect tDrawR, -m_tR.Left, -m_tR.Top
   Else
      lhDC = lOutputDc
      lOffsetX = m_tR.Left
      lOffsetY = m_tR.Top
   End If
  
   ' draw the background:
   drawBackground lhDC, tDrawR
  
   Dim hFont As Long
   Dim hFontOld As Long
   hFont = SelectObject(lhDC, m_font.hFont)
   SetBkMode lhDC, TRANSPARENT
   ' draw the objects
   Dim i As Long
   For i = 1 To m_iDrawObjectCount
      drawObject lhDC, i, lOffsetX, lOffsetY
   Next i
   SelectObject lhDC, hFontOld
      
   ' if using a memory DC, we need to copy the result:
   If (m_bUseMemDc) Then
      m_cMemDc.Draw lOutputDc, , , , , m_tR.Left, m_tR.Top
   End If

End Sub
Private Sub drawBackground(ByVal lhDC As Long, tDrawR As RECT)
Dim hBr As Long
   hBr = GetSysColorBrush(vbWindowBackground And &H1F&)
   FillRect lhDC, tDrawR, hBr
   DeleteObject hBr
Dim hPen As Long
Dim hPenOld As Long
Dim tJunk As POINTAPI
   hPen = CreatePen(PS_SOLID, 1, GetSysColor(vbHighlight And &H1F&))
   hPenOld = SelectObject(lhDC, hPen)
   MoveToEx lhDC, tDrawR.Left, tDrawR.Top, tJunk
   LineTo lhDC, tDrawR.Right - 1, tDrawR.Top
   LineTo lhDC, tDrawR.Right - 1, tDrawR.Bottom - 1
   LineTo lhDC, tDrawR.Left, tDrawR.Bottom - 1
   LineTo lhDC, tDrawR.Left, tDrawR.Top
   SelectObject lhDC, hPenOld
   DeleteObject hPen
  
End Sub
Public Sub CreateObjects(ByVal iCount As Long)
   m_iDrawObjectCount = iCount
   ReDim Preserve m_tDrawObject(1 To m_iDrawObjectCount) As DrawObjectData
   Dim i As Long
   Dim lSize As Long
   Dim h As Single, s As Single, l As Single
   Dim r As Long, b As Long, g As Long
   Dim rf As Long, bf As Long, gf As Long
   RGBToHLS &H33, &H99, &HEE, h, s, l
   For i = 1 To m_iDrawObjectCount
      With m_tDrawObject(i)
         HLSToRGB h, s, l, r, g, b
         l = l * 0.95
         .backColor = RGB(r, g, b)
         HLSToRGB h, s, l * 4, rf, gf, bf
         .foreColor = RGB(rf, gf, bf)
         Do While (.iDirX = 0)
            .iDirX = Rnd * 8 - 4
         Loop
         Do While (.iDirY = 0)
            .iDirY = Rnd * 8 - 4
         Loop
         .sCaption = Hex(.backColor)
         .lSize = 32 + Rnd * 32
         .tP.x = Rnd * (m_cMemDc.Width - .lSize)
         .tP.y = Rnd * (m_cMemDc.Height - .lSize)
      End With
   Next i
End Sub

Private Sub drawObject(ByVal lhDC As Long, ByVal iIndex As Long, ByVal lOffsetX As Long, ByVal lOffsetY As Long)
   With m_tDrawObject(iIndex)
      ' update the position
      If (.tP.x + .lSize + .iDirX >= m_cMemDc.Width) Then
         .iDirX = -.iDirX
      End If
      If (.tP.x + .iDirX <= 0) Then
         .iDirX = -.iDirX
      End If
      If (.tP.y + .lSize + .iDirY >= m_cMemDc.Height) Then
         .iDirY = -.iDirY
      End If
      If (.tP.y + .iDirY <= 0) Then
         .iDirY = -.iDirY
      End If
      .tP.x = .tP.x + .iDirX
      .tP.y = .tP.y + .iDirY
      
      Dim hBr As Long
      Dim tR As RECT
      tR.Left = .tP.x
      tR.Top = .tP.y
      tR.Right = tR.Left + .lSize
      tR.Bottom = tR.Top + .lSize
      
      OffsetRect tR, lOffsetX, lOffsetY
      
      hBr = CreateSolidBrush(.backColor)
      FillRect lhDC, tR, hBr
      DeleteObject hBr
      
      SetTextColor lhDC, .foreColor
      DrawText lhDC, .sCaption, -1, tR, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE
   End With
  
  
End Sub

Private Sub RGBToHLS( _
     ByVal r As Long, ByVal g As Long, ByVal b As Long, _
     h As Single, s As Single, l As Single _
     )
Dim Max As Single
Dim Min As Single
Dim delta As Single
Dim rR As Single, rG As Single, rB As Single

     rR = r / 255: rG = g / 255: rB = b / 255

'{Given: rgb each in [0,1].
' Desired: h in [0,360] and s in [0,1], except if s=0, then h=UNDEFINED.}
         Max = Maximum(rR, rG, rB)
         Min = Minimum(rR, rG, rB)
             l = (Max + Min) / 2 '{This is the lightness}
         '{Next calculate saturation}
         If Max = Min Then
             'begin {Acrhomatic case}
             s = 0
             h = 0
             'end {Acrhomatic case}
         Else
             'begin {Chromatic case}
                 '{First calculate the saturation.}
             If l <= 0.5 Then
                 s = (Max - Min) / (Max + Min)
             Else
                 s = (Max - Min) / (2 - Max - Min)
             End If
             '{Next calculate the hue.}
             delta = Max - Min
             If rR = Max Then
                     h = (rG - rB) / delta '{Resulting color is between yellow and magenta}
             ElseIf rG = Max Then
                 h = 2 + (rB - rR) / delta '{Resulting color is between cyan and yellow}
             ElseIf rB = Max Then
                 h = 4 + (rR - rG) / delta '{Resulting color is between magenta and cyan}
             End If
         'end {Chromatic Case}
     End If
End Sub

Private Sub HLSToRGB( _
     ByVal h As Single, ByVal s As Single, ByVal l As Single, _
     r As Long, g As Long, b As Long _
     )
Dim rR As Single, rG As Single, rB As Single
Dim Min As Single, Max As Single

     If s = 0 Then
     ' Achromatic case:
     rR = l: rG = l: rB = l
     Else
     ' Chromatic case:
     ' delta = Max-Min
     If l <= 0.5 Then
         's = (Max - Min) / (Max + Min)
         ' Get Min value:
         Min = l * (1 - s)
     Else
         's = (Max - Min) / (2 - Max - Min)
         ' Get Min value:
         Min = l - s * (1 - l)
     End If
     ' Get the Max value:
     Max = 2 * l - Min
    
     ' Now depending on sector we can evaluate the h,l,s:
     If (h < 1) Then
         rR = Max
         If (h < 0) Then
             rG = Min
             rB = rG - h * (Max - Min)
         Else
             rB = Min
             rG = h * (Max - Min) + rB
         End If
     ElseIf (h < 3) Then
         rG = Max
         If (h < 2) Then
             rB = Min
             rR = rB - (h - 2) * (Max - Min)
         Else
             rR = Min
             rB = (h - 2) * (Max - Min) + rR
         End If
     Else
         rB = Max
         If (h < 4) Then
             rR = Min
             rG = rR - (h - 4) * (Max - Min)
         Else
             rG = Min
             rR = (h - 4) * (Max - Min) + rG
         End If
        
     End If
            
     End If
     r = rR * 255: g = rG * 255: b = rB * 255
End Sub
Private Function Maximum(rR As Single, rG As Single, rB As Single) As Single
   If (rR > rG) Then
      If (rR > rB) Then
         Maximum = rR
      Else
         Maximum = rB
      End If
   Else
      If (rB > rG) Then
         Maximum = rB
      Else
         Maximum = rG
      End If
   End If
End Function
Private Function Minimum(rR As Single, rG As Single, rB As Single) As Single
   If (rR < rG) Then
      If (rR < rB) Then
         Minimum = rR
      Else
         Minimum = rB
      End If
   Else
      If (rB < rG) Then
         Minimum = rB
      Else
         Minimum = rG
      End If
   End If
End Function


Private Sub Class_Initialize()
   Set m_cMemDc = New pcMemDC
   Dim s As New StdFont
   s.Name = "Arial"
   s.Size = 8
   Set m_font = s
End Sub


____________________________
Been busy trying to take a second degree <--it's not working out...

05-02-2004 at 06:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Reduce flickering

quote:
vbgen wrote:
hello, boyz... no fighting.. that's good...

*sighs, starting to think of the other boards where i play peacemaker*

anyway, there's a redrawing function going on inside vb.. and it handles it poorly...

so basically.. wait... can't explain it.. haven't installed vb here in this laptop yet... sorry! *bonks herself on the head*

well, what i can say is that it intercepts the msgs for drawing the images, then the code will do it properly for you..

noticeably... in C programs... not much flickering occurs, because you program at the base level... you do the code yourself..

i'll try to get vb here and explain, unless fabio gets to do it already.  


No vbgen, you don't have to worry about fights on this forum. And certainly not between Goran and fabulous.

I have gone and downloaded the code and this is what I have seen. It draws quite nicely flickering a bit on my machine until I use MemDC. I would suggest use of both. I am now going to get inside the code to analyse it. Have fun and hope this helps someone. Here goes.

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

____________________________
Attached:
er_Free_API_Drawing_Sample.zip 6 KB (Downloads: 1)
05-02-2004 at 06:42 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Reduce flickering

Hmmm, seems you (vbgen) and I were looking at the same code. At the same time. Well I guess now people can be sure of it's use.  

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

05-02-2004 at 06:54 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Reduce flickering

I have seen the code, only a bit so far. The pcMemDC class is required for the code to work without flickering. It seems that the cDrawSample class is to do your own drawing and needs the MemDC (Memory DC {Device Context}) to do it without flickering.

Here is the bit of code that turns of the flickering in the sample provided:

Private Sub chkFlickerFree_Click()
   If (chkFlickerFree.value = vbChecked) Then
      ' use mem dc
      m_c.UseMemDc = True
   Else
      ' don't use mem dc
      m_c.UseMemDc = False
   End If
End Sub
'I would have done it this way
'm_c.UseMemDc = (chkFlickerFree.Value = vbChecked)
'one line, same effect


With that in mind, you would want to use the MemDC class as well. Happy coding.

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

05-02-2004 at 07:09 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Reduce flickering

Activity: milions of questions seeking for answers. Place: my head. This code works with own drawing, and as it is said in the article it does the same thing as Autoredraw property (creates an off-screen buffer). Since image control doesnt have a hDC, Yronium would have to use picture box. I still dont understand why does Yronium get flickering and I dont, altough I have slower PC that he does.

Main question would be does anyone know how the picture property behaves? (what stuff is performing inside VB?) Does it also create an off-screen buffer after receiving an image, perfors drawing on it, and at last does bltblt stuff on control? If it does, then basically we have then some sort of already implemented code we have here. if it, doesnt (which might be the case since there is flickering), then Yronium, you would have to do next:

- create a DC and bitmap based on your picturebox's height and with.
- Resize your images with code, which might bring you back to transparency problem. While ago when I wrote some code similar to this code vbgen presented, I had problem which occured when I loaded a transparent picture in Bitmap object - it lost all transparent bits, so you would have to either use TransparentBlt API, or to create your own code for setting transparency color. All this might slow down execution enough to be noticable.
- bitblt to a picture box DC.

The sub's you can use from this code are CreateFromPicture and Draw sub's from pcMemDC class, with some modifications.

Dont know if all this stuff you have read from our posts helped you, but this might be the only way you can solve our problem.

____________________________
If you find the answer helpful, please mark this topic as solved.

06-02-2004 at 01:08 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Reduce flickering

This one only to advice you all that I solved my flickering problems. It seemed to be related to GIF format. In fact, since I converted my pics to BMP, I didn't experieced it anymore. I made a tryout also with JPGs and no problems occurred, though I got less quality on the images.

The effect is good, so I'm planning to post a speech about it in another site section. Thank you all.
  

____________________________
Real Programmer can count up to 1024 on his fingers

09-02-2004 at 08:54 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Reduce flickering
Previous Topic (Not a new topic probably)Next Topic (GRAM-SCHMODT ORTHOGONALIZATION) New Topic New Poll Post Reply
Surf To: