'窗体 VERSION 5.00 Begin VB.Form Form1 Caption = "Form1" ClientHeight = 1650 ClientLeft = 60 ClientTop = 345 ClientWidth = 4350 LinkTopic = "Form1" ScaleHeight = 1650 ScaleWidth = 4350 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton Command1 Caption = "Popup" Height = 375 Left = 1440 TabIndex = 1 Top = 600 Width = 1455 End Begin VB.PictureBox Picture1 Appearance = 0 'Flat BackColor = &H80000005& ForeColor = &H80000008& Height = 2295 Left = 360 ScaleHeight = 2265 ScaleWidth = 1785 TabIndex = 0 Top = 1920 Visible = 0 'False Width = 1815 Begin VB.TextBox Text1 Height = 315 Left = 120 TabIndex = 2 Top = 120 Width = 1575 End End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option ExplicitPrivate Sub Form_Load() m_Hwnd = Me.hwnd End SubPrivate Sub Command1_Click() Dim rc As RECT Call Hook GetWindowRect Command1.hwnd, rc SetWindowLong Picture1.hwnd, GWL_STYLE, WS_POPUP Or WS_BORDER SetWindowLong Picture1.hwnd, GWL_EXSTYLE, WS_EX_TOPMOST Or WS_EX_TOOLWINDOW SetParent Picture1.hwnd, 0 Picture1.Move rc.Left * Screen.TwipsPerPixelX, (rc.Bottom + 2) * Screen.TwipsPerPixelY, Picture1.Width, Picture1.Height Picture1.Visible = True Call SetCapture(Picture1.hwnd) End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.Visible = False Call ReleaseCapture Call Unhook End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If X > Text1.Left And X < Text1.Left + Text1.Width _ And Y > Text1.Top And Y < Text1.Top + Text1.Height Then Call ReleaseCapture Call SetCapture(Text1.hwnd) Else Call SetCapture(Picture1.hwnd) End If End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If X < 0 Or X > Text1.Width Or Y < 0 Or Y > Text1.Height Then Call ReleaseCapture Call SetCapture(Picture1.hwnd) End If End Sub '模块 Option ExplicitPublic 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Public Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function ReleaseCapture Lib "user32" () As Long Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPublic Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePublic Type POINTAPI X As Long Y As Long End TypePublic Const GWL_EXSTYLE = (-20) Public Const GWL_STYLE = (-16) Public Const GWL_HWNDPARENT = (-8) Public Const GWL_WNDPROC = (-4)Public Const WS_THICKFRAME = &H40000 Public Const WS_VISIBLE = &H10000000 Public Const WS_POPUP = &H80000000 Public Const WS_BORDER = &H800000Public Const WS_EX_TOPMOST = &H8 Public Const WS_EX_TOOLWINDOW = &H80Public Const WM_NCACTIVATE = &H86 Public Const WM_ACTIVATEAPP = &H1CPublic m_Hwnd As Long Public m_Hook As Boolean Private m_PrevWndProc As LongPublic Sub Hook() If Not m_Hook Then m_PrevWndProc = SetWindowLong(m_Hwnd, GWL_WNDPROC, AddressOf WindowProc) m_Hook = True End If End SubPublic Sub Unhook() If m_Hook Then Call SetWindowLong(m_Hwnd, GWL_WNDPROC, m_PrevWndProc) m_Hook = False End If End SubPrivate Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case uMsg Case WM_NCACTIVATE WindowProc = CallWindowProc(m_PrevWndProc, hw, uMsg, 1, lParam)
Case Else WindowProc = CallWindowProc(m_PrevWndProc, hw, uMsg, wParam, lParam)
拼错了
商自尊
LostForce是什么东东??
是Lostfocus吧?但是焦点没有迁移的话是不触发的啊
有Deactivate事件吗
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1650
ClientLeft = 60
ClientTop = 345
ClientWidth = 4350
LinkTopic = "Form1"
ScaleHeight = 1650
ScaleWidth = 4350
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Popup"
Height = 375
Left = 1440
TabIndex = 1
Top = 600
Width = 1455
End
Begin VB.PictureBox Picture1
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 2295
Left = 360
ScaleHeight = 2265
ScaleWidth = 1785
TabIndex = 0
Top = 1920
Visible = 0 'False
Width = 1815
Begin VB.TextBox Text1
Height = 315
Left = 120
TabIndex = 2
Top = 120
Width = 1575
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub Form_Load()
m_Hwnd = Me.hwnd
End SubPrivate Sub Command1_Click()
Dim rc As RECT
Call Hook
GetWindowRect Command1.hwnd, rc
SetWindowLong Picture1.hwnd, GWL_STYLE, WS_POPUP Or WS_BORDER
SetWindowLong Picture1.hwnd, GWL_EXSTYLE, WS_EX_TOPMOST Or WS_EX_TOOLWINDOW
SetParent Picture1.hwnd, 0
Picture1.Move rc.Left * Screen.TwipsPerPixelX, (rc.Bottom + 2) * Screen.TwipsPerPixelY, Picture1.Width, Picture1.Height
Picture1.Visible = True
Call SetCapture(Picture1.hwnd)
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.Visible = False
Call ReleaseCapture
Call Unhook
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X > Text1.Left And X < Text1.Left + Text1.Width _
And Y > Text1.Top And Y < Text1.Top + Text1.Height Then
Call ReleaseCapture
Call SetCapture(Text1.hwnd)
Else
Call SetCapture(Picture1.hwnd)
End If
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X < 0 Or X > Text1.Width Or Y < 0 Or Y > Text1.Height Then
Call ReleaseCapture
Call SetCapture(Picture1.hwnd)
End If
End Sub
'模块
Option ExplicitPublic 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPublic Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Type POINTAPI
X As Long
Y As Long
End TypePublic Const GWL_EXSTYLE = (-20)
Public Const GWL_STYLE = (-16)
Public Const GWL_HWNDPARENT = (-8)
Public Const GWL_WNDPROC = (-4)Public Const WS_THICKFRAME = &H40000
Public Const WS_VISIBLE = &H10000000
Public Const WS_POPUP = &H80000000
Public Const WS_BORDER = &H800000Public Const WS_EX_TOPMOST = &H8
Public Const WS_EX_TOOLWINDOW = &H80Public Const WM_NCACTIVATE = &H86
Public Const WM_ACTIVATEAPP = &H1CPublic m_Hwnd As Long
Public m_Hook As Boolean
Private m_PrevWndProc As LongPublic Sub Hook()
If Not m_Hook Then
m_PrevWndProc = SetWindowLong(m_Hwnd, GWL_WNDPROC, AddressOf WindowProc)
m_Hook = True
End If
End SubPublic Sub Unhook()
If m_Hook Then
Call SetWindowLong(m_Hwnd, GWL_WNDPROC, m_PrevWndProc)
m_Hook = False
End If
End SubPrivate Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_NCACTIVATE
WindowProc = CallWindowProc(m_PrevWndProc, hw, uMsg, 1, lParam)
Case Else
WindowProc = CallWindowProc(m_PrevWndProc, hw, uMsg, wParam, lParam)
End Select
End Function
你的方法虽然很好,但是我做的控件是一个2级的combobox,下拉框部分感觉像
多级菜单,下拉部分的形状不是一个矩形。我原先想把usercontrol的BACKSTYLE属性设成透明的,但这样像你这样做就不能捕获鼠标信息了,
能不能帮帮我?
那么只要按zyl910的方法,把两个矩形都SetCapture到,然后在鼠标单击时判断是不是在两个矩形之外就可以了!