Change Windows Wallpaper using .NET 4.0? - c#

Is there a way to change the Windows wallpaper using some new feature in .NET 4?

You can use SystemParametersInfo to set the desktop wallpaper. This should work consistently on all versions of windows that your app can run on, however will require some interop.
The following interop declarations are what you need
public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_UPDATEINIFILE = 1;
public const int SPIF_SENDCHANGE = 2;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int SystemParametersInfo(
int uAction, int uParam, string lpvParam, int fuWinIni);
Which can be used like this to change the desktop wallpaper
SystemParametersInfo(
SPI_SETDESKWALLPAPER, 0, "filename.bmp",
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

You set the wallpaper by updating the registry. Here's an article from 2006 explaining how to do it. The details may have changed with newer versions of Windows, but the concept should be the same. Framework version should be irrelevant.
http://blogs.msdn.com/coding4fun/archive/2006/10/31/912569.aspx

Note that SystemParametersInfo will even return true, if the specified file does not exist! (on Windows 8 at least)
Plus you must give the full path to the file, not just a relative path.
Also on windows 7 and above this will create a new theme, and will turn off picture shuffling of course.

Related

Preventing Lock screen from Console Application [duplicate]

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.

Top most even for Modern Apps

I have a recording program that stays TopMost all the time except when I open a Modern app (Windows 8) or the Start Screen.
It is possible to make a desktop application stay on top of modern apps, like the Magnifying Glass tool:
Now, the problem is that using the TopMost option and/or the API call in a WPF window won't work with modern apps.
What I'm trying:
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//OnLoaded event handler:
var source = PresentationSource.FromVisual(this) as HwndSource;
SetWindowPos(source.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
Only applications marked as Accessibility related can do this. To achieve it, follow these guidelines (taken from the comments section of this article):
The application must demand uiAccess (app.manifest)
The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property,
programmatically or otherwise)
Without making changes to the group policy setting, it must be installed to some trusted location [C:\Windows, C:\Program Files,
C:\Program Files (x86)]. a. Note: If you want to be able to run it out
of an arbitrary location, you must disable the security setting: “User
Account Control: Only elevate UIAccess applications that are installed
in secure locations”. b. Note2: This is the same as setting
HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\ValidateAdminCodeSignatures
to 0
Said application cannot be ran in the debugger
If it’s a .NET application a. The manifest must be embedded in a post-build step b. The application must have “delayed signing”
(meaning it cannot be ran from the built-in debugger, although you can
build and attach – this is what Microsoft does)
The application must be signed with a trusted certificate.
Said trusted certificate must be installed to the Trusted Root Certificate Authority (this is important! It must not just simply
installed) For more info see:
http://msdn.microsoft.com/en-us/library/ms726294
... Not really a trivial task!

Webcam snapp-shot without showing Camera Window c# Vista, Windows 7

Hi all am creating an message application but as soon as the app snatch a picture from my webcam it starts showing a window to choice a camera know its only one camera installed
[DllImport("user32", EntryPoint = "SendMessage")]
static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle,
int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
const int WM_CAP_CONNECT = 1034;
const int WM_CAP_DISCONNECT = 1035;
const int WM_CAP_COPY = 1054;
const int WM_CAP_GET_FRAME = 1084;
This api works on windows xp with a service application so i created service app which pipe communicate with the appliaction and transfer the picture in bytes but note it only works on windows xp
how can i catch a webcam picture without this window am using some api in windows vista,7,8
Thanks B.
The API you are referring to (Video for Windows) is not supposed to operate without a window. The window however does not need to be visible, and you can move it out of work area too. It does not have to paint actually captured video as well, the window purpose is to communicate with the API. So it is usable in Window 7 too, provided of course that you have a well operating driver for these operating systems as well.
Other APIs you might want to look at are DirectShow and Media Foundation. DirectShow is the richest and best compatible throughout versions of Windows API, and you can do the task mentioned in the subject (capture without showing). From C# you typically work with it via DirectShow.NET library/binding and it has decent samples to look at on the website.

Interface to change volume and play sound for HDA Audio device under Windows CE 6.0?

I develop C# .Net CF applications for a Win CE device, and am having problems getting the speaker volume change to affect actual volume when playing sound.
The interface I use is:
int waveOutSetVolume(IntPtr hMod, UInt32 dwVolume);
int PlaySound(string szSound, IntPtr hMod, int flags);
The code I use worked well with our old device which had the following setup:
AC'97 Audio codec, Windows CE 5, .Net CF 2.0.
However, on the new device the sound is played but I'm not able to change the volume. The setup is as follows:
HDA Audio codec, Windows CE 6, .Net CF 3.5.
I am uncertain whether this problem is within the Windows CE 6 OS image (e.g. missing/incorrect audio driver), or if I use the incorrect interface in my C# code.
Any help and ideas are most welcome!
Thanks,
Karl
Additional details:
Code:
public unsafe int SetVolume(int newVolumeInPercent)
{
UInt32 newVol = (UInt32)((double)(newVolumeInPercent * ushort.MaxValue) / 100.0);
newVol = newVol + (newVol << 16);
int resultSetVolume = waveOutSetVolume(IntPtr.Zero, newVol);
return (int)Math.Round((double)resultSetVolume * 100 / ushort.MaxValue);
}
public void playSound(string soundFile)
{
PlaySound(soundFile, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
}
[DllImport("CoreDll.dll")]
private extern static int waveOutSetVolume(IntPtr hMod, UInt32 dwVolume);
[DllImport("CoreDll.dll", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int PlaySound(string szSound, IntPtr hMod, int flags);
private enum Flags
{
SND_ASYNC = 0x0001,
SND_FILENAME = 0x00020000,
}
As you see in the code, I use the argument percentage volume for both the left and right channel.
Using Windows CE Remote Process Viewer, I can see that the audio driver (i.e. "jwavehda.dll") is loaded. Also the "waveapi.dll" (generic Window wave api?) is loaded.
I do get sound when tapping the screen, and using the "PlaySound" function I am able to play a wave file. The only problem is that I cannot affect the volume.
Thanks!
I'd need to see your calling code (and your p/invoke declaration here are incomplete as well) to be sure. Are you aware that the waveOutSetVolume dwVolume is split into two words, the upper work being the left channel volume and the lower work being the right channel volume? The value you're sending in might be affecting the behavior (you didn't show that part of your code).
The fact that the code worked on one platform but fails on another indicates to me that it's likely an OS/Platform issue. Do you get audio for things like screen taps or other system events? Do you have an audio control panel applet? Did you look for an audio driver in the registry to make sure it both exists in the OS and is also loaded?

Controlling the volume while using the SndPlayAsync function on Windows Mobile 6

How on earth can you control the volume of the sound played using SndPlayAsync on Windows Mobile 6??
It seems like no one knows! The documentation doesn't mention anything regarding it...
So either there's no way, or it is kept top secret...
In addition, I am aware of the possibility of using the Windows Media Player, but I rather not, if possible.
Thanks for any help!
Aviv.
My suggestion is:
[DllImport("coredll.dll", SetLastError = true)]
protected static extern int waveOutSetVolume(IntPtr device, uint volume);
[DllImport("coredll.dll", SetLastError = true)]
internal static extern int waveOutGetVolume(IntPtr device, ref int volume);
And then you can call methods:
int before;
uint maxVol = uint.MaxValue;
waveOutGetVolume(IntPtr.Zero, ref before);
waveOutSetVolume(IntPtr.Zero, maxVol);
//Do some playing
waveOutSetVolume(IntPtr.Zero, before);
You can debug for other values. This will set it to highest.
Hope it helps?
You need to use the mixer... API functions to set the master volume. Here is a code sample:
http://www.csharp-home.com/index/tiki-read_article.php?articleId=134
To use this code in your Windows Mobile application, you need to change "winmm.dll" to "coredll.dll". Also, these methods may not be supported in Windows Mobile, but I'm pretty sure they are.

Categories