VB 控件随窗体缩放

Option Explicit
Private FormOldWidth     As Long                              
Private FormOldHeight     As Long                             
Private count1 As Integer


Public Sub ResizeInit(FormName As Form)                        
    Dim Obj As Control                                         
    FormOldWidth = FormName.ScaleWidth
    FormOldHeight = FormName.ScaleHeight
On Error Resume Next
    For Each Obj In FormName                                   
        Obj.Tag = Obj.Left & "   " & Obj.Top & "   " & Obj.Width & "   " & Obj.Height & "   "
    Next Obj
On Error GoTo 0
End Sub


Public Sub ResizeForm(FormName As Form)                      
    Dim Pos(4) As Double
    Dim i As Long, TempPos As Long, StartPos As Long
    Dim Obj As Control
    Dim ScaleX As Double, ScaleY As Double
    ScaleX = FormName.ScaleWidth / FormOldWidth               
    ScaleY = FormName.ScaleHeight / FormOldHeight              
On Error Resume Next
    For Each Obj In FormName
        StartPos = 1
        For i = 0 To 4                                           
            TempPos = InStr(StartPos, Obj.Tag, "   ", vbTextCompare)
            If TempPos > 0 Then
                Pos(i) = VBA.Mid(Obj.Tag, StartPos, TempPos - StartPos)
                StartPos = TempPos + 1
            Else
                Pos(i) = 0
            End If                                             
        Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
        Next i
    Next Obj
On Error GoTo 0
End Sub

Private Sub Form_Load()                                        
    Image1.Picture = LoadPicture(App.Path + "\logo.jpg")
    Call ResizeInit(Me)
End Sub


Private Sub Form_Resize()                                     
    Call ResizeForm(Me)
End Sub
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现无边框窗体缩放,可以使用以下步骤: 1. 在窗体的属性中将BorderStyle设置为None,即去掉窗体的边框。 2. 添加一个Panel控件,将其Dock属性设置为Fill,使其覆盖整个窗体。 3. 在Panel控件中添加控件,并设置其位置和大小。 4. 使用MouseMove事件处理程序来捕获鼠标的移动事件。 5. 在MouseMove事件处理程序中判断鼠标是否在窗体的边缘,如果是,则改变鼠标的形状为缩放箭头。 6. 如果鼠标在窗体的边缘,则使用API函数来改变窗体的大小。 以下是一个示例代码: ``` Public Class Form1 Private Const WM_NCHITTEST As Integer = &H84 Private Const HTLEFT As Integer = 10 Private Const HTRIGHT As Integer = 11 Private Const HTTOP As Integer = 12 Private Const HTTOPLEFT As Integer = 13 Private Const HTTOPRIGHT As Integer = 14 Private Const HTBOTTOM As Integer = 15 Private Const HTBOTTOMLEFT As Integer = 16 Private Const HTBOTTOMRIGHT As Integer = 17 Private Const BORDER_WIDTH As Integer = 10 Private Const MIN_WIDTH As Integer = 100 Private Const MIN_HEIGHT As Integer = 100 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None Me.DoubleBuffered = True Me.SetStyle(ControlStyles.ResizeRedraw, True) Dim panel As New Panel() panel.Dock = DockStyle.Fill Me.Controls.Add(panel) ' Add controls to panel here. End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) MyBase.WndProc(m) If m.Msg = WM_NCHITTEST AndAlso Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None Then Dim pt As New Point(m.LParam.ToInt32()) pt = Me.PointToClient(pt) If pt.X < BORDER_WIDTH AndAlso pt.Y < BORDER_WIDTH Then m.Result = New IntPtr(HTTOPLEFT) ElseIf pt.X < BORDER_WIDTH AndAlso pt.Y > Me.ClientSize.Height - BORDER_WIDTH Then m.Result = New IntPtr(HTBOTTOMLEFT) ElseIf pt.X > Me.ClientSize.Width - BORDER_WIDTH AndAlso pt.Y < BORDER_WIDTH Then m.Result = New IntPtr(HTTOPRIGHT) ElseIf pt.X > Me.ClientSize.Width - BORDER_WIDTH AndAlso pt.Y > Me.ClientSize.Height - BORDER_WIDTH Then m.Result = New IntPtr(HTBOTTOMRIGHT) ElseIf pt.X < BORDER_WIDTH Then m.Result = New IntPtr(HTLEFT) ElseIf pt.X > Me.ClientSize.Width - BORDER_WIDTH Then m.Result = New IntPtr(HTRIGHT) ElseIf pt.Y < BORDER_WIDTH Then m.Result = New IntPtr(HTTOP) ElseIf pt.Y > Me.ClientSize.Height - BORDER_WIDTH Then m.Result = New IntPtr(HTBOTTOM) End If End If End Sub Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If e.Button = Windows.Forms.MouseButtons.None Then Dim pt As New Point(e.X, e.Y) pt = Me.PointToScreen(pt) If pt.X < Me.Left + BORDER_WIDTH AndAlso pt.Y < Me.Top + BORDER_WIDTH Then Me.Cursor = Cursors.SizeNWSE ElseIf pt.X < Me.Left + BORDER_WIDTH AndAlso pt.Y > Me.Bottom - BORDER_WIDTH Then Me.Cursor = Cursors.SizeNESW ElseIf pt.X > Me.Right - BORDER_WIDTH AndAlso pt.Y < Me.Top + BORDER_WIDTH Then Me.Cursor = Cursors.SizeNESW ElseIf pt.X > Me.Right - BORDER_WIDTH AndAlso pt.Y > Me.Bottom - BORDER_WIDTH Then Me.Cursor = Cursors.SizeNWSE ElseIf pt.X < Me.Left + BORDER_WIDTH Then Me.Cursor = Cursors.SizeWE ElseIf pt.X > Me.Right - BORDER_WIDTH Then Me.Cursor = Cursors.SizeWE ElseIf pt.Y < Me.Top + BORDER_WIDTH Then Me.Cursor = Cursors.SizeNS ElseIf pt.Y > Me.Bottom - BORDER_WIDTH Then Me.Cursor = Cursors.SizeNS Else Me.Cursor = Cursors.Default End If End If End Sub Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.Cursor <> Cursors.Default Then ResizeForm() End If End Sub Private Sub ResizeForm() Dim cursorX As Integer = Cursor.Position.X Dim cursorY As Integer = Cursor.Position.Y Dim screenRect As Rectangle = Screen.PrimaryScreen.WorkingArea Dim maxWidth As Integer = screenRect.Width - Me.Left - 5 Dim maxHeight As Integer = screenRect.Height - Me.Top - 5 Select Case Me.Cursor Case Cursors.SizeNWSE While Cursor.Position.X = cursorX AndAlso Cursor.Position.Y = cursorY Me.SetBounds(Me.Left, Me.Top, Math.Max(Me.Width - 1, MIN_WIDTH), Math.Max(Me.Height - 1, MIN_HEIGHT)) Application.DoEvents() End While Exit Select Case Cursors.SizeNESW While Cursor.Position.X = cursorX AndAlso Cursor.Position.Y = cursorY Me.SetBounds(Me.Left + 1, Me.Top, Math.Max(Me.Width - 1, MIN_WIDTH), Math.Max(Me.Height - 1, MIN_HEIGHT)) Application.DoEvents() End While Exit Select Case Cursors.SizeWE While Cursor.Position.X = cursorX Me.SetBounds(Me.Left, Me.Top, Math.Max(Math.Min(Cursor.Position.X - Me.Left, maxWidth), MIN_WIDTH), Math.Max(Me.Height, MIN_HEIGHT)) Application.DoEvents() End While Exit Select Case Cursors.SizeNS While Cursor.Position.Y = cursorY Me.SetBounds(Me.Left, Me.Top, Math.Max(Me.Width, MIN_WIDTH), Math.Max(Math.Min(Cursor.Position.Y - Me.Top, maxHeight), MIN_HEIGHT)) Application.DoEvents() End While Exit Select Case Cursors.SizeAll Dim xDiff As Integer = 0 Dim yDiff As Integer = 0 While Cursor.Position.X = cursorX AndAlso Cursor.Position.Y = cursorY xDiff = Cursor.Position.X - Me.Left yDiff = Cursor.Position.Y - Me.Top Me.SetBounds(Math.Max(Math.Min(Cursor.Position.X - xDiff, maxWidth), 0), Math.Max(Math.Min(Cursor.Position.Y - yDiff, maxHeight), 0), Math.Max(Me.Width, MIN_WIDTH), Math.Max(Me.Height, MIN_HEIGHT)) Application.DoEvents() End While Exit Select End Select End Sub End Class ``` 需要注意的是,此示例代码中使用了一些API函数来实现窗体缩放操作。如果不熟悉API函数的使用,可以先学习一下相关知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值