📜 ⬆️ ⬇️

When you go out, turn off the lights (and turn off the music). Computer lock and mixer

Probably, with all this was - you start to be interested in a particular topic, and when you search for the right material, you come across another interesting material, etc. And you start to jump on the links from one site to another. It happened recently with me - I was looking for examples for the WM_APPCOMMAND message, and in addition I learned that the .NET Framework has classes for working with user sessions.

It all started with a simple. I found in my archives an example with the Windows message WM_APPCOMMAND and decided to include it with my reference books on the Windows API functions for C # / VB and Visual Basic 6.0 . When processing the material for a new article, I decided to google what other examples there are on a given topic. And I found an interesting example that caught my attention.

On the well-known portal CodeProject found the article CodeProject: How to mute the system volume after system lock. Free source code and programming help . Here's what an article looks like in a free translation (further from the first person):

I work, so I'm in one good company. And I have a computer with large speakers. It is easier for me to work when loud music is pouring from the speakers, so I cut the sound to full power. And in front of me sits my colleague, who does not really like music. And I occasionally go out for air (i.e. smoke, but it is between us). And we have strict procedures in the company - if you leave the workplace for a while, then you must block the computer. And it is right. But there is one problem - when the computer is locked, the sound is not blocked and the music continues to play. My colleague asked that at least for the time of my absence, I turned off the sound. I'm not a beast, of course, turn off. But manually doing it is lazy, and then I wrote a utility that automatically turns off the mixer when it is locked and turns it on again when unlocked.
')
The description of the utility ends here and the author of the article lists the application code. It turns out that in the .NET Framework there is a SystemEvents.SessionSwitch event that allows you to track the lock and unlock of the computer. The code is very simple.

private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
//If the reason for the session switch is lock or unlock
//send the message to mute or unmute the system volume
if (e.Reason == SessionSwitchReason. SessionLock )
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
else if (e.Reason == SessionSwitchReason. SessionUnlock )
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
}


Honestly, I did not know about this event. But I was surprised by the orders in the company. In our office, those people who are accustomed to working to music, put on headphones and do not interfere with others. I can hardly imagine a situation for someone to use the speakers. And you?

By the way, the idea of ​​turning off the mixer seemed to me to be wrong. In the WM_APPCOMMAND message there is such a parameter as APPCOMMAND_MEDIA_PLAY_PAUSE , which allows you to pause the player. True, it works for Windows Media Player and hardly for Winamp and similar players. But if you listen to music in the universal player Windows, then take note.

By the way, about the WM_APPCOMMAND message. With the help of the constant APPCOMMAND_DWM_FLIP3D, you can call FLIP 3D mode from your program, which is used in Windows 7. By the way, sales of Windows 7 started today (calmly, I know that this news is pretty much borne by many of you).

PS A brief description of the WM_APPCOMMAND message can also be read on my blog .
Good luck with programming!

Source: https://habr.com/ru/post/73135/


All Articles