c# - How to hide desktop icons programatically? -
how can show/hide desktop icons programatically, using c#?
i'm trying create alternative desktop, uses widgets, , need hide old icons.
you can using windows api. here sample code in c# toggle desktop icons.
[dllimport("user32.dll", setlasterror = true)] static extern intptr findwindow(string lpclassname, string lpwindowname); [dllimport("user32.dll", setlasterror = true)] static extern intptr getwindow(intptr hwnd, getwindow_cmd ucmd); enum getwindow_cmd : uint { gw_hwndfirst = 0, gw_hwndlast = 1, gw_hwndnext = 2, gw_hwndprev = 3, gw_owner = 4, gw_child = 5, gw_enabledpopup = 6 } [dllimport("user32.dll", charset = charset.auto)] static extern intptr sendmessage(intptr hwnd, uint32 msg, intptr wparam, intptr lparam); private const int wm_command = 0x111; static void toggledesktopicons() { var toggledesktopcommand = new intptr(0x7402); intptr hwnd = getwindow(findwindow("progman", "program manager"), getwindow_cmd.gw_child); sendmessage(hwnd, wm_command, toggledesktopcommand, intptr.zero); }
this sends message shelldll_defview child window of progman, tells toggle visibility (by adding or removing ws_visible style) of it's child, "folderview". "folderview" actual window contains icons.
to test see if icons visible or not, can query ws_visible style using getwindowinfo function, shown below:
[return: marshalas(unmanagedtype.bool)] [dllimport("user32.dll", setlasterror = true)] private static extern bool getwindowinfo(intptr hwnd, ref windowinfo pwi); [structlayout(layoutkind.sequential)] public struct rect { private int _left; private int _top; private int _right; private int _bottom; } [structlayout(layoutkind.sequential)] struct windowinfo { public uint cbsize; public rect rcwindow; public rect rcclient; public uint dwstyle; public uint dwexstyle; public uint dwwindowstatus; public uint cxwindowborders; public uint cywindowborders; public ushort atomwindowtype; public ushort wcreatorversion; public windowinfo(boolean? filler) : this() // allows automatic initialization of "cbsize" "new windowinfo(null/true/false)". { cbsize = (uint32)(marshal.sizeof(typeof(windowinfo))); } }
here function calls above code , returns true if window visible, false if not.
static bool isvisible() { intptr hwnd = getwindow(getwindow(findwindow("progman", "program manager"), getwindow_cmd.gw_child), getwindow_cmd.gw_child); windowinfo info = new windowinfo(); info.cbsize = (uint)marshal.sizeof(info); getwindowinfo(hwnd, ref info); return (info.dwstyle & 0x10000000) == 0x10000000; }
the windows api code along more information window styles can found here: http://www.pinvoke.net/default.aspx/user32/getwindowinfo.html
wiki
Comments
Post a Comment