Last active
August 13, 2024 22:36
-
-
Save javascripto/162c38d04d31b01316ca84c3ae8e93fa to your computer and use it in GitHub Desktop.
Set a fake wallpaper when user has no permission to set windows wallpaper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Drawing; | |
| using System.Windows.Forms; | |
| using System.Runtime.InteropServices; | |
| // Copile with: | |
| // C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe FakeWallpaper.cs | |
| // Compilar with icon file: | |
| // C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe /win32icon:FakeWallpaper.ico FakeWallpaper.cs | |
| public class FakeWallpaper : Form { | |
| private NotifyIcon notifyIcon; | |
| private ContextMenuStrip contextMenuStrip; | |
| [STAThread] | |
| public static void Main(string[] args) { | |
| Application.Run(new FakeWallpaper()); | |
| } | |
| public FakeWallpaper() { | |
| InitializeComponent(); | |
| this.Load += FakeWallpaper_Load; | |
| } | |
| private void InitializeComponent() { | |
| ToolStripMenuItem closeMenuItem = new ToolStripMenuItem("Close"); | |
| closeMenuItem.Click += (sender, e) => Application.Exit(); | |
| contextMenuStrip = new ContextMenuStrip(); | |
| contextMenuStrip.Items.Add(closeMenuItem); | |
| notifyIcon = new NotifyIcon() { | |
| Icon = SystemIcons.Application, | |
| Visible = true, | |
| Text = "Fake Wallpaper", | |
| ContextMenuStrip = contextMenuStrip | |
| }; | |
| } | |
| private void FakeWallpaper_Load(object sender, EventArgs e) { | |
| OpenFileDialog openFileDialog = new OpenFileDialog(); | |
| openFileDialog.Filter = "Imagens|*.jpg;*.jpeg;*.png;*.bmp"; | |
| if (openFileDialog.ShowDialog() != DialogResult.OK) { | |
| Close(); | |
| return; | |
| } | |
| this.BackgroundImage = Image.FromFile(openFileDialog.FileName); | |
| this.BackgroundImageLayout = ImageLayout.Stretch; | |
| this.FormBorderStyle = FormBorderStyle.None; | |
| Rectangle screen = Screen.PrimaryScreen.WorkingArea; | |
| this.Bounds = screen; | |
| // "Progman" == Desktop | |
| IntPtr progman = FindWindow("Progman", null); | |
| // Cria uma nova janela de WorkerW atrás dos ícones da área de trabalho | |
| IntPtr result = IntPtr.Zero; | |
| SendMessageTimeout(progman, 0x052C, new IntPtr(0), IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result); | |
| // Obtém a nova janela de WorkerW | |
| IntPtr workerw = IntPtr.Zero; | |
| EnumWindows(new EnumWindowsProc((tophandle, topparamhandle) => { | |
| IntPtr p = FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero); | |
| if (p != IntPtr.Zero) { | |
| workerw = FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero); | |
| } | |
| return true; | |
| }), IntPtr.Zero); | |
| // Posiciona o formulário abaixo dos ícones da área de trabalho e acima do wallpaper | |
| SetParent(this.Handle, workerw); | |
| } | |
| [DllImport("user32.dll")] | |
| private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |
| [DllImport("user32.dll")] | |
| static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); | |
| [DllImport("user32.dll")] | |
| static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out IntPtr lpdwResult); | |
| [DllImport("user32.dll")] | |
| private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); | |
| [DllImport("user32.dll")] | |
| private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); | |
| private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); | |
| [Flags] | |
| private enum SendMessageTimeoutFlags : uint { | |
| SMTO_NORMAL = 0x0000, | |
| SMTO_BLOCK = 0x0001, | |
| SMTO_ABORTIFHUNG = 0x0002, | |
| SMTO_NOTIMEOUTIFNOTHUNG = 0x0008 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Running with powershell without compiling: | |
| Add-Type ` | |
| -ReferencedAssemblies System.Drawing,System.Windows.Forms,System.Runtime.InteropServices ` | |
| -Path "$PSScriptRoot\FakeWallpaper.cs" | |
| [FakeWallpaper]::Main("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment