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

如题:我想设计一个程序,但是它运行的时候不能手动改变大小,但是能最小化,不能最大化。用FormLoad代码实现这个过程。Form里面那个BorderStyle属性设置了不能改变窗口大小,但是最小化就没有了。还有用Resize方法使窗口不变,但是运行的时候最大化最小化仍旧要出错…………实在没法了啊!!

请高手解答…………
MaxiumBox 和MiniumBox是什么属性?、属性窗口里面没有啊(只有MaxButton,MinButton)!!它们和MaxButton,MinButton一样吗?、
最新回答
野稚

2024-10-16 18:26:03

解决办法分三步:

一、窗体属性设置: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!
月影依风

2024-10-16 21:01:09

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

2024-10-16 12:12:06

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

2024-10-16 10:59:10

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
孤独儛埗

2024-10-16 14:23:28

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

2024-10-16 22:52:38

Private Sub Form_Resize()过程来解决!