Results 1 to 4 of 4

Thread: Programming a simple Zoom in / Zoom out

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Programming a simple Zoom in / Zoom out

    I'm writting a program where I use rectangle or lines to represent the data.
    I've 6 zoom levels: from 0 (the minimum zoom, the smaller) to 5 (the maximum zoom, the bigger).

    Depending what zoom level I'm applying at the moment the program represents the data in lines or rectangles:
    Zoom 0 or 1: Lines
    Zoom 2, 3, 4, 5: rectangles



    I'm testing the next piece of code but it's seems that I'm doing something wrong:
    Code:
    Public Class MyForm
    Dim ZoomValue as Integer 'stores the actual value of the zoom level (0 to 5)
    Dim MyRectangleWidthValue as Integer 'stores the width that I want to apply to the rectangle (only, no lines) when we use the zoom
    
    Private Sub CreateShape_Button_Click(sender As System.Object, e As System.EventArgs) Handles CrearteShape_Button.Click
    'creates a new rectangle or line to apply zoom
    
    
    Me.Refresh() 'Erases the form in case that we've previously drawn any picture Dim MyPencil as Pen MyPencil = New Pen (Color.Black, 1) Select Case ZoomValue 'here depending the actual value of the zoom the program chooses to draw a line or a rectangle Case 0 to 1 Me.CreateGraphics.DrawLine(MyPencil, 50, 50, 50, 60) Case 2 to 5 Me.CreateGraphics.DrawRectangle(Pincel, 50, 50, MyRectangleWidthValue, 20) End Select MyPencil.Dispose()
    End Sub Private Sub ApplyZoomValue (MyValue as Integer)
    Select Case Value Case Is = 0 ZoomValue = 0 MyRectangleWidthValue = 5 Case Is = 1 ZoomValue = 1 MyRectangleWidthValue = 10 (etc...) End Select
    End Sub Private Sub IncreaseZoom_Button_Click(sender As System.Object, e As System.EventArgs) Handles IncreaseZoom_Button.Click Select Case MyRectangleWidthValue Case 0 To 4 ApplyZoomValue(ZoomValue + 1) Case 5 ApplyZoomValue(ZoomValue + 1) IncreaseZoom_Button.Enabled = False End Select CreateShape_Button_Click(Nothing, Nothing) End Sub
    All is ok, or do you know other way to perform the same task?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Programming a simple Zoom in / Zoom out

    Well, you're still doing this with CreateGraphics in a button click rather than in the Paint event with the supplied Graphics object, so that's a problem. Other than that, what problem are you having?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: Programming a simple Zoom in / Zoom out

    Quote Originally Posted by Shaggy Hiker View Post
    Well, you're still doing this with CreateGraphics in a button click rather than in the Paint event with the supplied Graphics object, so that's a problem. Other than that, what problem are you having?
    Yes, I tried to do with the Paint event also. The problem here is that it works fine, but I don't know why it doesn't pass over the zoom level 4. I'm modifying the code.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Programming a simple Zoom in / Zoom out

    What does "pass over the zoom level 4" mean?

    By the way, I'm surprised that you say this works fine. After all, you have this statement at the beginning of the CreateShape handler:

    Me.Refresh() 'Erases the form in case that we've previously drawn any picture

    Therefore, you know what Me.Refresh can do. Are you also aware that Me.Invalidate will do the same thing? Are you aware that both are just doing what happens all over the place in any Windows App? All that is happening is that the screen area is invalidated and told to redraw itself. Refresh is more imperative than Invalidate, so it stops and draws at that moment rather than as soon as it gets a chance. All kinds of other processes inside and outside of the app will do the same thing, and your drawings will then be erased, just as you say in your comment.

    If you do the drawing in the Paint event, then YOU are in control of what the form looks like. If you do the drawing as you are then you are at the mercy of the OS as to whether or not the program does as you wish. It may work at some times and not at others. Frankly, I'm a bit surprised that you haven't already triggered an automatic Me.Invalidate as a side effect of something you have done, which would mean your drawing would be instantly erased. I guess you haven't triggered such a side effect, though....yet, but you will eventually.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width