Forum Discussion
BORDERS USERFORM
Hi, there a way to put round, rectangle borders on Userform?
I see a guy on youtube and i notice that, it's very nice the corners on his project.
He even put a shadow effect, but on the video he did not teach how to do that.
LINK: in case someone want so see
https://www.youtube.com/watch?v=X-CY7Gilo5w
- Mks_1973Iron Contributor
To add rounded corners to your UserForm, you can use the CreateRoundRectRgn and SetWindowRgn functions from the Windows API.
Add the following code at the top of your UserForm code module to declare the necessary API functions:Private Declare PtrSafe Function CreateRoundRectRgn Lib "gdi32" ( _
ByVal nLeftRect As Long, ByVal nTopRect As Long, _
ByVal nRightRect As Long, ByVal nBottomRect As Long, _
ByVal nWidthEllipse As Long, ByVal nHeightEllipse As Long) As LongPrivate Declare PtrSafe Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
=======================Add the following code in the UserForm_Initialize event to set rounded corners:
Private Sub UserForm_Initialize()
Dim hRgn As Long
Dim radius As Long
radius = 20 ' Adjust this value to change the roundness of the corners
hRgn = CreateRoundRectRgn(0, 0, Me.Width * Screen.TwipsPerPixelX, Me.Height * Screen.TwipsPerPixelY, radius, radius)
SetWindowRgn Me.hWnd, hRgn, True
End Sub
***Adjust the radius variable to control the level of roundness ***
=========================
Please ensure Drop Shadow is Enabled: (check the below)Private Declare PtrSafe Function SetClassLongPtr Lib "user32" Alias "SetClassLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtrPrivate Const CS_DROPSHADOW As Long = &H20000
Private Sub UserForm_Initialize()
Dim classLong As LongPtr
classLong = GetClassLongPtr(Me.hWnd, -26) ' GCL_STYLE
SetClassLongPtr Me.hWnd, -26, classLong Or CS_DROPSHADOW
End Sub
*** This code leverages the SetClassLongPtr function to add a shadow effect to the UserForm. ****
Tip:
Set your UserForm’s BackColor and Font properties in the VBA editor to match the modern look.
For a cleaner interface, adjust button colors and borders. You may also use custom icons or images to represent menu options. - WLLIANBrass Contributor
I try the way you say, but nothing happing. kkk
Or i'm doing wrong kkkI'm sorry, i only know basic of excel.
- Mks_1973Iron Contributor
Oh I see, in this case need to give the step by step guidance.
Please follow these:Open the VBA Editor:
In Excel, press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
In the VBA editor, locate your UserForm. If you haven’t created a UserForm yet:
Go to Insert > UserForm in the toolbar. This will create a blank UserForm.
Access the Code Module for the UserForm:Right-click on your UserForm (e.g., UserForm1) in the Project Explorer window on the left.
Choose View Code to open the code module where you’ll add the code.
Add the API Declarations:Copy the following lines of code and paste them at the very top of your UserForm code module. These declarations allow VBA to access specific Windows functions that help create rounded corners and shadows:
' API declarations for rounded corners and shadow effect
Private Declare PtrSafe Function CreateRoundRectRgn Lib "gdi32" ( _
ByVal nLeftRect As Long, ByVal nTopRect As Long, _
ByVal nRightRect As Long, ByVal nBottomRect As Long, _
ByVal nWidthEllipse As Long, ByVal nHeightEllipse As Long) As LongPrivate Declare PtrSafe Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As LongPrivate Declare PtrSafe Function SetClassLongPtr Lib "user32" Alias "SetClassLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtrPrivate Const CS_DROPSHADOW As Long = &H20000
Copy the following code and paste it below the declarations. This code will run when the UserForm is initialized (i.e., when it opens), setting rounded corners and shadow effects:Private Sub UserForm_Initialize()
Dim hRgn As Long
Dim radius As Long
Dim classLong As LongPtrradius = 20
hRgn = CreateRoundRectRgn(0, 0, Me.Width * Screen.TwipsPerPixelX, _
Me.Height * Screen.TwipsPerPixelY, radius, radius)
SetWindowRgn Me.hWnd, hRgn, True' Set shadow effect
classLong = GetClassLongPtr(Me.hWnd, -26) ' -26 is GCL_STYLE
SetClassLongPtr Me.hWnd, -26, classLong Or CS_DROPSHADOW
End Sub
radius = 20 (You can change 20 to a higher)
Please ensure the UserForm_Initialize code is in the same UserForm module.
Below is the full UserForm code, you can bypass above and directly paste the whole code from below:
Private Declare PtrSafe Function CreateRoundRectRgn Lib "gdi32" ( _
ByVal nLeftRect As Long, ByVal nTopRect As Long, _
ByVal nRightRect As Long, ByVal nBottomRect As Long, _
ByVal nWidthEllipse As Long, ByVal nHeightEllipse As Long) As LongPrivate Declare PtrSafe Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As LongPrivate Declare PtrSafe Function SetClassLongPtr Lib "user32" Alias "SetClassLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtrPrivate Const CS_DROPSHADOW As Long = &H20000
Private Sub UserForm_Initialize()
Dim hRgn As Long
Dim radius As Long
Dim classLong As LongPtr
radius = 20
hRgn = CreateRoundRectRgn(0, 0, Me.Width * Screen.TwipsPerPixelX, _
Me.Height * Screen.TwipsPerPixelY, radius, radius)
SetWindowRgn Me.hWnd, hRgn, True' Set shadow effect
classLong = GetClassLongPtr(Me.hWnd, -26)
SetClassLongPtr Me.hWnd, -26, classLong Or CS_DROPSHADOW
End Sub
I hope by doing this you will succeed. INSHA ALLAH- WLLIANBrass Contributor
error