WPF VB.NET WM_APPCOMMAND

WPF VB.NET WM_APPCOMMAND,wpf,vb.net,wndproc,Wpf,Vb.net,Wndproc,我正在将我的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_LP

我正在将我的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#转换过来,因此可能不正确。(这会使我的应用程序崩溃)


我是在正确的轨道上还是偏离了方向?

您的窗口程序错误:

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
参数是一个
整数
,而不是字符串。这应该会给你一个编译时错误,所以我不知道你说它会破坏你的应用程序是什么意思

您需要Windows头文件来查找
WM_APPCOMMAND
消息的ID,或者文档中有时会给出它们。在这种情况下。该值为
&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

那么你想在WPF应用程序中覆盖WndProc吗?是的,除非有更简单的方法捕获多媒体密钥。可能的副本我已经看过那篇文章了,我真的不明白。另外,这篇文章是用C#而不是vb.netIf写的。如果是这样,那么我建议你明确询问你不理解的内容,而不是问“是否有办法让[这个]在WPF中工作”,尤其是在没有任何证据表明你自己的努力的情况下。我花了大约15秒的时间在谷歌上搜索“wpf wndproc”,找到了那个页面和其他一些你可能感兴趣的页面。您对C#/VB.NET语法差异是否有问题(“我发现了这段C#代码,但如何在VB.NET中做同样的事情?”)?它是关于代码如何工作的(“这个C代码做什么?”)?还是别的?非常感谢,它工作得很好。我只需要添加
Dim cmd As Integer=CInt(CUInt(lParam)>>16而不是&HF000)
,它的工作原理与以前一样。
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
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