[VB.NET]表單靠近視窗邊緣自動貼齊

  • 15258
  • 0
  • 2011-08-24

[VB.NET]表單靠近視窗邊緣自動貼齊

 

一、簡介

本文說明如何在表單靠近視窗桌面上下左右時,自動貼齊邊緣

 

二、方法

在控制項移動時會觸發 Move 事件中,因此我們在 Control.Move 事件 中去判斷目前表單座標是否貼近邊緣,當貼近邊緣時就自動貼齊


Public Class Form1

    Const iThreshold = 10
    Private Sub Form1_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move
        ' 左
        If Me.Location.X < iThreshold Then
            Me.Location = New Point(0, Me.Location.Y)
        End If

        ' 上
        If Me.Location.Y < iThreshold Then
            Me.Location = New Point(Me.Location.X, 0)
        End If

        ' 右
        If Me.Location.X + Me.Width + iThreshold > Screen.PrimaryScreen.WorkingArea.Right Then
            Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Right - Me.Width, Me.Location.Y)
        End If

        ' 下
        If Me.Location.Y + Me.Height + iThreshold > Screen.PrimaryScreen.WorkingArea.Bottom Then
            Me.Location = New Point(Me.Location.X, Screen.PrimaryScreen.WorkingArea.Bottom - Me.Height)
        End If
    End Sub

End Class