Form.BorderStyle 属性为0时,Form_GotFocus 和 Form_LostFocus 没有反应,怎么解决?

tlz 2003-12-25 11:25:20
'这两部分都没有反应,窗体没有标题栏,该怎样达到效果呀?

Private Sub Form_GotFocus()
'颜色变灰
Label1.ForeColor = &H8000000F
End Sub
'--------------------------------------------

Private Sub Form_LostFocus()
'颜色变黑
Label1.ForeColor = &H0&
End Sub
...全文
88 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
workup 2003-12-25
用API写回调函数,截获鼠标信息。
SETWINDOWLONG 和一个回调函数。
回复
jhzhou882 2003-12-25
写在别的事件里
回复
tlz 2003-12-25
那怎么办?
回复
flc 2003-12-25
Form.BorderStyle 属性为0时,
不能够响应Form_GotFocus 和 Form_LostFocus 事件。
回复
LanceJin 2003-12-25
可以啊
回复
flc 2003-12-25
关注
回复
踏平扶桑 2003-12-25

'新建工程,两个窗体
Private Sub Form_GotFocus()
MsgBox "got"
End Sub

Private Sub Form_Load()
Form2.Show
End Sub

Private Sub Form_LostFocus()
MsgBox "lost"
End Sub
回复
rainstormmaster 2003-12-25
事实上,BorderStyle=0也同样可以触发Form_GotFocus()和Form_LostFocus()事件
回复
rainstormmaster 2003-12-25
设置BorderStyle属性为3,用SetWindowLong改变窗口风格:

两个窗体:窗体2没有任何代码,默认设置,用做检验,启动对象为form1

窗体一,一个标签,代码如下:

Option Explicit

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'获取窗体结构信息函数
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_SYSMENU = &H80000
Private Const WS_CAPTION = &HC00000
Private Const WS_SIZEBOX = &H40000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
'为窗体指定一个新位置和状态函数
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 SWP_NOZORDER = &H4
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_NOREPOSITION = &H200
'获得整个窗体的大小和位置
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type



Private Sub Form_Click()
Form2.Show
End Sub

Private Sub Form_Load()
Dim lStyle As Long
Dim MyRect As RECT
Dim Change As Boolean
'获取窗体的大小和位置
GetWindowRect Me.hwnd, MyRect
'取得当前窗体信息
lStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
lStyle = lStyle And Not WS_SYSMENU
lStyle = lStyle And Not WS_CAPTION
lStyle = lStyle And Not WS_SIZEBOX
lStyle = lStyle And Not WS_MAXIMIZEBOX
lStyle = lStyle And Not WS_MINIMIZEBOX
'按lStyle的值设置窗体信息
SetWindowLong Me.hwnd, GWL_STYLE, lStyle
'保持窗体的大小与位置不变
SetWindowPos Me.hwnd, 0, MyRect.Left, MyRect.Top, MyRect.Right - MyRect.Left, MyRect.Bottom - MyRect.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED

End Sub
Private Sub Form_GotFocus()
'颜色变蓝
Label1.ForeColor = vbBlue
End Sub
'--------------------------------------------

Private Sub Form_LostFocus()
'颜色变绿
Label1.ForeColor = vbGreen
End Sub


回复
tlz 2003-12-25
Form_Activate、Deactivate事件也不触发
回复
lix110 2003-12-25
可不可以写在Form_Activate、Deactivate 事件中来代替 :)
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告