-2

在 C# 中,此 SendMessage 函数成功提高音量:

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

SendMessage(Handle, 0x319, (int)Handle, APPCOMMAND_VOLUME_UP);

但是在 C++ 中,这个 SendMessage 函数不起作用,即音量没有上升:

// HWND hwnd = CreateWindow(...
SetWindowText(hwnd, "Hi"); // Worked
SendMessage(hwnd, WM_APPCOMMAND, (int)hwnd, APPCOMMAND_VOLUME_UP); // Don't work

我错了什么?你能帮我解决一下吗?谢谢!

完整代码:

#include <windows.h>
HHOOK _hhook;
HWND hwnd;
HINSTANCE _hInstance;

KBDLLHOOKSTRUCT kbdStruct;

LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        if (wParam == WM_KEYDOWN)
        {
            // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
            kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
            // a key (non-system) is pressed.
            if (kbdStruct.vkCode == VK_F1)
            {
                // F1 is pressed!
                SetWindowText(hwnd, "Hi");
                SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, APPCOMMAND_VOLUME_UP);
                //MessageBox(NULL, "F1 is pressed!", "key pressed", MB_ICONINFORMATION);
            }
        }
    }
    return CallNextHookEx(_hhook, nCode, wParam, lParam);
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) 
{
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.hCursor = LoadCursor( NULL, IDC_HAND );
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = TEXT("Philip");
    wc.lpszMenuName = 0;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass( &wc );
    hwnd = CreateWindow(
        TEXT("Philip"),
        TEXT("Hello World!"),
        WS_OVERLAPPEDWINDOW,
        10, 10,
        500, 200,
        NULL, NULL,
        hInstance, NULL );

    ShowWindow(hwnd, iCmdShow );
    UpdateWindow(hwnd);

    _hhook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0);

    MSG msg;
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(   HWND hwnd,
                            UINT message,
                            WPARAM wparam,
                            LPARAM lparam )
{

    switch( message )
    {

    case WM_DESTROY:
        PostQuitMessage( 0 ) ;
        return 0;
        break;


    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}
4

2 回答 2

5

您需要使用MAKELPARAM宏传递APPCOMMAND_VOLUME_UP给 SendMessageMAKELPARAM(0,APPCOMMAND_VOLUME_UP) 最终版本应如下所示

SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, MAKELPARAM(0,APPCOMMAND_VOLUME_UP));
于 2013-04-19T19:04:02.913 回答
1

经过一番调查,我能够得到这个工作。问题源于传递了一个不正确的lParam值。命令本身位于值的高 16 位,但您没有考虑到这一点。以下更改应该让您上路:

SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, APPCOMMAND_VOLUME_UP << 16);
于 2013-04-19T19:00:01.027 回答