Forum Discussion
BORDERS USERFORM
I try the way you say, but nothing happing. kkk
Or i'm doing wrong kkk
I'm sorry, i only know basic of excel.
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 Long
Private Declare PtrSafe Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare PtrSafe Function SetClassLongPtr Lib "user32" Alias "SetClassLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
Private 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 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) ' -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 Long
Private Declare PtrSafe Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare PtrSafe Function SetClassLongPtr Lib "user32" Alias "SetClassLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
Private 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
- WLLIANNov 12, 2024Brass Contributor
error
- Mks_1973Nov 12, 2024Iron Contributor
You need to add some extra code to retrieve the hWnd handle for the UserForm.
At the top of your UserForm code module, add an additional declaration to retrieve the handle (hWnd) for the UserForm.
Private Declare PtrSafe Function FindWindowA Lib "user32" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
Also Modify the UserForm_Initialize event to get the hWnd using FindWindowA:Private Sub UserForm_Initialize()
Dim hRgn As Long
Dim radius As Long
Dim classLong As LongPtr
Dim hWnd As LongPtr' Get the hWnd handle for the UserForm
hWnd = FindWindowA(vbNullString, Me.Caption)
If hWnd = 0 Then
MsgBox "Unable to retrieve hWnd for UserForm.", vbCritical
Exit Sub
End If
radius = 20
hRgn = CreateRoundRectRgn(0, 0, Me.Width * Screen.TwipsPerPixelX, _
Me.Height * Screen.TwipsPerPixelY, radius, radius)
SetWindowRgn hWnd, hRgn, True
classLong = GetClassLongPtr(hWnd, -26)
SetClassLongPtr hWnd, -26, classLong Or CS_DROPSHADOW
End Sub
================================
FindWindowA: This function retrieves the hWnd handle by searching for the window title (Me.Caption). Make sure that the Caption of the UserForm is unique so that FindWindowA can find it.
Error Check: If hWnd is not found (returns 0), the code displays a message and exits to avoid further errors.
Replace Me.hWnd with hWnd: Now that hWnd is retrieved, you can use it in SetWindowRgn and SetClassLongPtr functions
I hope this workaround will works because it finds the window by its title (caption). Be sure the caption is not duplicated by other forms, as that may cause conflicts, try running your UserForm again. This should resolve the hWnd issue and allow the code to apply rounded corners and shadow effects without errors.- WLLIANNov 12, 2024Brass Contributor
Did not work.
Nevermind this project.
Looks too much complicated.
Thank you for your time.
Thanks for trying.