كود :
Imports System.Runtime.InteropServices
Public Class Form1
Private rect As Rectangle
Private m_mode As Modes = Modes.None
#Region " Enum & Constants "
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_DRAGMOVE As Integer = &HF012
Private Const SC_DRAGSIZE_N As Integer = &HF003
Private Const SC_DRAGSIZE_S As Integer = &HF006
Private Const SC_DRAGSIZE_E As Integer = &HF002
Private Const SC_DRAGSIZE_W As Integer = &HF001
Private Const SC_DRAGSIZE_NW As Integer = &HF004
Private Const SC_DRAGSIZE_NE As Integer = &HF005
Private Const SC_DRAGSIZE_SW As Integer = &HF007
Private Const SC_DRAGSIZE_SE As Integer = &HF008
Private Enum Modes
None
Bottom
BottomLeft
BottomRight
Left
Right
Top
TopLeft
TopRight
End Enum
#End Region
#Region " Methods "
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim rect As New Rectangle(0, 0, Me.Width, 29)
Using lgb As New Drawing2D.LinearGradientBrush(rect, Color.Cyan, Color.Blue, 90, True)
e.Graphics.FillRectangle(lgb, rect)
lgb.Dispose()
End Using
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
'MyBase.OnMouseDown(e)
Dim hwnd As IntPtr = Me.Handle
Select Case m_mode
Case Modes.Bottom
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_S, 0)
Exit Select
Case Modes.Right
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_E, 0)
Exit Select
Case Modes.BottomRight
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_SE, 0)
Exit Select
Case Modes.TopRight
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_NE, 0)
Exit Select
Case Modes.Top
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_N, 0)
Exit Select
Case Modes.TopLeft
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_NW, 0)
Exit Select
Case Modes.Left
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_W, 0)
Exit Select
Case Modes.BottomLeft
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGSIZE_SW, 0)
Exit Select
Case Modes.None
NativeAPI.ReleaseCapture(hwnd)
NativeAPI.SendMessage(hwnd, WM_SYSCOMMAND, SC_DRAGMOVE, 0)
Exit Select
End Select
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
Me.Cursor = Cursors.[Default]
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If (e.X < 5) AndAlso (e.Y < 5) Then
Me.Cursor = Cursors.SizeNWSE
m_mode = Modes.TopLeft
ElseIf (e.X < 5) AndAlso Math.Abs(e.Y - Me.ClientSize.Height) < 5 Then
Me.Cursor = Cursors.SizeNESW
m_mode = Modes.BottomLeft
ElseIf e.X < 5 Then
Me.Cursor = Cursors.SizeWE
m_mode = Modes.Left
ElseIf Math.Abs(e.X - Me.Width) < 5 AndAlso (e.Y < 5) Then
Me.Cursor = Cursors.SizeNESW
m_mode = Modes.TopRight
ElseIf Math.Abs(e.X - Me.Width) < 5 AndAlso Math.Abs(e.Y - Me.ClientSize.Height) < 5 Then
Me.Cursor = Cursors.SizeNWSE
m_mode = Modes.BottomRight
ElseIf Math.Abs(e.X - Me.Width) < 5 Then
Me.Cursor = Cursors.SizeWE
m_mode = Modes.Right
ElseIf Math.Abs(e.Y - Me.ClientSize.Height) < 5 Then
Me.Cursor = Cursors.SizeNS
m_mode = Modes.Bottom
ElseIf e.Y < 5 Then
Me.Cursor = Cursors.SizeNS
m_mode = Modes.Top
Else
Me.Cursor = Cursors.Default
m_mode = Modes.None
End If
End Sub
#End Region
#Region " Internal Class "
Private Class NativeAPI
<DllImport("USER32.DLL", EntryPoint:="SendMessage")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
End Function
<DllImport("USER32.DLL")> _
Public Shared Function ReleaseCapture(ByVal hwnd As IntPtr) As Integer
End Function
End Class
#End Region
End Class