Private Sub Form_Load() frmMain.SetFocus End Sub这样会提示无效的过程或调用参数,但如果用一个command事件的话就不会了Private Sub command1_click() frmMain.SetFocus End Sub 下面一段是正确的
Private Sub Form_Activate() frmMain.SetFocusEnd Sub
To:如果一定要按照你的思路,注意下列问题:Private Sub Form_Load() frmMain.SetFocus End Sub加载的时候frmMain还没有显示,这个时候setfocus当然不行,你可以这样,在frmMain里面放置一个Timer,间隔设置为100毫秒,默认Enabled=false,在Form_Load()事件里面:timer1.enabled=true然后定义timer1_Timer()事件 Private Sub Timer1_Timer() command1_click() timer1.enabled=false End Sub应该就没有问题了!
你可以添加一个keydown到text控件上三,我觉得上面的方法应该都可以吧,你再看看。
//新建一个窗体,里面只有一个文本框,起初的时候,焦点落在文本框里,请问如何让焦点落在窗体上(可增加一个按扭)?那你就加一个按钮: Private Sub Form_Load() Me.KeyPreview = True Me.Show Command1.SetFocus End Sub
sub xx() Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1") If Not rs.EOF Then For i = 1 To rs.RecordCount Load xf(rs("id")) xf(rs("id")).Visible = True xf(rs("id")).Width = rs("t_width") xf(rs("id")).Height = rs("t_height") xf(rs("id")).Top = rs("t_top") xf(rs("id")).Left = rs("t_left") xf(rs("id")).Movie = App.Path & "\" & rs("t_filename") xf(rs("id")).Width = xf(rs("id")).Width - 10 xf(rs("id")).Width = xf(rs("id")).Width + 10 rs.MoveNext If rs.EOF Then Exit For Next End If end sub 我的窗体焦点还是得不到,如果去掉上面的过程就可以得到焦点,也就是说,form.keydown不起任何作用?
'屏蔽键盘 Dim hhkLowLevelKybd As Long '屏蔽鼠标 Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long '窗体最大化,并且在最前面 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_nosize = &H1 Private Const swp_nomove = &H2 Private Const hwnd_topmost = -1 Private Const hwnd_notopmost = -2 Private Sub Form_DblClick() frmMain.KeyPreview = True End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 115 Then ShowCursor 1 '恢复鼠标 '恢复热键 UnhookWindowsHookEx hhkLowLevelKybd hhkLowLevelKybd = 0 End ElseIf KeyCode = 116 Then ShowCursor 1 '恢复鼠标 '恢复热键 UnhookWindowsHookEx hhkLowLevelKybd hhkLowLevelKybd = 0 formset.Show End IfEnd SubPrivate Sub Form_Load() 'wm1.uiMode = "none" wm1.URL = App.Path & "/b.wmv" wm1.Controls.Play wm1.Visible = False'WindowsMediaPlayer1.uiMode = "none" '窗体最大化,并且在最前面 'SetWindowPos Me.hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove Or swp_nosize '屏蔽鼠标 'ShowCursor 0 '屏蔽热键 'hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)'调用数据库 OpenDB'调用flash x_flash Timer1.Enabled = True End Sub Sub x_flash()Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1") If Not rs.EOF Then For i = 1 To rs.RecordCount Load xf(rs("id")) xf(rs("id")).Visible = True xf(rs("id")).Width = rs("t_width") xf(rs("id")).Height = rs("t_height") xf(rs("id")).Top = rs("t_top") xf(rs("id")).Left = rs("t_left") xf(rs("id")).Movie = App.Path & "\" & rs("t_filename") xf(rs("id")).Width = xf(rs("id")).Width - 10 xf(rs("id")).Width = xf(rs("id")).Width + 10 rs.MoveNext If rs.EOF Then Exit For Next End IfEnd SubSub xf_reset()Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1") If Not rs.EOF Then For i = 1 To rs.RecordCount xf(rs("id")).Visible = True xf(rs("id")).Width = rs("t_width") xf(rs("id")).Height = rs("t_height") xf(rs("id")).Top = rs("t_top") xf(rs("id")).Left = rs("t_left") xf(rs("id")).Movie = App.Path & "\" & rs("t_filename") xf(rs("id")).Width = xf(rs("id")).Width - 10 xf(rs("id")).Width = xf(rs("id")).Width + 10 rs.MoveNext If rs.EOF Then Exit For Next End If End SubPrivate Sub Form_unload(Cancel As Integer) ShowCursor 1 '恢复鼠标 '恢复热键 UnhookWindowsHookEx hhkLowLevelKybd hhkLowLevelKybd = 0 End SubPrivate Sub Timer1_Timer() 'xf_reset frmMain.SetFocus frmMain.KeyPreview = True Timer1.Enabled = False End Sub
设置了这个属性就好了,不用管焦点在那里。都可以使form收到keydown
form1.setfocus
frmMain.SetFocus
End Sub这样会提示无效的过程或调用参数,但如果用一个command事件的话就不会了Private Sub command1_click()
frmMain.SetFocus
End Sub
下面一段是正确的
frmMain.SetFocusEnd Sub
frmMain.SetFocus
End Sub加载的时候frmMain还没有显示,这个时候setfocus当然不行,你可以这样,在frmMain里面放置一个Timer,间隔设置为100毫秒,默认Enabled=false,在Form_Load()事件里面:timer1.enabled=true然后定义timer1_Timer()事件
Private Sub Timer1_Timer()
command1_click()
timer1.enabled=false
End Sub应该就没有问题了!
Private Sub Form_Load()
Me.KeyPreview = True
Me.Show
Command1.SetFocus
End Sub
Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1")
If Not rs.EOF Then
For i = 1 To rs.RecordCount
Load xf(rs("id"))
xf(rs("id")).Visible = True
xf(rs("id")).Width = rs("t_width")
xf(rs("id")).Height = rs("t_height")
xf(rs("id")).Top = rs("t_top")
xf(rs("id")).Left = rs("t_left")
xf(rs("id")).Movie = App.Path & "\" & rs("t_filename")
xf(rs("id")).Width = xf(rs("id")).Width - 10
xf(rs("id")).Width = xf(rs("id")).Width + 10
rs.MoveNext
If rs.EOF Then Exit For
Next
End If
end sub
我的窗体焦点还是得不到,如果去掉上面的过程就可以得到焦点,也就是说,form.keydown不起任何作用?
[email protected]
Dim hhkLowLevelKybd As Long
'屏蔽鼠标
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
'窗体最大化,并且在最前面
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_nosize = &H1
Private Const swp_nomove = &H2
Private Const hwnd_topmost = -1
Private Const hwnd_notopmost = -2
Private Sub Form_DblClick()
frmMain.KeyPreview = True
End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 115 Then
ShowCursor 1 '恢复鼠标
'恢复热键
UnhookWindowsHookEx hhkLowLevelKybd
hhkLowLevelKybd = 0
End
ElseIf KeyCode = 116 Then
ShowCursor 1 '恢复鼠标
'恢复热键
UnhookWindowsHookEx hhkLowLevelKybd
hhkLowLevelKybd = 0
formset.Show
End IfEnd SubPrivate Sub Form_Load()
'wm1.uiMode = "none"
wm1.URL = App.Path & "/b.wmv"
wm1.Controls.Play
wm1.Visible = False'WindowsMediaPlayer1.uiMode = "none"
'窗体最大化,并且在最前面
'SetWindowPos Me.hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove Or swp_nosize
'屏蔽鼠标
'ShowCursor 0
'屏蔽热键
'hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)'调用数据库
OpenDB'调用flash
x_flash
Timer1.Enabled = True
End Sub
Sub x_flash()Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1")
If Not rs.EOF Then
For i = 1 To rs.RecordCount
Load xf(rs("id"))
xf(rs("id")).Visible = True
xf(rs("id")).Width = rs("t_width")
xf(rs("id")).Height = rs("t_height")
xf(rs("id")).Top = rs("t_top")
xf(rs("id")).Left = rs("t_left")
xf(rs("id")).Movie = App.Path & "\" & rs("t_filename")
xf(rs("id")).Width = xf(rs("id")).Width - 10
xf(rs("id")).Width = xf(rs("id")).Width + 10
rs.MoveNext
If rs.EOF Then Exit For
Next
End IfEnd SubSub xf_reset()Set rs = getDataset("select *from kj where x_type=1 and x_sffb=1")
If Not rs.EOF Then
For i = 1 To rs.RecordCount
xf(rs("id")).Visible = True
xf(rs("id")).Width = rs("t_width")
xf(rs("id")).Height = rs("t_height")
xf(rs("id")).Top = rs("t_top")
xf(rs("id")).Left = rs("t_left")
xf(rs("id")).Movie = App.Path & "\" & rs("t_filename")
xf(rs("id")).Width = xf(rs("id")).Width - 10
xf(rs("id")).Width = xf(rs("id")).Width + 10
rs.MoveNext
If rs.EOF Then Exit For
Next
End If
End SubPrivate Sub Form_unload(Cancel As Integer)
ShowCursor 1 '恢复鼠标
'恢复热键
UnhookWindowsHookEx hhkLowLevelKybd
hhkLowLevelKybd = 0
End SubPrivate Sub Timer1_Timer()
'xf_reset
frmMain.SetFocus
frmMain.KeyPreview = True
Timer1.Enabled = False
End Sub