本窗体的所有功能都写在这里, 其他窗体如果想具有这项功能,不需要做任何变动,只需要继承这个窗体即可,比如:
Partial Class _frmSuspiciousAlert_case
Inherits AML._changableForm ' System.Windows.Forms.Form
Inherits AML._changableForm ' System.Windows.Forms.Form
下面是窗体的代码
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 可根据窗体尺寸自动调整其中的控件大小的窗体
Public Class _changableForm
Inherits Form ' 继承自正常的窗体
Public Class _changableForm
Inherits Form ' 继承自正常的窗体
#Region "记录及设置 窗体上的控件大小"
' 字典:各个控件 及其 位置和大小信息
Private dic_controlXY As Dictionary(Of Control, String) = New Dictionary(Of Control, String)
' 获取各个控件的 left、top、width、height、字体大小的值
Private Sub getControlsSize(cons As Control)
' 遍历窗体中的控件
For Each con As Control In cons.Controls
'Console.WriteLine(" ----------- " + con.Name )
' 记下这个控件 的 左距、上距、 宽度、高度、字体大小
'con.Tag = con.Width & ":" & con.Height & ":" & con.Left & ":" & con.Top & ":" & con.Font.Size
Dim _value As String = con.Left & ":" & con.Top & ":" & con.Width & ":" & con.Height & ":" & con.Font.Size
If dic_controlXY.ContainsKey(con) Then
dic_controlXY(con) = _value
Else
dic_controlXY.Add(con, _value)
End If
Dim _value As String = con.Left & ":" & con.Top & ":" & con.Width & ":" & con.Height & ":" & con.Font.Size
If dic_controlXY.ContainsKey(con) Then
dic_controlXY(con) = _value
Else
dic_controlXY.Add(con, _value)
End If
'Console.WriteLine( con.Tag )
'Console.WriteLine(" con.Controls.Count = " + con.Controls.Count)
If con.Controls.Count > 0 Then ' 如果这个控件内部还有其它控件
getControlsSize(con) ' 递归调用
End If
Next
End Sub
''' 根据 窗体的新的大小调整 各个控件 的 大小
Private Sub setControlsSize(newx As Single, newy As Single, cons As Control)
Private Sub setControlsSize(newx As Single, newy As Single, cons As Control)
' 遍历窗体中的控件,重新设置控件的值
For Each con As Control In cons.Controls
'Console.WriteLine(con.Name & "---" & con.Tag.ToString)
'Console.WriteLine(" setControls " + con.Name)
'Console.WriteLine(" setControls " + con.Name)
If dic_controlXY.ContainsKey(con) = False Then ' 如果字典中没有这个控件
Continue For ' 跳过去
End If
Continue For ' 跳过去
End If
'Dim mytag As String() = con.Tag.ToString().Split(":") '获取控件的Tag属性值,并分割后存储字符串数组
Dim mytag As String() = dic_controlXY(con).Split(":") '获取控件的Tag属性值,并分割后存储字符串数组
Dim a As Single = Convert.ToSingle(mytag(0)) * newx '根据窗体缩放比例确定控件的值, 宽度
con.Left = CInt(a) '左边缘距离
'Console.WriteLine(" con.Width = " + con.Width )
a = Convert.ToSingle(mytag(1)) * newy
con.Top = CInt(a) ' 上边缘距离
'Console.WriteLine(" con.Height = " + con.Height )
a = Convert.ToSingle(mytag(2)) * newx
con.Width = CInt(a) '宽度
'Console.WriteLine(" con.Left = " + con.Left)
a = Convert.ToSingle(mytag(3)) * newy
con.Height = CInt(a) '高度
'Console.WriteLine(" con.Top = " + con.Top )
Dim fontSize As Single = Convert.ToSingle(mytag(4)) * newy '字体大小
con.Font = New Font(con.Font.Name, fontSize, con.Font.Style, con.Font.Unit)
If con.Controls.Count > 0 Then ' 如果本控件内部还有其它控件
setControlsSize(newx, newy, con) ' 则进行嵌套处理
End If
Next
End Sub
#End Region
Dim timer1 As Timer ' 定时器,用于在窗体加载成功以后调用相关功能
Private Sub timer1_Tick()
timer1.Enabled = False
xyz_read() ' 读取尺寸位置信息,并照此进行设置
Me.WindowState = FormWindowState.Normal ' 注意,必须将 FormWindowState设置为Normal,上面的设置方可生效
' MessageBox.Show("timer1_Tick")
End Sub
' 在鼠标拖动变化之前,本窗体的大小
Private formLastX As Single
Private formLastY As Single
Private formLastX As Single
Private formLastY As Single
Private Sub _changableForm_Load(sender As Object, e As EventArgs) Handles Me.Load
' MaximizeBox = False ' 最大化按钮不可用;
' 原因: 点击最大化按钮, 窗体最大化,但不会激发Resize动作,所以已经缩小的控件不会随窗体而放大,比较难看
' 有待解决: 窗体最大化的事件,该如何捕捉?
'AddHandler Me.ResizeEnd, AddressOf Form1_Resize ' 窗体调整大小时引发事件
' 注意,不要用上面 ,因为窗体的最大化无法激发ResizeEnd,但可以激发下面的 Resize
'AddHandler Me.Resize, AddressOf _changableForm_Resize ' 窗体调整大小时引发事件
' 注意,不要用上面 ,因为窗体的最大化无法激发ResizeEnd,但可以激发下面的 Resize
'AddHandler Me.Resize, AddressOf _changableForm_Resize ' 窗体调整大小时引发事件
formLastX = Me.Width '获取窗体的宽度
formLastY = Me.Height '获取窗体的高度
getControlsSize(Me) '调用方法,将每个控件的 尺寸、位置 都标记下来,作为以后随窗体而变化的基础
formLastY = Me.Height '获取窗体的高度
getControlsSize(Me) '调用方法,将每个控件的 尺寸、位置 都标记下来,作为以后随窗体而变化的基础
timer1 = New Timer
timer1.Interval = 500 '5000 ‘ 执行时间
timer1.Enabled = True ' 定时器开始执行
AddHandler timer1.Tick, AddressOf timer1_Tick ' 引发事件
End Sub
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'_changableForm
'
Me.ClientSize = New Size(284, 261)
Me.Name = "_changableForm"
Me.ResumeLayout(False)
Me.SuspendLayout()
'
'_changableForm
'
Me.ClientSize = New Size(284, 261)
Me.Name = "_changableForm"
Me.ResumeLayout(False)
End Sub
' 当窗体的大小被改变时,获取其宽度和高度被改变的比例,按照新的比例调整 各个控件的大小和位置
Private Sub _changableForm_Resize(sender As Object, e As EventArgs) Handles Me.Resize ' MyBase.Resize
Dim newx As Single = Convert.ToSingle(Me.Width / formLastX) ' 窗体宽度缩放比例
Dim newy As Single = Convert.ToSingle(Me.Height / formLastY) ' 窗体高度缩放比例
setControlsSize(newx, newy, Me) '随窗体大小改变控件大小
' this.Text = this.Width.ToString() + " " + this.Height.ToString()'窗体标题栏文本
xyz_write() ' 当尺寸变化时,记录下来当时的窗体大小及位置
End Sub
Private filePath As String ' = Application.StartupPath & "\config\xyz"
Private filePathName As String ' = filePath & "\\" & Me.Name.ToString & ".txt"
Private filePathName As String ' = filePath & "\\" & Me.Name.ToString & ".txt"
Private Sub xyz_read() ' 读取文本文件中窗体的位置及大小信息,然后照此设置
Dim filePath As String = Application.StartupPath & "\config\xyz"
Dim filePathName As String = filePath & "\\" & Me.Name.ToString & ".txt"
Dim filePathName As String = filePath & "\\" & Me.Name.ToString & ".txt"
If IO.File.Exists(filePathName) = False Then ' 如果这个文件不存在
Return ' 退出
End If
Return ' 退出
End If
Dim reader As IO.TextReader = IO.File.OpenText(filePathName) ' 打开文件
Dim line As String = reader.ReadLine() '读第一行
reader.Close()
'MessageBox.Show(line, "xyz_read() 读取文件")
Dim ss As String() = line.Split(",")
Me.Left = CInt(ss(0))
Me.Top = CInt(ss(1))
Me.Width = CInt(ss(2))
Me.Height = CInt(ss(3))
' 上面下面,效果一样
' SetBounds(CInt(ss(0)), CInt(ss(1)), CInt(ss(2)), CInt(ss(3)))
Me.Top = CInt(ss(1))
Me.Width = CInt(ss(2))
Me.Height = CInt(ss(3))
' 上面下面,效果一样
' SetBounds(CInt(ss(0)), CInt(ss(1)), CInt(ss(2)), CInt(ss(3)))
End Sub
' 把当前窗体的大小及位置信息写到文本文件中
Private Sub xyz_write()
' 刚开始时(根据timer判断)
If IsNothing(timer1) OrElse timer1.Enabled Then
Return ' 退出
End If
If IsNothing(timer1) OrElse timer1.Enabled Then
Return ' 退出
End If
filePath = Application.StartupPath & "\config\xyz"
If IO.File.Exists(filePath) = False Then ' 如果这个文件夹不存在
IO.Directory.CreateDirectory(filePath) ' 新建这个文件夹
End If
filePathName = filePath & "\\" & Me.Name.ToString & ".txt" ' 文件名,即窗体的名称
' 写入 日志文件
'Dim t As IO.StreamWriter = New IO.StreamWriter(PathUserData, True, System.Text.Encoding.UTF8)
Dim t As IO.StreamWriter = New IO.StreamWriter(filePathName, False, System.Text.Encoding.UTF8)
'Dim t As IO.StreamWriter = New IO.StreamWriter(PathUserData, True, System.Text.Encoding.UTF8)
Dim t As IO.StreamWriter = New IO.StreamWriter(filePathName, False, System.Text.Encoding.UTF8)
Dim strWrite As String = Me.Left & "," & Me.Top & "," & Me.Width & "," & Me.Height
t.WriteLine(strWrite)
t.Close()
t.Close()
End Sub
' 当窗体的位置及大小变化时,记录下来
Private Sub _changableForm_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
', Me.Resize 'MyBase.LocationChanged
' 奇怪,不能同时 handle Me.Resize,否则界面不正常
xyz_write()
End Sub
End Class