Volume up or down with SendMessage in Windows

C++

hWnd = FindWindow("Shell_TrayWnd");

// volume up
SendMessage(hWnd, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP << 16);

// volume up - using only decimal values
SendMessage(hWnd,           793, 0,                     655360);

// volume down
SendMessage(hWnd, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP << 16);
SendMessage(hWnd,           793, 0,                     589824);

We use SendMessage to send a message to increase volume in Windows:

LRESULT WINAPI SendMessage(
  _In_  HWND hWnd,     - handler of our window or any handler
                         because is the handler of the window that
                         sends the message.
  _In_  UINT Msg,      - WM_APPCOMMAND which is defined
                         as 0x0319 (hex) in Windows so
                         we use 793 (decimal).
  _In_  WPARAM wParam, - 0
  _In_  LPARAM lParam  - message specific information derived from
                         APPCOMMAND_VOLUME_UP which is defined
                         as 10 (decimal) in Windows
 );

To get a handler we use FindWindow to get the Tray window:

HWND WINAPI FindWindow(
  _In_opt_  LPCTSTR lpClassName,
  _In_opt_  LPCTSTR lpWindowName
);

Useful definitions from here:

#define WM_APPCOMMAND   0x0319
APPCOMMAND_VOLUME_MUTE       8
APPCOMMAND_VOLUME_DOWN       9
APPCOMMAND_VOLUME_UP        10

Bitwise calculator here.

Lasă un răspuns

Adresa ta de email nu va fi publicată.