vb.net : 可根據窗體大小自動調整其中的控件的窗體,下一次打開時,可自動按照以前的位置和大小進行設置


 
  本窗體的所有功能都寫在這里, 其他窗體如果想具有這項功能,不需要做任何變動,只需要繼承這個窗體即可,比如:
 
Partial Class _frmSuspiciousAlert_case
        Inherits  AML._changableForm     ' System.Windows.Forms.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

            '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)
        '   遍歷窗體中的控件,重新設置控件的值
        For Each con As Control In cons.Controls
            'Console.WriteLine(con.Name & "---" & con.Tag.ToString)
            'Console.WriteLine("  setControls  " + con.Name)
            If dic_controlXY.ContainsKey(con) = False Then   '  如果字典中沒有這個控件
                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 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   ' 窗體調整大小時引發事件
        formLastX = Me.Width      '獲取窗體的寬度
        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)
    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 Sub xyz_read()   '  讀取文本文件中窗體的位置及大小信息,然后照此設置
        Dim filePath As String = Application.StartupPath & "\config\xyz"
        Dim filePathName As String = filePath & "\\" & Me.Name.ToString & ".txt"
     If IO.File.Exists(filePathName) = False Then    '  如果這個文件不存在
            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)))
 
    End Sub

    '  把當前窗體的大小及位置信息寫到文本文件中
    Private Sub xyz_write()
        '  剛開始時(根據timer判斷)
        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 strWrite As String = Me.Left & "," & Me.Top & "," & Me.Width & "," & Me.Height
        t.WriteLine(strWrite)
        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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM