Problem : Completely removing a VBA form’s window border

Problem : Completely removing a VBA form’s window border

I am trying to remove all window frame artifacts from a VBA user form. Using the code below I can successfully remove the title bar but the three-pixel wide frame remains.

Public Sub RemoveWindowFrame(TargetForm As Object)
Dim Style As Long
Dim HWnd As Long
If Val(Application.Version) < 9 Then
‘ Excel 97 or earlier
HWnd = FindWindow(“ThunderXFrame”, TargetForm.Caption)
Else
‘ Excel 2000 or later
HWnd = FindWindow(“ThunderDFrame”, TargetForm.Caption)
End If
Style = GetWindowLong(HWnd, GWL_STYLE)
Style = Style And Not (WS_CAPTION Or WS_THICKFRAME Or WS_DLGFRAME Or WS_BORDER)
SetWindowLong HWnd, GWL_STYLE, Style
SetWindowPos HWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
DrawMenuBar HWnd
End Sub

Has anyone been successful achieving this objective? I haven’t posted any of the declarations as anyone who does not already have them in place will probably not be able to help.


Solution : Completely removing a VBA form’s window border

Played with this code

Public Sub RemoveWindowFrame(TargetForm As Object)
Dim lOffstX As Long: lOffstX = 1
Dim lOffstY As Long: lOffstY = 1
Dim hRgn    As Long
Dim rcBox   As Rect
Dim hwnd As Long
Dim Style As Long
If Val(Application.Version) < 9 Then
‘ Excel 97 or earlier
hwnd = FindWindow(“ThunderXFrame”, TargetForm.Caption)
Else
‘ Excel 2000 or later
hwnd = FindWindow(“ThunderDFrame”, TargetForm.Caption)
End If
Style = GetWindowLong(hwnd, GWL_STYLE)
Style = Style And Not (enWindowStyles.WS_CAPTION Or enWindowStyles.WS_THICKFRAME Or enWindowStyles.WS_DLGFRAME Or enWindowStyles.WS_BORDER)
SetWindowLong hwnd, GWL_STYLE, Style
SetWindowPos hwnd, 0, 0, 0, 0, 0, enSetWindowPos.SWP_FRAMECHANGED Or enSetWindowPos.SWP_NOMOVE Or enSetWindowPos.SWP_NOZORDER Or enSetWindowPos.SWP_NOSIZE
DrawMenuBar hwnd
If GetClientRect(hwnd, rcBox) Then
With rcBox
‘Offset the rectangle to exclude the borders.
.Left = .Left + lOffstX
.Top = .Top + lOffstY
.Right = .Right + lOffstX
.Bottom = .Bottom + lOffstY
‘Create a region from the rectangle
hRgn = CreateRectRgn(.Left, .Top, .Right, .Bottom)
‘Set the Form’s window region to this region
Call SetWindowRgn(hwnd, hRgn, True)
‘Note: Do not delete this region.
‘It is now owned by Windows and Windows
‘will destroy it when the Form unloads.
End With
End If
End Sub

the work is in the lines
Dim lOffstX As Long: lOffstX = 1
Dim lOffstY As Long: lOffstY = 1

if you set them to anything smaller then 1 it will show a thin frame around the form

to be a good sport [it took me some time to find yours together] i’ll post my declarations 😉

‘–>>
Private Type Rect
Left    As Long
Top     As Long
Right   As Long
Bottom  As Long
End Type

Private Const GWL_STYLE As Long = (-16)

‘\\ Window Style
Public Enum enWindowStyles
WS_BORDER = &H800000
WS_CAPTION = &HC00000
WS_CHILD = &H40000000
WS_CLIPCHILDREN = &H2000000
WS_CLIPSIBLINGS = &H4000000
WS_DISABLED = &H8000000
WS_DLGFRAME = &H400000
WS_EX_ACCEPTFILES = &H10&
WS_EX_DLGMODALFRAME = &H1&
WS_EX_NOPARENTNOTIFY = &H4&
WS_EX_TOPMOST = &H8&
WS_EX_TRANSPARENT = &H20&
WS_EX_TOOLWINDOW = &H80&
WS_GROUP = &H20000
WS_HSCROLL = &H100000
WS_MAXIMIZE = &H1000000
WS_MAXIMIZEBOX = &H10000
WS_MINIMIZE = &H20000000
WS_MINIMIZEBOX = &H20000
WS_OVERLAPPED = &H0&
WS_POPUP = &H80000000
WS_SYSMENU = &H80000
WS_TABSTOP = &H10000
WS_THICKFRAME = &H40000
WS_VISIBLE = &H10000000
WS_VSCROLL = &H200000
‘\\ New from 95/NT4 onwards
WS_EX_MDICHILD = &H40
WS_EX_WINDOWEDGE = &H100
WS_EX_CLIENTEDGE = &H200
WS_EX_CONTEXTHELP = &H400
WS_EX_RIGHT = &H1000
WS_EX_LEFT = &H0
WS_EX_RTLREADING = &H2000
WS_EX_LTRREADING = &H0
WS_EX_LEFTSCROLLBAR = &H4000
WS_EX_RIGHTSCROLLBAR = &H0
WS_EX_CONTROLPARENT = &H10000
WS_EX_STATICEDGE = &H20000
WS_EX_APPWINDOW = &H40000
WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
End Enum

Public Enum enSetWindowPos
SWP_FRAMECHANGED = &H20 ‘ The frame changed: send WM_NCCALCSIZE
SWP_HIDEWINDOW = &H80
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200 ‘ Don’t do owner Z ordering
SWP_NOREDRAW = &H8
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_SHOWWINDOW = &H40
End Enum

Private Declare Function GetClientRect Lib “user32” _
(ByVal hwnd As Long, lpRect As Rect) As Long
Private Declare Function CreateRectRgn Lib “gdi32” _
(ByVal X1 As Long, ByVal Y1 As Long, _
ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib “user32” _
(ByVal hwnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare Function FindWindow Lib “user32” _
Alias “FindWindowA” (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib “user32” _
Alias “GetWindowLongA” _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowPos Lib “user32” _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function SetWindowLong Lib “user32” _
Alias “SetWindowLongA” ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib “user32” _
(ByVal hwnd As Long) As Long
‘<<–

this is my only post for tonight so i’ll catch maybe tomorrow see if it helped

———-
bruintje
share what you know, learn what you don’t