Disabling Screen Saver and Power Options in C# - c#

I am writing an application in C# that plays a movie. I need to figure out how to disable the screen saver and power options using C#.
I know the Windows SDK API has a function called SetThreadExecutionState() which can be used to do this, however, I do not know if there is a better way to do it. If not, how do I incorporate this function into C#?

Not sure if there is a better .NET solution but here is how you could use that API:
The required usings:
using System.Runtime.InteropServices;
The P/Invoke:
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_DISPLAY_REQUIRED = 0x00000002;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint SetThreadExecutionState([In] uint esFlags);
And then disable screensaver by:
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
Finnaly enable screensaver by reseting the execution state back to original value:
SetThreadExecutionState(ES_CONTINUOUS);
Note that I just picked one of the flags at random in my example. You'd need to combine the correct flags to get the specific behavior you desire. You will find the description of flags on MSDN.

Related

Can you get/set the System Sounds volume or Windows OS master volume via Powershell?

It's Win10 or 11 and I've seen the below for increasing/decreasing the volume 2% each time it's called, however I'd like to get/set the System Sounds volume, or the Device (master) Volume. Is that possible in Powershell?
const uint WM_APPCOMMAND = 0x319;
const uint APPCOMMAND_VOLUME_UP = 0xA0000;
const uint APPCOMMAND_VOLUME_DOWN = 0x90000;
const uint APPCOMMAND_VOLUME_MUTE = 0x80000;
Win32.Win32.SendMessage(handy, WM_APPCOMMAND, handy, (IntPtr)APPCOMMAND_VOLUME_UP);
Win32.Win32.SendMessage(handy, WM_APPCOMMAND, handy, (IntPtr)APPCOMMAND_VOLUME_DOWN);
The AudioDeviceCmdlets module in powershell that can be used to manage sound devices and volume level.
And you could also try to use Core Audio APIs to control the system volume. And then you could access the [Audio]::Volume property from PowerShell.
I suggest you could refer to the threads:
https://stackoverflow.com/a/19348221/11872808
https://stackoverflow.com/a/31751275/11872808

How to using sndPlaySound function to play "empty the recycle bin" sound by using It's alias?

For some reasons, I want to play a system sound which is named "EmptyRecycleBin" in my program when a file has been deleted.
So I wrote these code:
Code1
[DllImport("winmm.dll", EntryPoint = "sndPlaySound")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean SndPlaySound(String pszSound, Int32 fuSound);
private const Int32 SND_SYSTEM = 0x00200000;
SndPlaySound("EmptyRecycleBin", SND_SYSTEM ); //The sound that was played is wrong, but I don't know why……
sndPlaySound function executes successfully, but unfortunately, the sound that was played is "Beep".
According to #AlexK. 's explain, sndPlaySound function can't identify the alias "EmptyRecycleBin".
In this case, I have to do this in another way:
Code2
var sound = new SoundPlayer(Registry.CurrentUser.OpenSubKey(#"AppEvents\Schemes\Apps\Explorer\EmptyRecycleBin\.Current", false).GetValue(null) as String);
sound.PlaySync();
It's all ok, but I still want to know that if it's possible to make Code1 work by using any other alias?
My system is windows 10, any help would be appreciated.

Change system default language programmatically with c#

We have a virtual keyboard (for touch screen) which its language layout is configured via the windows default language.
I have seen numerous answers which involves InputLanguageManager and CultureInfo.
They're not useful to me, didn't do the job.
There is this one method - SystemParametersInfo function with the SPI_SETDEFAULTINPUTLANG flag that i'm trying to check.
So far, didn't find any useful usage examples besides this one here, but it changes the keyboard layout from Dvorak to Marshal.
Can you give me an example (hopefully with the SystemParametersInfo) that converts the default system language to en-US?
Edit
A brief clarification.
This program replaces the explorer as windows shell, hence all keyboard settings such as setting default keyboard layout should be handled from my program.
Moreover, my wish is to replace between different installed languages such as English, Swedish, Portuguese and so on..
I don't want to change between Dvorak and Qwerty layout of the keyboard.
The purpose of this post is to ask for examples for changing between different languages and not for layout of English symbols on keyboard.
Thanks!
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni);
public void Foo()
{
uint localeUS = 0x00000409;
uint localeNL = 0x00000403;
SetSystemDefaultInputLanguage(localeUS);
}
public bool SetSystemDefaultInputLanguage(uint locale)
{
return SystemParametersInfo(SPI_SETDEFAULTINPUTLANG, 0, ref locale, 0);
}
public uint GetSystemDefaultInputLanguage()
{
uint result = uint.MinValue;
bool retVal = SystemParametersInfo(SPI_GETDEFAULTINPUTLANG, 0, ref result, 0);
return result;
}
This seems to work fine for me.
Sources:
SystemParametersInfo API
SystemParametersInfo Parameter Definition
Input Locales List MSDN

Changing windows settings with a script

I have a problem that every once in a while the amount of lines or w\e that moves when I turn the mouse wheel goes from 3 to 30 and my brother sometimes sees it changes to 100.
What I'm talking about is when you go to "Control Panel" -> "Mouse" -> and then the mouse-wheel tab, it has 2 number-scrollers, and I need to change the value of the first one with a script or .exe or whatever way you know how to change it with.
For whoever might encounter that problem and wants the solution, here's the code:
[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);
static void Main(string[] args)
{
const int SPI_SETWHEELSCROLLLINES = 0x0069;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDCHANGE = 0x02;
SystemParametersInfo(SPI_SETWHEELSCROLLLINES, 3, IntPtr.Zero,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
Thanks for helping!
System parameters are not to be changed via registry keys, because (1) that's an implementation detail, that may change in future versions of Windows or even depending on other user profile settings and (2) because you are updating just the saved value, not the one that is currently active.
The correct way to go is to use the SystemParametersInfo API specifying the correct parameter constant (in your case, SPI_SETWHEELSCROLLLINES) and SPIF_UPDATEINIFILE | SPIF_SENDCHANGE as last parameter to both activate it right now and save it for the next sessions.
With regedit you can setup a script that sets the mouse wheeel scrolling speed, property is here:
HKEY_CURRENT_USER\Control Panel\Desktop\WheelScrollLines
just browse regedit (win+R regedit) look at the WeelScrollLines value, then create a reg file as specified by microsoft : https://support.microsoft.com/en-us/kb/310516#bookmark-syntax

How to 'Refresh' the Vista start menu programmatically

I am working on a piece of code that removes an extra folder we have in the user's start menu. I start by removing all of the shortcuts it contains, and then remove the folder itself.
After this is done, I can confirm that the shortcuts have been removed from the start menu, but their containing folder remains listed in the start menu. So, I checked the file system for such a folder and found none. Suspecting that this is some sort of refresh problem, I logged my user out and back into Vista and found that the folder was now removed from the start menu list.
How utterly annoying... Does anyone know how to programmatically force a 'refresh' of the Vista start menu, so that the user doesn't see this empty folder before they log out?
Thanks,
-Ben
I tried to implement this myself but it did not work as expected using SendMessageTimeout.
Instead, it worked when I used
SHGetSpecialFolderLocation(CSIDL_STARTMENU)
SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidl, NULL);
See this article for sample c++ code:
http://support.microsoft.com/kb/q193293/
Tested on Windows Server 2008 Enterprise (x86) with SP1.
This article seems to have the answer you're looking for:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/ce540c7d-a113-4f39-956e-0af6bc91abd3/
The answer given is:
class Program
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessageTimeout ( IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult );
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
private const int WM_SETTINGCHANGE = 0x1a;
private const int SMTO_ABORTIFHUNG = 0x0002;
static void Main ( string[] args )
{
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
}
}

Categories

Resources