GDI Scrolling Problem

fredriko

Active member
Joined
Jun 3, 2004
Messages
35
Programming Experience
10+
I am creating a user control that displays two boxes. Box 1 is displayed at the top of the control and Box 2 is positioned just below it.
I have set AutoScroll=True and purposefully set the AutoScrollMinimum size to twice the height of the user control in order to force a vertical scrollbar.
I want to be able to scroll up and down the user control and have two things happen
1. Box1 moves in and out of the screen depending on how far I scroll
2. Box2 remains stationary in the same position regardless of where the user scrolls.

I have overriden the OnPaint method of the usercontrol with the code below
VB.NET:
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        'Set the minimum autoscroll size and force a scroll
        Me.AutoScrollMinSize = New Size(Me.Width, Me.Height * 2)
        Dim oBrush1 As New SolidBrush(Color.White)
        Dim oBrush2 As New SolidBrush(Color.Red)
        Dim oPen As New Pen(Color.Black)
        Dim oBoxSize As New Size(100, 50)
        Dim oPosition As New Point(40, 40)

        'Draw a box which is scrolled when the scrollbar is moved
        Dim oRect As New Rectangle(oPosition, oBoxSize)
        e.Graphics.FillRectangle(oBrush1, oRect)
        e.Graphics.DrawRectangle(oPen, oRect)
        Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)

        'Draw a fixed box that doesn't move (use autoscroll to calculate the new position)
        oPosition = New Point(40 - Me.AutoScrollPosition.X, 100 - Me.AutoScrollPosition.Y)
        oRect = New Rectangle(oPosition, oBoxSize)
        e.Graphics.FillRectangle(oBrush2, oRect)
        e.Graphics.DrawRectangle(oPen, oRect)
        Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)

        'Cleanup the objects
        oBrush1.Dispose()
        oBrush2.Dispose()
        oPen.Dispose()

        'Pass the call down
        MyBase.OnPaint(e)
    End Sub
The problem I am having is that the Box 2 does not remaing stationary as I expected and also if you scroll quickly neither box draws correctly and leaves lines and colors all over the control?? I have set various styles to control this to no affect. Can anybody help me understand where I am going wrong?

Thanks in advance.:eek:
 
'Draw a box which is scrolled when the scrollbar is moved
Dim oRect As New Rectangle(oPosition, oBoxSize)
e.Graphics.FillRectangle(oBrush1, oRect)
e.Graphics.DrawRectangle(oPen, oRect)
Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)

'Draw a fixed box that doesn't move (use autoscroll to calculate the new position)
oPosition = New Point(40 - Me.AutoScrollPosition.X, 100 - Me.AutoScrollPosition.Y)
oRect = New Rectangle(oPosition, oBoxSize)
e.Graphics.FillRectangle(oBrush2, oRect)
e.Graphics.DrawRectangle(oPen, oRect)
Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)

I'm struggling a bit here. To me it looks like the part highlighted in red is the box that is going to scroll even though your comment suggests otherwise. If i were you i'd forget about the autoscroll here and just add a scrollbar to your control at the correct location. Then in the ValueChanged event you can set up the new location of the rectangle you want to move. Then you would call Me.Invalidate to force a re-paint of the control. I can't do a demo right now as i'm out on call (working hard!!;) ) If no-one else has by tonight i'll see if I can put something together.

oPosition should not be set in the paint event rather should be a property of that class and should be adjusted from the ValueChanged event. Thats how i would go about it anyway.
 
Thanks for that I'll give it a try. I'd appreciate you're best solution if you have time as I'm keen to learn the best way to approach these things.
FYI, the reason I am looking to have a stationary rectangle is that I want to eventually draw a box (or several of them depending on certain properties) in the middle of the control which will scroll up and down.
I would then like to draw a border (whose color and thickness I can change) around the control which should not scroll with the previously drawn boxes.
I wanted to write the whole control using GDI+ rather than placing other controls on a panel so I could learn more about it.
 
Here you go fredriko, I knocked up a small demo for you. It just the class beacuse you are using 2003. Just add it to a control library project and drop it onto your form. No commenting i'm afaid but the code is dead simple. Even put in a little plug for Vb.Net forum:)
 

Attachments

  • ScrollBarDemo.zip
    1.9 KB · Views: 57
Back
Top