End If
End If
End Sub
''如果还要屏蔽Alt+F4,加上
Private Sub Form_QueryUnload(ByVal Cancel As Integer, ByVal UnloadMode As Integer)
Cancel = 1
End Sub
在VB.NET中,这次需要借助API了,因为系统没有提供这样的类,这个例子,同时给大家提供了一个API的使用范例。(因为系统类库包装了绝大部分API,所以不推荐使用)
以下是VB.NET的代码:
''API声明
Private Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Integer, ByVal bRevert As Long) As Integer
Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Integer) As Integer
Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Integer) As Integer
Private Const MF_BYPOSITION = &H400&
Private Const MF_DISABLED = &H2&
Private Sub disableX(ByVal wnd As Form)
Dim hMenu As Integer, nCount As Integer
''得到系统Menu
hMenu = GetSystemMenu(wnd.Handle.ToInt32, 0)
''得到系统Menu的个数
nCount = GetMenuItemCount(hMenu)
''去除系统Menu
Call RemoveMenu(hMenu, nCount - 1, MF_BYPOSITION Or MF_DISABLED)
''重画MenuBar
DrawMenuBar(Me.Handle.ToInt32)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''使用X不能用
disableX(Me)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''关闭窗口
Me.Close()
End Sub
''如果还要屏蔽Alt+F4,加上
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim SC_CLOSE As Integer = 61536
Dim WM_SYSCOMMAND As Integer = 274
''判断是系统消息,是不是关闭窗体,使Alt+F4无效
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
Exit Sub
End If
MyBase.WndProc(m)
End Sub
5、无标题栏的窗体的拖动问题
在特殊窗体的应用中,我们有时需要把窗体的标题栏屏蔽掉,以窗体换上自己的外壳。是,当去掉了窗体标题栏后,移动窗体就成了一个问题。
我们还是来看一下在VB6中的实现,VB6中实现(借助API函数SendMessage)
在设计时将窗体的BorderStyle属性设置为0-none
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Declare Sub ReleaseCapture Lib "User32" ()
Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lngReturnValue As Long
If Button = 1 Then
''Release capture
Call ReleaseCapture()
''Send a ''left mouse button down on caption''-message to our form
lngReturnValue = SendMessage(Me.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
End Sub
Private Sub Form_Paint()
Me.Print("Click on the form, hold the mouse button and drag it")
End Sub
在VB.NET中,这次需要借助API SendMessage 了
在设计时将Form.FormBorderStyle 属性设置为None,然后添加以下代码:
Declare Function SendMessage Lib "user32" Alias "SendMess