Ir al contenido


Foto

Controlar el volumen del PC desde WinXP hasta Win8 y 2


  • Por favor identifícate para responder
2 respuestas en este tema

#1 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 17 enero 2014 - 02:56

Hace unos meses publicaba una unit, WinMasterVolumen, para controlar el volumen del PC con WinXP, Vista, Win7 y Win8. En esta ocasión y basada en el mismo código presento la segunda versión que añade la característica de disparar un evento cuando el volumen o el mute del sistema varían, bien por acción del usuario o por código.

El sistema callback es diferente, de nuevo, en WinXP y común en adelante. En XP se basa en un mensaje MM_MIXM_CONTROL_CHANGE. En principio es fácil de usar pero requiere un bucle de mensajes para que funcione y una ventana.

En Vista, Win7 y Win8 tenemos que echar mano de interfaces de windows y el evento es una llamada a una función (OnNotify) que usaremos para realizar el proceso.

Para conseguir que el código funcione en todos los sistemas operativos he escrito esta nueva unit que presenta una clase TMasterVolume que hará el trabajo. Esta clase tiene los siguientes funciones y precedimientos públicos:
 
 


delphi
  1.     constructor Create;
  2.     destructor  Destroy; override;
  3.     procedure SetNotifyEvent(OnNotify: TOnNotify);
  4.     procedure SetChangeStatusEvent(OnChangeStatus: TOnChangeStatus);
  5.     procedure SetChangeStatusCallBack(OnChangeStatus: TOnChangeStatusCallBack);
  6.     function  GetMasterMute: boolean;
  7.     procedure SetMasterMute(Value: boolean);
  8.     function  GetMasterVolume: DWORD;
  9.     procedure SetMasterVolume(Value: DWORD);

 
Los eventos pueden ser de estos tipos:
 


delphi
  1. type
  2. TOnNotify = function(pNotify: PAUDIO_VOLUME_NOTIFICATION_DATA): HRESULT; stdcall;
  3. POnNotify = ^TOnNotify;
  4.  
  5. TOnChangeStatus = procedure(Volume: DWORD; Mute: boolean) of object;
  6. TOnChangeStatusCallBack = procedure(Volume: DWORD; Mute: boolean); stdcall;

SetNotifyEvent Sólo válido para vista en adelante, asigna un evento similar al usado por windows, TOnNotify . Al usar valores en coma flotante he añadido otros dos que usan DWORD siendo mas compatible en todos los S.O.
SetChangeStatusEvent Asigna un evento tipo TOnChangeStatus compatible con la VCL, es decir, será una función de nuestro Form, por ejemplo.
SetChangeStatusCallBack Asigna un evento tipo TOnChangeStatusCallBack, no compatible con VCL y diseñado para uso en programas API puros.

El resto de las funciones son equivalentes a WinMasterVolumen.

El siguiente es un ejemplo se uso usando la VCL, un formulario con un TrackBar y un CheckBox:


delphi
  1. procedure TForm1.OnChangeStatus(MasterVolume: DWORD; Muted: boolean);
  2. begin
  3.   CheckBox1.Checked:= Muted;
  4.   TrackBar1.Position:= MasterVolume;
  5. end;
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9.   Master:= TMasterVolume.Create;
  10.   Master.SetChangeStatusEvent(OnChangeStatus);
  11.   CheckBox1.Checked:= Master.GetMasterMute;
  12.   TrackBar1.Position:= Master.GetMasterVolume;
  13. end;
  14.  
  15. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  16. begin
  17.   Master.Free;
  18. end;
  19.  
  20. procedure TForm1.TrackBar1Change(Sender: TObject);
  21. begin
  22.   Master.SetMasterVolume(TrackBar1.Position);
  23. end;
  24.  
  25. procedure TForm1.CheckBox1Click(Sender: TObject);
  26. begin
  27.   Master.SetMasterMute(CheckBox1.Checked);
  28. end;

Un ejemplo de uso con API pura en WindowsXP:
 


delphi
  1. uses
  2.   Windows, Messages, WinMasterVolumeClass;
  3.  
  4. var
  5.   Msg: TMsg;
  6.   Master: TMasterVolume;
  7.  
  8. procedure OnChangeStatus(Volume: DWORD; Mute: boolean); stdcall;
  9. begin
  10.   // Mantenes el volumen al máximo y con mute desactivado
  11.   Master.SetMasterVolume($FFFF);
  12.   Master.SetMasterMute(false);
  13. end;
  14.  
  15. begin
  16.   Master:= TMasterVolume.Create;
  17.   Master.SetMasterVolume($FFFF);
  18.   Master.SetMasterMute(false);
  19.   Master.SetChangeStatusCallBack(@OnChangeStatus);
  20.  
  21.   // bucle de mensajes
  22.   repeat GetMessage(Msg, 0, 0, 0);
  23.     DispatchMessage(Msg);
  24.   until (Msg.Message = WM_QUIT);
  25. end.

El mismo código en Vista, Win7 y Win8. No precisa bucle de mensajes:
 


delphi
  1. uses
  2.   Windows, Messages, WinMasterVolumeClass;
  3.  
  4. var
  5.   Msg: TMsg;
  6.   Master: TMasterVolume;
  7.  
  8. procedure OnChangeStatus(Volume: DWORD; Mute: boolean); stdcall;
  9. begin
  10.   Master.SetMasterVolume($FFFF);
  11.   Master.SetMasterMute(false);
  12. end;
  13.  
  14. begin
  15.   Master:= TMasterVolume.Create;
  16.   Master.SetMasterVolume($FFFF);
  17.   Master.SetMasterMute(false);
  18.   Master.SetChangeStatusCallBack(@OnChangeStatus);
  19.   repeat sleep(200); until false;
  20. end.

Aunque podríamos dar más funcionalidad a esta unit, de momento la dejo como está. Espero que os sea útil.


Saludos.

Archivos adjuntos


  • 0

#2 KlausGunther

KlausGunther

    Newbie

  • Miembros
  • Pip
  • 1 mensajes

Escrito 09 diciembre 2017 - 09:25

Hello,

 

Sorry about that, but I don't speak Spanish. English, German, French or Portugese, but not Spanish. so, please, be indulgent with my english comment.

 

This message has initially be posted as a reply to the following publication:

http://delphiaccess....hasta-win8-y-2/

My reply has appearently be isolated to a separate message stream, not longer connected to the original massage. I dont know why. But I give the original URL because otherwise, my reply would not be comprehensive.

 

I'm the author of a huge support DLL (more then 1000 functions) written in Delphi 6 Personal Edition, designed for use by a freeware BASIC clone named Panoramic. Recently, I came to temper with the volume controls of Windows, and after some coding and research on the web, I stumbled upon this piece of software. I downloaded it and tested it, under Windows 10,Fall creator update installed. It just worked right out of the box ! Very nice piece of softare !

 

So, I decided to include it into my DLL and build my specific functions arround it. A few hours of work, and it was fuly operational, and I just published it into the (small) forum of Panoramic users. I granted you full award for this software in the CHM documentation, as well as in the online documentation for my DLL. I hope you will be enjoyed by this application of your software.

 

Here is the link to the corrsponding online documentation page:

http://klauspanorami...MasterVolume_fr

Don't hesitate to navigate to the licence page - you will find a link to this website !

 

Here is a link to my personal website:

https://klauspanoram...webhostapp.com/

 

Here is the link to the French forum (my pseudo is Klaus):

http://panoramic.free-boards.net/

 

Finaly, here is the link to Panoramic's home page:

http://panoramic-lan...ench/index.html

 

So, congratulations and thanks for the publication of this component !


  • 0

#3 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 11 diciembre 2017 - 12:25

Muchas gracias a ti por haberte interesado en el tema.

 

Saludos.


  • 0




IP.Board spam blocked by CleanTalk.