你是否知道VB.NET借助API方法

本人很喜欢VB.NET借助API,在工作中也很喜欢总结关于VB.NET借助API的经验教训,在特殊窗体的应用中,我们有时需要把窗体右上角标题栏上的关闭按钮屏幕,当用户点击其它地方(比如说一个Button)退出,下面就这个问题来详细说说吧。

我们还是来看一下在VB6中的实现,VB6中实现(借助API函数)

PrivateDeclareFunction GetSystemMenu Lib "user32" (ByVal hwnd AsLong, ByVal bRevert AsLong) AsLong
PrivateDeclareFunction GetMenuItemCount Lib "user32" (ByVal hMenu AsLong) AsLong
PrivateDeclareFunction DrawMenuBar Lib "user32" (ByVal hwnd AsLong) AsLong
PrivateDeclareFunction RemoveMenu Lib "user32" (ByVal hMenu AsLong, ByVal nPosition AsLong, ByVal wFlags AsLong) AsLong
Const MF_BYPOSITION = &H400&
Const MF_REMOVE = &H1000&
PrivateSub Form_Load()
Dim hSysMenu AsLong, nCnt AsLong
'Get handle to our form's system menu
'(Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
'Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
'Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE 'Remove the seperator
DrawMenuBar(Me.hwnd)
'Force caption bar's refresh. Disabling X button
Me.Caption = "Try to close me!"
EndIf
EndIf
EndSub

'如果还要屏蔽Alt+F4,加上
PrivateSub Form_QueryUnload(ByVal Cancel AsInteger, ByVal UnloadMode AsInteger)
Cancel = 1
EndSub

VB.NET借助API,因为系统没有提供这样的类,这个例子,同时给大家提供了一个API的使用范例。(因为系统类库包装了绝大部分API,所以不推荐使用)

api

相关推荐