【90分】VB:如何FORM的置顶和禁止最小化
1.如何设置form的置顶,在所有窗口最前
2.禁止form的最小化和禁止通过Alt+F4关闭
3.禁止鼠标右键 --------------------编程问答-------------------- 1 SetWindowPos
2 SubClass
3 SubClass --------------------编程问答-------------------- 1
--------------------编程问答-------------------- 置顶的代码上面已给出了,禁止最小化,最简单的是在设计时指定窗口的MinButton属性为False。 --------------------编程问答-------------------- 我是LZ,补充说明一下:
'********************************* Methods *************************************
Public Sub DisplayAlwaysOnTop(ByRef hWnd As Long, _
ByVal OnTop As Boolean, _
Optional ByVal DisplayUnderMenu As Boolean = False)
'***************************************************************************
' PURPOSE: Sets the passed form to be always displayed on top
' ARGUMENTS: MyForm - Form object (passed by reference)
' OnTop - Boolean to set on top or NOT on top status
' CALLS: SetWindowPos (API)
' RETURNS: -
' USAGE: AlwaysOnTop Me, True ' To make always on top
' AlwaysOnTop Me, False ' To make NOT always on top
'***************************************************************************
Const dUnderMenu = 67
Dim lngUnderMenu As Long
Dim intResult As Integer
On Error GoTo ErrorHandler
If DisplayUnderMenu Then
lngUnderMenu = dUnderMenu
End If
If OnTop Then
'intResult = SetWindowPos(hWnd, HWND_TOPMOST, 0, lngUnderMenu, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
intResult = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
Else
'intResult = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, lngUnderMenu, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
intResult = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End If
ExitHandler:
Exit Sub
ErrorHandler:
Resume Next
End Sub
禁止最小化是指禁止通过WIN+D按键实现,3楼的方法不对头。 --------------------编程问答-------------------- 我说了,这是最简单的。
你说的不是禁止窗口最小化,是过滤掉系统热键,因为WIN+D不仅可以最小化,还可以最大化所有窗口。 --------------------编程问答-------------------- 至于过滤(或叫禁止)系统热键,网上有许多现成的代码,你GOOGLE一下。 --------------------编程问答-------------------- 呵呵,对于WIN+D,屏蔽太麻烦!
直接写个TIMER,让自己显示出来.
3.禁止鼠标右键
这个你主要指 TEXTBOX等控件的吧?
API搞定! --------------------编程问答--------------------
不响应WIN+D的条件:
一,置顶
二,窗体的taskvisable=false,即不显示在任务栏上.
--------------------编程问答-------------------- 1. 用SetWindowPos函数,
Private Declare Function SetWindowPos Lib "user32" ( _详见http://blog.163.com/wll_009/blog/static/117373117200941564847329/
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long _
) As Long
Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40
Private Sub Form_load()
Dim retValue As Long
'将窗体设置为处于所有窗口的顶层,注意在 VB 中运行时,
'可能不行,但编译成EXE后就可以了
retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, Me.CurrentX,
Me.CurrentY, 300, 300, SWP_SHOWWINDOW)
End Sub
2.设置 MinButton 为 false
3.只要你不设置右键菜单,右键又有何用呢? --------------------编程问答-------------------- 不对... ...,即使是 老马说的这两个条件成立,其实 窗口还是 响应了 WIN +D,因为他失去了焦点
WIN + D 的作用是使当前所有有显示窗体的进程都失去焦点并最小化到TaskBar
再次按下时,会使最后一个获取焦点(需要不符合那两点条件的窗口,否则不会获取焦点)的窗口重新获得焦点
最簡單實現樓主的需要:
1.如何设置form的置顶,在所有窗口最前
-->
Option Explicit
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST& = -1
' 將窗口置於列表頂部,並位於任何最頂部窗口的前面
Private Const SWP_NOSIZE& = &H1
' 保持窗口大小
Private Const SWP_NOMOVE& = &H2
' 保持窗口位置
Private Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
' 將窗口設為總在最前
End Sub
2.禁止form的最小化和禁止通过Alt+F4关闭
-->
'設置
Form.BorderStyle = 0
Form.ShowInTaskBar= False
Form.MinButton = False
3.禁止鼠标右键
-->如果你都沒寫鼠標右鍵事件,Form不會給你生成的,所以這個不是問題
--------------------编程问答--------------------
请问在webbrowser控件中要怎么禁用鼠标右键呢? --------------------编程问答--------------------
谢谢你的回答。
第二个问题没能解决额。。。还是不能禁止WIN+D
第三个问题,我需要在webbrowser中禁用右键。 --------------------编程问答-------------------- 新建工程,添加这个模块:
http://www.m5home.com/bbs/dispbbs.asp?boardid=27&Id=1707&page=1&star=1
然后把窗体ShowInTaskbar = False,代码里设置不了:
Option Explicit
Private Sub Form_Load()
' Me.ShowInTaskbar = False
OnTop Me
End Sub
看看行不行.
我的代码是测试后才放上来的.
除非因为我是2003的原因....... --------------------编程问答-------------------- 呵呵。这个问题我之前写过一个桌面程式。
这个就好比是桌面式程序。不最小化。不响应任何按键。拦截窗口函数。只是不贴在桌面。而是置顶。
需要GetWindowLong和setwindowslong。
'//模块部分
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public 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 Const WM_WINDOWPOSCHANGED = &H47
Public Const GWL_WNDPROC = (-4)
Public YProc As Long
Public Function WinProc(ByVal nhWnd As Long, ByVal wMsg As Long, ByVal wp As Long, ByVal lp As Long) As Long
If wMsg = WM_WINDOWPOSCHANGED Then
Form1.WindowState = 0
SetWindowPos Form1.hwnd, -1, 0, 0, 0, 0, 3
Else
WinProc = CallWindowProc(YProc, nhWnd, wMsg, wp, lp)
End If
End Function
'//窗体部分
Private Sub Form_Load()
YProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WinProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, YProc
End Sub
补充:VB , 基础类