C ++为什么Sendmessage不起作用?

时间:2013-04-19 17:27:15

标签: c++ winapi handle sendmessage hwnd

在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 );
}

2 个答案:

答案 0 :(得分:5)

您需要使用MAKELPARAM宏将APPCOMMAND_VOLUME_UP传递给SendMessage MAKELPARAM(0,APPCOMMAND_VOLUME_UP) 最终版本应该看起来像

SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, MAKELPARAM(0,APPCOMMAND_VOLUME_UP));

答案 1 :(得分:1)

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

SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, APPCOMMAND_VOLUME_UP << 16);