VBA Excel UserForm Maximize Minimized Size Drag Control

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I got the following code to maximize, minimize and resize the form, but it didn't work well, every time I ran it, I got a popup msg "Sub or Function not defined" and highlight "GetWindowLong"

Note: I have 64bit system, so I added "PtrSafe" to the "Private Declare Function" but keep getting the same error above

Please, help me fixing this issue and let me know if I can add all this code to a module and just Call it from each form, because I have a lot of forms and lot of codes on my project, and don't need to add it to all of them

Many thanks in advanced


[TABLE="width: 800"]
<tbody>[TR]
[TD]Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindow Lib "user32" Alias "GetWindowA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindow Lib "user32" Alias "SetWindowA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE As Long = (-16)
Private Const WS_THICKFRAME As Long = &H40000
Const MIN_BOX As Long = &H20000
Const MAC_BOX As Long = &H10000
Private Declare PtrSafe Function DrawMenuBar Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare PtrSafe Function GetForegroundWindow Lib "user32.dll" () As Long
Public Sub AddToForm(ByVal Box_Type As Long)
Dim BisMask As Long
Dim Window_Handle As Long
Dim WindowStyle As Long
Dim Ret As Long
If Box_Type = MIN_BOX Or Box_Type = MAX_BOX Then
Window_Handle = GetForegroundWindow()
WindowStyle = GetWindowLong(Window_Handle, GWL_STYLE)
BisMask = WindowStyle Or Box_Type
Ret = SetWindowLong(Window_Handle, GWL_STYLE, BisMask)
Ret = DrawMenuBar(Window_Handle)
End If
End Sub


Private Sub UserForm_Activate()
Call AddToForm(MIN_BOX)
Call AddToForm(MAX_BOX)
End Sub


Sub Resize()
Dim hWndForm As Long
Dim istyle As Long
If Val(Application.Version) < 9 Then
hWndForm = FindWindow("ThunderXFrame", Me.Caption)
Else
hWndForm = FindWindow("ThunderDFrame", Me.Caption)
End If
istyle = GetWindowLong(hWndForm, GWL_STYLE)
istyle = istyle Or WS_THICKFRAME
Call SetWindowLong(hWndForm, GWL_STYLE, istyle)

End Sub


Private Sub UserForm_Initialize()
Call Resize
End Sub


[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi



The code you attach goes #
Code:
Attach the original 32-bit version, the changes must always work in both versions
 
Upvote 0
Thanks Jaafar,

This code works, but the form stays at the same size and there is no Maximize and Minimize buttons, so I found another code that works 100% as I need.
But there is just one problem which is the code is too big and I have many codes in each form
1- Do you have any way to put the code in one module and just call it from each form?
2- Another thing, (in each form) if I put all the controls that I need in a frame, can I edit this code to align it in the center of the frame? without scaling it?

Here is the new code that in a frame and 2 modules
https://drive.google.com/file/d/1vp8DgkQYTMCGkXSqkqSJlS2i2bjfuWn4/view?usp=sharing
 
Upvote 0
I'm curious why you need such a large UserForm that you need to resize.

Have you ever used a UserForm Multipage?

A Multipage is just like a Worksheet.

You can have one small UserForm with as many Multipage Pages as you want.

Think of it like The UserForm is a Workbook and Multipage Pages are like Workbook sheets.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top