C
C#16mo ago
maria 🌟

❔ problem changing the opacity of desktop icons via windows api

I have a code thath i made using information on internet and it works but some times the desktop get blacks depending on the opacity of the icons
31 Replies
maria 🌟
maria 🌟16mo ago
code in peaces
private const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x00080000; // Creates a layered window.
const int WS_EX_TRANSPARENT = 0x00000020; // Not painted until after siblings beneath the window
const int WS_EX_TOOLWINDOW = 0x00000080; // Creates a floating toolbar window, does not appear in the taskba
private const int LWA_ALPHA = 0x2;


public static int CurrentAlpha = GetDesktopIconOpacity();
public static void SetDesktopIconsOpacity(int Alpha)
{
IntPtr hWnd = GetDesktopHwnd();

IntPtr exStyle = NativeMethods.GetWindowLong(hWnd, GWL_EXSTYLE);

//
NativeMethods.SetWindowLong(hWnd, GWL_EXSTYLE, (IntPtr)(exStyle.ToInt64() | WS_EX_LAYERED | WS_EX_TOOLWINDOW));


int CoreAlpha = (int)(255 * (double)Alpha / 100);

if (Alpha != 100 & Alpha != 0)
{
CoreAlpha += 1;
}

NativeMethods.SetLayeredWindowAttributes(hWnd, 0, (byte)CoreAlpha, LWA_ALPHA);
//NativeMethods.SendMessage(hWnd, (0x1000 + 38), IntPtr.Zero, (IntPtr)ColorTranslator.ToWin32(Color.Transparent));

//NativeMethods.UpdateWindow(hWnd);

CurrentAlpha = Alpha;
}
private const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x00080000; // Creates a layered window.
const int WS_EX_TRANSPARENT = 0x00000020; // Not painted until after siblings beneath the window
const int WS_EX_TOOLWINDOW = 0x00000080; // Creates a floating toolbar window, does not appear in the taskba
private const int LWA_ALPHA = 0x2;


public static int CurrentAlpha = GetDesktopIconOpacity();
public static void SetDesktopIconsOpacity(int Alpha)
{
IntPtr hWnd = GetDesktopHwnd();

IntPtr exStyle = NativeMethods.GetWindowLong(hWnd, GWL_EXSTYLE);

//
NativeMethods.SetWindowLong(hWnd, GWL_EXSTYLE, (IntPtr)(exStyle.ToInt64() | WS_EX_LAYERED | WS_EX_TOOLWINDOW));


int CoreAlpha = (int)(255 * (double)Alpha / 100);

if (Alpha != 100 & Alpha != 0)
{
CoreAlpha += 1;
}

NativeMethods.SetLayeredWindowAttributes(hWnd, 0, (byte)CoreAlpha, LWA_ALPHA);
//NativeMethods.SendMessage(hWnd, (0x1000 + 38), IntPtr.Zero, (IntPtr)ColorTranslator.ToWin32(Color.Transparent));

//NativeMethods.UpdateWindow(hWnd);

CurrentAlpha = Alpha;
}
public static int GetDesktopIconOpacity() => NativeMethods.GetLayeredWindowAttributes(GetDesktopHwnd(), out uint crKey, out byte bAlpha, out uint dwFlags) ? (100 * bAlpha) / 255 : 255;

private static Process ExplorerProcess;
private static IntPtr LastExplorerIconsHandle;
private static IntPtr GetDesktopHwnd()
{
if (ExplorerProcess != null)
{
if (ExplorerProcess.HasExited)
{
ExplorerProcess = null;

return GetDesktopHwnd();
}
else
{
return LastExplorerIconsHandle;
}
}

ExplorerProcess = Process.GetProcessesByName("explorer").FirstOrDefault();

IntPtr hWnd = NativeMethods.FindWindow("Progman", "Program Manager");
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", "FolderView");

if (hWnd == IntPtr.Zero)
{
IntPtr hDesktop = NativeMethods.GetDesktopWindow();

foreach (var handle in GetChildWindows(hDesktop))
{
hWnd = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "SHELLDLL_DefView", null);

if (hWnd == IntPtr.Zero)
{
continue;
}

hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", "FolderView");

if (hWnd != IntPtr.Zero)
{
break;
}
}
}

LastExplorerIconsHandle = hWnd;

return hWnd;
}
public static int GetDesktopIconOpacity() => NativeMethods.GetLayeredWindowAttributes(GetDesktopHwnd(), out uint crKey, out byte bAlpha, out uint dwFlags) ? (100 * bAlpha) / 255 : 255;

private static Process ExplorerProcess;
private static IntPtr LastExplorerIconsHandle;
private static IntPtr GetDesktopHwnd()
{
if (ExplorerProcess != null)
{
if (ExplorerProcess.HasExited)
{
ExplorerProcess = null;

return GetDesktopHwnd();
}
else
{
return LastExplorerIconsHandle;
}
}

ExplorerProcess = Process.GetProcessesByName("explorer").FirstOrDefault();

IntPtr hWnd = NativeMethods.FindWindow("Progman", "Program Manager");
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", "FolderView");

if (hWnd == IntPtr.Zero)
{
IntPtr hDesktop = NativeMethods.GetDesktopWindow();

foreach (var handle in GetChildWindows(hDesktop))
{
hWnd = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "SHELLDLL_DefView", null);

if (hWnd == IntPtr.Zero)
{
continue;
}

hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", "FolderView");

if (hWnd != IntPtr.Zero)
{
break;
}
}
}

LastExplorerIconsHandle = hWnd;

return hWnd;
}
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);

public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);

List<IntPtr> GetRootWindowsOfProcess(int pid)
{
List<IntPtr> rootWindows = GetChildWindows(IntPtr.Zero);
List<IntPtr> dsProcRootWindows = new List<IntPtr>();
foreach (IntPtr hWnd in rootWindows)
{
uint lpdwProcessId;
GetWindowThreadProcessId(hWnd, out lpdwProcessId);
if (lpdwProcessId == pid)
dsProcRootWindows.Add(hWnd);
}
return dsProcRootWindows;
}

public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
Win32Callback childProc = new Win32Callback(EnumWindow);

EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}

private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);

return true;
}
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);

public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);

List<IntPtr> GetRootWindowsOfProcess(int pid)
{
List<IntPtr> rootWindows = GetChildWindows(IntPtr.Zero);
List<IntPtr> dsProcRootWindows = new List<IntPtr>();
foreach (IntPtr hWnd in rootWindows)
{
uint lpdwProcessId;
GetWindowThreadProcessId(hWnd, out lpdwProcessId);
if (lpdwProcessId == pid)
dsProcRootWindows.Add(hWnd);
}
return dsProcRootWindows;
}

public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
Win32Callback childProc = new Win32Callback(EnumWindow);

EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}

private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);

return true;
}
im sory if its a bunch of code but idk what else can i do the error happends randomly on even win 10 or win 11 pls halp
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
maria 🌟
maria 🌟16mo ago
no
ero
ero16mo ago
what's the issue?
maria 🌟
maria 🌟16mo ago
maria 🌟
maria 🌟16mo ago
the windows desktop backgorunds turns black
ero
ero16mo ago
in what circumstance
maria 🌟
maria 🌟16mo ago
after a time i change the opacity of the icons
ero
ero16mo ago
after what time, what do you change the opacity to come on provide even the least amount of info
maria 🌟
maria 🌟16mo ago
the time i dont know more than 10 minutes maybie its random and its like a timer it starts when you change the opacity if you put the opacity to 50% it is 50% black and the other 50% the background if you put the opacity of the icons to 0% the desktop shows nice and if you put the opacity to 100% it just all black like if the desktop icons background becoming black instead of transparent for no reason aftrer x period of time very weird iusse
Jester
Jester16mo ago
i wouldnt have expected anyone to do this weird issue you have <:PES_Think:639363477458255874> i might take a better look at the code tmr
maria 🌟
maria 🌟16mo ago
I sended the whole class if you want it on file i can send sory if the code is bad im not pro
Jester
Jester16mo ago
id say make sure you are editing the correct window. does this have to be in c#? i think this would be easier to do in c
sibber
sibber16mo ago
so you just force explorer to be a layered window i cant imagine any world where that would work reliably
Jester
Jester16mo ago
yeah the desktop background is made up of a few windows and the icons is one of them you can pretty reliably make windows layered
sibber
sibber16mo ago
well, it depends on the app
Jester
Jester16mo ago
i think the issue might be that these windows are child windows and those dont always behave the same as normal windows
sibber
sibber16mo ago
but i imagine explorer assumes that no one will play around with the styles and it does some pretty not normal stuff with that assumption maybe for notepad or something, sure it could be reliable
Jester
Jester16mo ago
ive been messing with explorer windows, its probably "ok"
sibber
sibber16mo ago
but tbf ive never messed around with explorer
Jester
Jester16mo ago
it can also be that the explorer windows change their own styles now and then and thus overwrite your changes i have, bcuz im writing another program to modify my desktop
sibber
sibber16mo ago
well if it works it works
Jester
Jester16mo ago
i think this GetDesktopHwnd could maybe be improved
maria 🌟
maria 🌟16mo ago
yea but it works for now but if you want to make some suggestions go ahead
maria 🌟
maria 🌟16mo ago
this when desktop gets bugged and i put opacity to 100%
maria 🌟
maria 🌟16mo ago
this with 50%
maria 🌟
maria 🌟16mo ago
and this with 0%
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
maria 🌟
maria 🌟16mo ago
no
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.