#t#本人很喜欢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,所以不推荐使用)
以下是VB.NET的代码:
- 'API声明
- PrivateDeclareFunction GetSystemMenu Lib "User32" (ByVal hwnd AsInteger, ByVal bRevert AsLong) AsInteger
- PrivateDeclareFunction RemoveMenu Lib "User32" (ByVal hMenu AsInteger, ByVal nPosition AsInteger, ByVal wFlags AsInteger) AsInteger
- PrivateDeclareFunction DrawMenuBar Lib "User32" (ByVal hwnd AsInteger) AsInteger
- PrivateDeclareFunction GetMenuItemCount Lib "User32" (ByVal hMenu AsInteger) AsInteger
- PrivateConst MF_BYPOSITION = &H400&
- PrivateConst MF_DISABLED = &H2&
- PrivateSub disableX(ByVal wnd As Form)
- Dim hMenu AsInteger, nCount AsInteger
- '得到系统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)
- EndSub
- PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
- '使用X不能用
- disableX(Me)
- EndSub
- PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- '关闭窗口
- Me.Close()
- EndSub
- '如果还要屏蔽Alt+F4,加上
- ProtectedOverridesSub WndProc(ByRef m As System.Windows.Forms.Message)
- Dim SC_CLOSE AsInteger = 61536
- Dim WM_SYSCOMMAND AsInteger = 274
- '判断是系统消息,是不是关闭窗体,使Alt+F4无效
- If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
- ExitSub
- EndIf
- MyBase.WndProc(m)
- EndSub
新闻名称:你是否知道VB.NET借助API方法
网站路径:http://www.shufengxianlan.com/qtweb/news25/385275.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联