vb api制作总在最前的窗体

类别:.NET开发 点击:0 评论:0 推荐:

第一步使用VB打开工程如图

第二步在工程中窗体中加入check控件
第三步:添加模块1,在其中使用外界程序中的API浏览器添加如下代码
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
'以上为API函数声明
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_FRAMECHANGED = &H20
'The frame changed: send WM_NCCALCSIZE
Public Const SWP_DRAWFRAME = SWP_FRAMECHANGED
'以上为程序中用到的常量

Public Const HWND_BOTTOM = 1
Public Const HWND_BROADCAST = &HFFFF&
Public Const HWND_DESKTOP = 0
Public Const HWND_NOTOPMOST = -2
Public Const HWND_TOP = 0
Public Const SWP_HIDEWINDOW = &H80
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_NOCOPYBITS = &H100
Public Const SWP_NOOWNERZORDER = &H200
'Don't do owner Z ordering
Public Const SWP_NOREDRAW = &H8
Public Const SWP_NOREPOSITION = SWP_NOOWNERZORDER
Public Const SWP_NOZORDER = &H4
Public Const SWP_SHOWWINDOW = &H40
'以上常量声明在程序中没有使用
'可以试着在调用SetWindowPos函数时使用这些常量或它们的组合
'得到其他效果
Public Const Flags = SWP_DRAWFRAME Or SWP_NOMOVE Or SWP_NOSIZE
第四步 然后在check1控件中添加如下的代码
Private Sub Check1_Click()
 Dim res As Long
 If Check1.Value = 1 Then
    res = SetWindowPos(Me.hwnd, HWND_TOPMOST, _
                        0, 0, 0, 0, Flags)
 Else
    res = SetWindowPos(Me.hwnd, HWND_NOTOPMOST, _
                        0, 0, 0, 0, Flags)

 End If
End Sub
用于控制窗体是否总是在前.
第五步:可运行,看你的运气是否不错.

本文地址:http://com.8s8s.com/it/it42690.htm