我正在将我的winforms项目转换为WPF,并在我这样做时学习WPF。
我遇到了这段代码的问题
此代码检测媒体键盘或Media Center遥控器上按下的按钮。
Protected Overrides Sub WndProc(ByRef msg As Message)
If msg.Msg = &H319 Then
' WM_APPCOMMAND message
' extract cmd from LPARAM (as GET_APPCOMMAND_LPARAM macro does)
Dim cmd As Integer = CInt(CUInt(msg.LParam) >> 16 And Not &HF000)
Select Case cmd
Case 13
MessageBox.Show("Stop Button")
Exit Select
Case 47
MessageBox.Show("Pause Button")
Exit Select
Case 46
MessageBox.Show("Play Button")
Exit Select
End Select
End If
MyBase.WndProc(msg)
end sub
我想知道是否有办法让它在WPF中运行或者可能做类似的事情。
修改
我的最新尝试,我试图将其从C#转换为所以它可能是不正确的。 (这只会让我的应用崩溃)
Dim src As HwndSource = HwndSource.FromHwnd(New WindowInteropHelper(Me).Handle)
src.AddHook(New HwndSourceHook(AddressOf WndProc))
和
Public Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
'Do something here
If msg = "WM_APPCOMMAND" Then
MessageBox.Show("dd")
End If
Return IntPtr.Zero
End Function
我是在正确的轨道还是离开?
答案 0 :(得分:3)
您的窗口程序错误:
Public Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
'Do something here
If msg = "WM_APPCOMMAND" Then
MessageBox.Show("dd")
End If
Return IntPtr.Zero
End Function
请注意,msg
参数是Integer
,而不是字符串。这个应该给你一个编译时错误,所以我不知道你对你的应用程序崩溃的意思。
您需要Windows头文件才能找到WM_APPCOMMAND
消息的ID,或者有时会在文档中给出这些消息。在这种情况下,it is。值为&H0319
(以VB十六进制表示法)。
所以将代码更改为:
Private Const WM_APPCOMMAND As Integer = &H0319
Public Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
' Check if the message is one you want to handle
If msg = WM_APPCOMMAND Then
' Handle the message as desired
MessageBox.Show("dd")
' Indicate that you processed this message
handled = True
End If
Return IntPtr.Zero
End Function