你的问题描述的不清楚,其实直接说明你想干什么或许能得到更好的帮助。实际上,向特定窗体发送键值没有多大意义。通常不需要这样做。但还是按要求给你写了个窗体间传递控制参数的小例子,不知是否能帮到你。运行该例程,点击按钮【打开受控窗体】后,通过方向键可以控制【Form2】或【PictureBox】上下左右移动。
方法:
1 创建【Form1】和【Form2】两个窗体
2 Form1 上添加 文本框,下拉框,按钮各一个
3 Form2 上添加一个PictureBox 控件
把下列代码粘贴到【Form1】
Dim B As Boolean
Private Sub Combo1_Click()
If B = True Then Text1.SetFocus '单击后把焦点转移到[Text1]
End Sub
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Activate()
B = True
End Sub
Private Sub Form_GotFocus()
Text1.SetFocus '窗体得到焦点后把焦点转移到[Text1]
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'=======================================================================
'功能: 利用方向键【←】【↑】【→】【↓】控制指定窗体或窗体中的控件
'=======================================================================
If Combo1.Text = "移动窗体" Then
Select Case KeyCode
Case 37
Form2.Command("向左1") = 20 ‘向Form传送操控指令和移动距离
Case 38
Form2.Command("向上1") = 20
Case 39
Form2.Command("向右1") = 20
Case 40
Form2.Command("向下1") = 20
End Select
Else
Select Case KeyCode
Case 37
Form2.Command("向左2") = 20
Case 38
Form2.Command("向上2") = 20
Case 39
Form2.Command("向右2") = 20
Case 40
Form2.Command("向下2") = 20
End Select
End If
End Sub
Private Sub Form_Load()
B = False
Me.KeyPreview = True '窗体先接收键盘事件,然后是活动控件接收事件
Command1.Caption = "打开受控窗体"
Text1.TabIndex = 0 '设置默认的焦点
Combo1.TabStop = False '表示当用户按下【tab】键时,跨越下拉框
Combo1.AddItem "移动窗体"
Combo1.AddItem "移动控件"
Combo1.Text = "移动窗体"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Unload Form2
End Sub
把下列代码粘贴到【Form2】
Public Property Let Command(ByVal CommandName As String, ByVal PYl As Long)
'=================================================================
'功能: 接受来自控制窗体的指令,移动指定对象
'参数: CommandName 为指令,PYl为偏移量
'=================================================================
Dim S As String
S = Trim(CommandName)
Select Case S
'''''''''''''''''''''''''''''''''''''''''移动窗体指令
Case "向上1"
Me.Move Me.Left, Me.Top - PYl
Case "向下1"
Me.Move Me.Left, Me.Top + PYl
Case "向左1"
Me.Move Me.Left - PYl, Me.Top
Case "向右1"
Me.Move Me.Left + PYl, Me.Top
'''''''''''''''''''''''''''''''''''''''''''移动窗体中的控件
Case "向上2"
Picture1.Move Picture1.Left, Picture1.Top - PYl
Case "向下2"
Picture1.Move Picture1.Left, Picture1.Top + PYl
Case "向左2"
Picture1.Move Picture1.Left - PYl, Picture1.Top
Case "向右2"
Picture1.Move Picture1.Left + PYl, Picture1.Top
End Select
End Property
Private Sub Form_Activate()
Form1.Text1.SetFocus '释放焦点,把焦点设置到控制窗体或它上面的某个控件
End Sub
希望上述代码对你有所帮助。