zooming (scaling) a window using a trackbar

B

Bernie Yaeger

I'd like to allow the user to zoom a window using a trackbar, say from 80%
to 120% the size of the original window. I'm trying to do this with the
following code, but it fails pretty miserably:
(from the form)

Dim per As Decimal

per = CDec(zoomit.Value)

setzoom(Me, Me.Font, per) ' per is the current value of the trackbar as a
decimal

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

Public Sub setzoom(ByRef senderform As Form, ByRef senderfont As Font, ByVal
per As Decimal)

per = per * 0.01

Dim iwidth As Int32 = SystemInformation.PrimaryMonitorSize.Width

Dim sizeold As SizeF =
System.Windows.Forms.Form.GetAutoScaleSize(senderfont)

senderfont = New Font(senderfont.FontFamily, senderfont.Size * iwidth /
1280)

' 1280 is my current environment width

Dim sizenew As SizeF =
System.Windows.Forms.Form.GetAutoScaleSize(senderfont)

sizenew.Width = sizenew.Width * per

sizenew.Height = sizenew.Height * per

senderform.Scale(sizenew.Width / sizeold.Width, sizenew.Height /
sizeold.Height)

End Sub

Thanks for any help.

Bernie Yaeger
 
C

Cor

Hi Bernie,

Have a look of this code, I did want to send it to you yesterday, but I did
not really test it.
The idea of this is that it changes recursevly the controls in a child
control.

I first made it with height and width, but when I saw your solution with
scale I changed it.
But I think that with height and width it is even nicer.

Cor



\\\To test
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim mescale As Single = 1.1
doScale.scale(Me, mescale)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim mescale As Single = 1 / 1.1
doScale.scale(Me, mescale)
End Sub
End Class
///
\\\
Public Class doScale
Public Shared Sub scale(ByVal ctr As Control, ByVal mescale As Single)
ctr.Width = CInt(ctr.Width * mescale)
ctr.Height = CInt(ctr.Height * mescale)
ctr.Scale(mescale)
Dim Berny As New doCtr(ctr, mescale)
End Sub
Private Class doCtr
Public Sub New(ByVal parentCtr As Control, ByVal mescale As Single)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Scale(mescale)
Dim newdoCtr As _
New doCtr(ctr, mescale)
Next
End Sub
End Class
///
 
J

Jay B. Harlow [MVP - Outlook]

Bernie,
I see two problems, well one problem and one potential problem.

I believe you want to only scale the font:
senderfont = New Font(senderfont.FontFamily, senderfont.Size * iwidth /
1280 * per)

Also while attempting to get it to work I would remove the "iwidth /1280"
logic, once your zooming works, I would then attempt at incorporating that
back in...


Now the "bigger" problem, which I currently don't have a solution.

After the first call to SetZoom, you are scaling the scaled form. Somehow
each time you call SetZoom, you need to "reset" the form to the original
size, then apply the new scale. Or maybe only zoom original values (not
current values)...

If you don't have it, you may want to review the section in Petzold's book
that explains how the code I gave you works...

Hope this helps
Jay
 
B

Bernie Yaeger

Hi Jay,

Actually, the code you gave me yesterday works splendidly when I scale based
purely on the current resolution of a given monitor. That's because it
scales only once.

I now want to extend that logic to a trackbar scale, but I now realize that
once I scale I don't have the original form to rescale - it's now another
size, not the resolution size. So, yes, either I maintain the original size
somewhere (could be done) or I scale the current scale value (eg, now it's
at .95 so multiply everything appropriately, etc).

As always, you've helped to put me on the right track. If you have any
further thoughts, please send them to me.

Thanks again,

Bernie
 
B

Bernie Yaeger

Hi Cor,

Thanks for your help. Yes, my problem is that I have to scale recursively
and my current code does not do that, so that is where I think I'm failing.

I will review your code, which has already given me some ideas.

Tx again for all your help,

Bernie
 
Top