VB中如何使程序运行时窗口不能通过拖动改变大小,但是能最小化?

2024-11-21 21:54:18
推荐回答(6个)
回答1:

解决办法分三步:

一、窗体属性设置:BorderStyle=2,MaxButton=False

二、在窗体代码区复制下面代码:
Option Explicit

Private Sub Form_Load()
lockform Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unlockform
End Sub

三、建立一个模块,复制下面代码
Option Explicit
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam 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 Type PointAPI
X As Long
Y As Long
End Type
Private Type MINMAXINFO
ptReserved As PointAPI
ptMaxSize As PointAPI
ptMaxPosition As PointAPI
ptMinTrackSize As PointAPI
ptMaxTrackSize As PointAPI
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)

Private Const WM_GETMINMAXINFO = &H24
Private Const GWL_WNDPROC = (-4)

Dim OldProc&
Dim mhWnd&, mkd As Long, mgd As Long

' 锁定窗体边界
Sub lockform(nhWnd As Form) '锁定

If OldProc <> 0 Then Exit Sub

mhWnd& = nhWnd.hwnd
mgd = nhWnd.Height / Screen.TwipsPerPixelY
mkd = nhWnd.Width / Screen.TwipsPerPixelX
OldProc = SetWindowLong(mhWnd&, GWL_WNDPROC, AddressOf WinProc)

End Sub

Sub Unlockform() '解除锁定

If OldProc = 0 Then Exit Sub

SetWindowLong mhWnd, GWL_WNDPROC, OldProc
OldProc = 0
End Sub

Function WinProc&(ByVal hwnd&, ByVal wMsg&, _
ByVal wParam&, ByVal lParam&)

Select Case wMsg&
Case WM_GETMINMAXINFO
Dim MinMax As MINMAXINFO

CopyMemory MinMax, ByVal lParam, Len(MinMax)

MinMax.ptMinTrackSize.X = mkd
MinMax.ptMinTrackSize.Y = mgd
MinMax.ptMaxTrackSize.X = mkd
MinMax.ptMaxTrackSize.Y = mgd

CopyMemory ByVal lParam, MinMax, Len(MinMax)

WinProc& = 1
Exit Function
End Select

WinProc& = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Function

四、运行程序,OK!

回答2:

他说的就是,只不过那个max是maxium的缩写罢了。就是最大化和最小化可操作性的属性设置,如果他们的值为False,那就意味着最大化和最小化是不允许的,反之如果值为true则可以在运行期间使窗体可大可小。

回答3:

将窗体的 FormBordStyle 设置为任意 Fix 开头的,然后将 MaxiumBox 设置为 False,保留 MiniumBox 为 True

回答4:

Dim w As Integer, h As Integer '窗口初始宽度和高度用于保持窗口尺寸不变

Private Sub Form_Load()

w = Me.Width '保存窗口初始宽度
h = Me.Height '保存窗口初始高度
End Sub

Private Sub Form_Resize()
If Me.WindowState = 0 Then '只有窗口正常显示(非最大化最小化)时才进行设置
Me.Width = w
Me.Height = h
End If
End Sub

回答5:

把form窗口的borderstyle属性值设为3就可以

回答6:

Private Sub Form_Resize()过程来解决!