Im making a simple wallpaper changer. It works when changing the wallpaper but i cant change the pattern of the wallpaper. I tried something like this but it doesnt work :S
SystemParametersInfo(SPI_SETDESKPATTERN, 0, "Center",
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
Can some1 please show me the proper way of setting the wallpaper pattern?
I assume you mean the centred/ streched / tiled setting that would be the second past value int 1-3
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);
private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;
private void SetWallpaper(string path)
{
if (File.Exists(path))
{
Image imgInFile = Image.FromFile(path);
try
{
imgInFile.Save(SaveFile, ImageFormat.Bmp);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, SaveFile, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
catch
{
MessageBox.Show("error in setting the wallpaper");
}
finally
{
imgInFile.Dispose();
}
}
}
Related
First of all, please understand that I am not good at English so I am not good at choosing or write sentence or word.
I am having an issue with C# TopMost not working.
calling method is at Called C# Application From VB6.
according to each specific situation show different from each other Form.
All forms have the following code:
// Define
[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);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
// All forms have the following code:
SetWindowPos(f.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
Commom.User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
Commom.User32.SetForegroundWindow(h);
Commom.User32.ShowWindow(h, 1);
f.BringToFront();
f.TopLevel = true;
f.TopMost = true;
f.Focus();
// f is Form, h is Handle
Despite the above, it's TopMost not working.
I want you to help me if you know the problem or if you can help me.
Have a good day.
I try to change the background using C#.Example:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32
uiParam, String pvParam, UInt32 fWinIni);
private static UInt32 SPI_SETDESKWALLPAPER = 20;
private static UInt32 SPIF_UPDATEINIFILE = 0x1;
And then
SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, #"C:\background.bmp", SPIF_UPDATEINIFILE);
}
But it doesn't work...Help?
pvParam should be a local file. It will not work for urls...
First download the image, then give its local path to SystemParametersInfo method.
var filename = "4.jpg";
new WebClient().DownloadFile("http://www.scottgames.com/4.jpg", filename);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, filename, SPIF_UPDATEINIFILE);
I want to use SystemIcons.Warning but it is too big for my need. I want to resize it.
I have tried :
Icon sizedIcon = new Icon(SystemIcons.Warning, new Size(10,10));
But it does not work, icon remains the same.
Any suggestions?
The .NET Icon class is pretty crippled, it is stuck in the previous decade due to once-relevant support for Windows 98 and Windows 2000. On top of this, Windows does not support loading the system icons in any size other than the system's default icon size. Usually 32x32. Resizing it is going to inevitably look bad.
Do keep in mind that no icon is ever going to look good at 10x10. It is but a fleck on a modern monitor. You do get ahead a bit by starting with an icon size that's close to the desired final size, the less drastic the required resize, the higher the odds that it still looks reasonable. When you can, do favor sizes that are likely to be present in the icon. Like 16x16, 32x32, 48x48.
Anyhoo, this is fixable. Do keep in mind that this takes considerable hack-o-rama since Windows doesn't directly support this. What's required is reading the icon directly from the system DLL resources. And using a more modern winapi, LoadImage() instead of the one that .NET uses, LoadIcon(). Works on XP and up.
Add a new class to your project and paste this code:
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
public class IconEx : IDisposable {
public enum SystemIcons {
Application = 100,
Asterisk = 104,
Error = 103,
Exclamation = 101,
Hand = 103,
Information = 104,
Question = 102,
Shield = 106,
Warning = 101,
WinLogo = 100
}
public IconEx(string path, Size size) {
IntPtr hIcon = LoadImage(IntPtr.Zero, path, IMAGE_ICON, size.Width, size.Height, LR_LOADFROMFILE);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public IconEx(SystemIcons sysicon, Size size) {
IntPtr hUser = GetModuleHandle("user32");
IntPtr hIcon = LoadImage(hUser, (IntPtr)sysicon, IMAGE_ICON, size.Width, size.Height, 0);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public Icon Icon {
get { return this.icon; }
}
public void Dispose() {
if (icon != null) icon.Dispose();
}
private Icon icon;
private void attach(IntPtr hIcon) {
// Invoke the private constructor so we can get the Icon object to own the handle
var ci = typeof(Icon).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(IntPtr), typeof(bool) }, null);
this.icon = (Icon)ci.Invoke(new object[] { hIcon, true});
}
private const int IMAGE_ICON = 1;
private const int LR_LOADFROMFILE = 0x10;
private const int LR_SHARED = 0x8000;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, string lpszName, int uType,
int cxDesired, int cyDesired, int fuLoad);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, IntPtr resId, int uType,
int cxDesired, int cyDesired, int fuLoad);
}
Sample usage:
using (var icon = new IconEx(IconEx.SystemIcons.Warning, new Size(10, 10))) {
e.Graphics.DrawIcon(icon.Icon, 0, 0);
}
It does look better than SystemIcons.Warning. It's squeaky clean when you use 16x16.
On a WindowsCE platform (custom build) our C# gui uses regular forms to show an "popup menu".
We set the FormBorderstyle to None as we don't want the form controls to be visible.
Some clients reported "Gray boxes" after a while.
After some testing here we could reproduce the problem quite fast. When we open 2 different menu's (forms) constantly the platform shows us an native exception.
Error
A native exception has occurred
in Tiger.CEHost.exe. Select Quit and
then restart this program, or select
Details for more information.
The details:
Error
ExceptionCode: 0xC0000005
ExceptionAdress: 0x00000001
Reading: 0x00000001
at WL.SetSTyle(IntPtr hwnThis, UInt32 dwMask, UInt32 dwStyle)
at Form._SetBorderStyle(AGL_WINDOWSTYLE wstyVal, AGL_WINDOWSTYLE wstyMask)
at Form.set_FormBorderStyle(FormBorderStyle value)
at pDropDown.PopupForm.Show()
at pDropDown.Show()
at pButton.ShowHideDropDown()
at pButton.OnClick(EventArgs e)
at Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at EVL.EnterMainLoop(IntPtr hwnMain)
at Application.Run(Form fm)
at Program.Main(String[] args)
It always seems to fail at the FormBorderStyle property. We've already tried to remove all the pInvokes as perhaps some memory was overwritten, but this didn't help.
We also log each call to the Show method and each call is made in the gui thread and the Form contains a valid handle.
I've never seen this, which tends to make me think that it's less likely to be a problem in the CF or even your app.
Does your device have enough memory to run the app? A low-memory condition should throw an OOM, but I've seen it do other, less predicatble things, so it's always the first thing to check.
If memory is not the issue, are you certain that it's not a platform problem? Remember, since a large portion of the OS is developed by the OEM, you can't rule out problems in the OS.
I'd try two things:
Does the same app run fine on some other hardware (even the emulator) without problems? If it works on other hardware, it heavily implicates the platform as the problem.
Since it's fairly easy to repro with a small app in C#, I'd recommend building an app in C/C++ that does the same functional items to see if it behaves or gives the same issue.
It seems to be a bug in netcfagl3_5.dll (will notify Microsoft about this)
When we set the FormBorderstyle using pinvokes (SetWindowLong) we can't reproduce the problem.
In case someone experiences this rare bug, this is the code to set the formborderstyle without using the .net FormBorderStyle property.
private const uint WS_OVERLAPPED = 0x00000000;
private const uint WS_POPUP = 0x80000000;
private const uint WS_CHILD = 0x40000000;
private const uint WS_MINIMIZE = 0x20000000;
private const uint WS_VISIBLE = 0x10000000;
private const uint WS_DISABLED = 0x08000000;
private const uint WS_CLIPSIBLINGS = 0x04000000;
private const uint WS_CLIPCHILDREN = 0x02000000;
private const uint WS_MAXIMIZE = 0x01000000;
private const uint WS_CAPTION = 0x00C00000;
private const uint WS_BORDER = 0x00800000;
private const uint WS_DLGFRAME = 0x00400000;
private const uint WS_VSCROLL = 0x00200000;
private const uint WS_HSCROLL = 0x00100000;
private const uint WS_SYSMENU = 0x00080000;
private const uint WS_THICKFRAME = 0x00040000;
private const uint WS_GROUP = 0x00020000;
private const uint WS_TABSTOP = 0x00010000;
private const int WS_MINIMIZEBOX = 0x00020000;
private const int WS_MAXIMIZEBOX = 0x00010000;
private const uint WS_EX_DLGMODALFRAME = 0x00000001;
private const uint WS_EX_NOPARENTNOTIFY = 0x00000004;
private const uint WS_EX_TOPMOST = 0x00000008;
private const uint WS_EX_ACCEPTFILES = 0x00000010;
private const uint WS_EX_TRANSPARENT = 0x00000020;
private const uint WS_EX_MDICHILD = 0x00000040;
private const uint WS_EX_TOOLWINDOW = 0x00000080;
private const uint WS_EX_WINDOWEDGE = 0x00000100;
private const uint WS_EX_CLIENTEDGE = 0x00000200;
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_EX_STATICEDGE = 0x00020000;
private const int WS_EX_NOANIMATION = 0x04000000;
public const int GWL_EX_STYLE = -20;
public const int GWL_STYLE = (-16);
public static void SetNoBorder(Form form) {
RemoveFormStyle(form, GWL_STYLE, (int)(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU));
RemoveFormStyle(form, GWL_EX_STYLE, (int)(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
}
public static void RemoveFormStyle(Form f, int modifier, int style) {
int currStyle = GetWindowLong(f.Handle, GWL_EX_STYLE);
currStyle &= ~style;
SetWindowLong(f.Handle, modifier, currStyle);
}
[DllImport("Coredll.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
Its possible remove a window console title bar using c# and windows api, if yes howto? Please.
This simple app hides and shows the title bar of the console that it's in. It changes the console title to a guid momentarily to find the window handle. Afterwards it uses ToggleTitleBar to show or hide using the found handle.
public class Program
{
public static void ToggleTitleBar(long hwnd, bool showTitle)
{
long style = GetWindowLong(hwnd, -16L);
if (showTitle)
style |= 0xc00000L;
else
style &= -12582913L;
SetWindowLong(hwnd, -16L, style);
SetWindowPos(hwnd, 0L, 0L, 0L, 0L, 0L, 0x27L);
}
public static void Main()
{
Guid guid = Guid.NewGuid();
string oldTitle = Console.Title;
Console.Title = guid.ToString();
int hwnd = FindWindow("ConsoleWindowClass", guid.ToString());
Console.Title = oldTitle;
Console.WriteLine("Press enter to hide title");
Console.ReadLine();
ToggleTitleBar(hwnd, false);
Console.WriteLine("Press enter to show title");
Console.ReadLine();
ToggleTitleBar(hwnd, true);
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
[DllImport("user32", EntryPoint = "GetWindowLongA")]
public static extern long GetWindowLong(long hwnd, long nIndex);
[DllImport("user32", EntryPoint = "SetWindowLongA")]
public static extern long SetWindowLong(long hwnd, long nIndex, long dwNewLong);
[DllImport("user32")]
public static extern long SetWindowPos(long hwnd, long hWndInsertAfter, long x, long y, long cx, long cy,
long wFlags);
[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
}
Edit: Sorry, I see that you're looking for a solution for a console application. No, there is no way I know of to do what you're trying to do. It is also not simple to host a console in a WinForms application.
However, if you ARE using a WinForms application or WPF, consider the following.
this.ControlBox = false;
this.Text = string.Empty;
Otherwise, you could set FormBorderStyle to None.
You can also hide the program from the task bar if you need to.
this.ShowInTaskBar = false;
This will probably not work. In theory you could use something like this:
HWND handle = FindWindow(L"ConsoleWindowClass", NULL);
LONG style = GetWindowLong(handle, GWL_STYLE);
style = style & ~WS_CAPTION;
SetWindowLong(handle, GWL_STYLE, style);
This will work for every window except console windows. SetWindowLong returns 0, and GetLastError returns 5 (Access denied), even if you run the application as administrator.
I have some (very) old code I think somehow related; I'd to display Microsoft Excel inside a winform application:
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SetParent(int hWndChild, int hWndNewParent);
[DllImport("user32.dll")]
public static extern int MoveWindow(
int hWnd, int x, int y,
int nWidth, int nHeight, int bRepaint);
//
private static int hwnExcel = 0;
private System.Windows.Forms.PictureBox picContainer;
// ...
private void Principal_Resize(object sender, EventArgs e)
{
picContainer.Width = this.Width - 8;
picContainer.Height = this.Height - 45;
User32.SetParent(hwnExcel, 0);
User32.MoveWindow(
hwnExcel, 0, 0,
picContainer.Width, picContainer.Height, 1);
}
use FindWindow to get the handle of the console window then SetWindowLong to modify his properties