我正在将我的 winforms 项目转换为 WPF,并在我这样做的同时学习 WPF。
我遇到了这段代码的问题
此代码检测媒体键盘或媒体中心遥控器上按下的按钮。
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
我是在正确的轨道上还是我走错了路?