Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Cancel = 1 Me.Hide End Sub这样也可以!
Private Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long Private Const MF_BYPOSITION = &H400& Private Const MF_DISABLED = &H2&Private Sub DisableX(Frm As Form) Dim hMenu As Long, nCount As Long hMenu = GetSystemMenu(Frm.hwnd, 0) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) DrawMenuBar Frm.hwnd End SubPrivate Sub Form_Load() Call DisableX(Me) End Sub或者这样
加一个模块 Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias _ "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _ As Long, ByVal dwNewLong As Long) As Long
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
Public Sub Init(hWnd As Long) PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc) End Sub
Public Sub Terminate(hWnd As Long) Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc) End Sub
Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) _ As Long
If Msg = WM_DESTROY Then Terminate (Form1.hWnd)
If Msg = WM_SYSCOMMAND Then If wParam = SC_MINIMIZE Then MsgBox "fdsa" Exit Function End If End If
SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam) End Function ----------------------------------------------------------------------- 窗口中的代码 Private Sub Form_Load() Call Init(Me.hWnd) End Sub
Private Sub Form_Unload(Cancel As Integer) Call Terminate(Me.hWnd) End Sub
Cancel = 1
Me.Hide
End Sub这样也可以!
Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Const MF_DISABLED = &H2&Private Sub DisableX(Frm As Form)
Dim hMenu As Long, nCount As Long
hMenu = GetSystemMenu(Frm.hwnd, 0)
nCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
DrawMenuBar Frm.hwnd
End SubPrivate Sub Form_Load()
Call DisableX(Me)
End Sub或者这样
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _
As Long, ByVal dwNewLong As Long) As Long
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
Const GWL_WNDPROC = (-4&)
Dim PrevWndProc&
Const WM_DESTROY = &H2
Const WM_SYSCOMMAND = &H112
Const SC_MINIMIZE = &HF020&
Public Sub Init(hWnd As Long)
PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc)
End Sub
Public Sub Terminate(hWnd As Long)
Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc)
End Sub
Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) _
As Long
If Msg = WM_DESTROY Then Terminate (Form1.hWnd)
If Msg = WM_SYSCOMMAND Then
If wParam = SC_MINIMIZE Then
MsgBox "fdsa"
Exit Function
End If
End If
SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)
End Function
-----------------------------------------------------------------------
窗口中的代码
Private Sub Form_Load()
Call Init(Me.hWnd)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Terminate(Me.hWnd)
End Sub