如何通过keydown从文件加载图像?

时间:2016-01-09 16:25:54

标签: vb.net

我正在尝试制作一个超级基本程序,每次单击F4时都会弹出一个图片和声音。我将程序的背景设置为绿色,因为我将使用它作为图片的绿色屏幕。我对VB没有多少经验,但由于我无法在网上找到这样做的程序,所以我决定采取摆动并尝试自己制作。 (失败了......)无论如何,这是我到目前为止所得到的。

Public Class Form1
Private Sub Form1_KeyPress(KeyAscii As Integer)
    If (Chr(KeyAscii) = "115") Then Form1.Picture = loadpicture("directory")
End Sub
End Class

注意:“目录”不是我在loadpicture()中的内容。

2 个答案:

答案 0 :(得分:1)

试试这个:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
End Sub

答案 1 :(得分:0)

这是最终代码,其中还包括按下按键时播放的音频剪辑!

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
    If e.KeyCode = Keys.F4 Then My.Computer.Audio.Play("C:\audio.wav", AudioPlayMode.Background)
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Nothing
End Sub
End Class