vb.net(c#)winform所有控件(包括字体)跟随窗体同比列缩放
作者:互联网
写了一个类
大家使用的时候 在form的load事件里 加上一句 dim xxx as new Dim xxx As New chck(Me)
tabcontrol字体没有放大,因为放大后有点挤,如果有需要可以稍微改一下
c# 里把me改成this,代码是vb.net的,如果有人要c#的,可以联系我。
Class chck
Dim c As Control
Dim m0 As New stru
Sub New(ByVal c As Control)
Me.c = c
m0.l = c.Left
m0.t = c.Top
m0.w = c.Width
m0.h = c.Height
m0.n = c.Name
dgkj(c)
AddHandler c.Resize, AddressOf Me.c_Resize
End Sub
Dim kj As New Hashtable
Sub dgkj(ByVal c As Control)
'Dim d As DataGridView
'If c.GetType.Name = "DataGridView" Then
' MsgBox(c.Name)
'End If
'If kj.Count = 0 Then
' zjkj(c)
'End If
For Each cc As Control In c.Controls
zjkj(cc)
dgkj(cc)
Next
End Sub
Sub fdg(ByVal cc As Control)
Dim m As Control = cc.FindForm
'Dim m0 As stru = kj(m.Name)
For Each c As Control In cc.Controls
'If cc.Name = "TextBox3" Then
' MsgBox(cc.Name)
'End If
If kj(c.Name) IsNot Nothing Then
Dim x As stru = kj(c.Name)
c.Left = x.l * m.Width / m0.w
c.Top = x.t * m.Height / m0.h
c.Width = x.w * m.Width / m0.w
c.Height = x.h * m.Height / m0.h
'If c.Parent.Name = "Panel4" Then
' cx.RichTextBox1.Text &= c.Parent.Name & x.t & "," & m.Height & "," & m0.h & System.Environment.NewLine
'End If
'If c.Name = "PictureBox8" Then
' m.Text = c.Left & "," & m.Width
'End If
Select Case c.GetType.Name
Case "Label", "TextBox", "ComboBox", "DateTimePicker", _
"Button", "MenuStrip", "TreeView"
'If c.Name = "Label14" Then
' MsgBox(c.Name)
'End If
c.Font = New Font(c.Font.Name, x.s * m.Height / m0.h)
If c.GetType.Name = "TreeView" Then
Dim t As TreeView = c
t.ItemHeight = 26 * m.Height / m0.h
End If
End Select
Select Case c.GetType.Name
Case "ComboBox", "RichTextBox", "TextBox"
If c.Parent.GetType.Name = "Panel" Then
Dim p As Panel = c.Parent
If p.Name.Contains("pn") Then
p.Height = c.Height + 2
End If
End If
End Select
End If
fdg(c)
Next
End Sub
Structure stru
Dim n As String
Dim l, t, w, h As Integer
Dim s As Single
End Structure
Sub zjkj(ByVal cc As Control)
If kj(cc.Name) Is Nothing Then
Dim x As New stru
x.l = cc.Left
x.t = cc.Top
x.w = cc.Width
x.h = cc.Height
x.n = cc.Name
'If cc.GetType.Name = "MenuStrip" Then
' MsgBox(cc.Name)
'End If
Select Case cc.GetType.Name
Case "TextBox"
AddHandler cc.KeyPress, AddressOf Me.tx_KeyPress
End Select
Select Case cc.GetType.Name
Case "Label", "TextBox", "ComboBox", "DateTimePicker", _
"Button", "TabPage", "MenuStrip", "TreeView"
x.s = cc.Font.Size
End Select
kj.Add(cc.Name, x)
End If
End Sub
Private Sub c_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
Dim x = c.FindForm
If Not x.formshown Then Exit Sub
fdg(c)
End Sub
Private Sub tx_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = Chr(Keys.Return) Then
e.Handled = True
End If
End Sub
End Class
标签:控件,vb,End,Name,缩放,cc,m0,Dim,Sub 来源: https://blog.csdn.net/fanyx_bj/article/details/122765701