有趣的Windows桌面图标

突然闲了一两天,盯着电脑桌面看的时候有些无聊(我原本的桌面图标乱得不忍直视,现在为了让它们“听话”,都把其他转移了。)

现在是这样的

干净得过分了。前面我说要让它们“听话”,这是我的突发奇想吧,也许是看得无聊了。

现在我把它们变成这样了。

暂时用这种方式展现他们吧,如果我找到了创建DeskTopIcons的方式,我倒是想做个贪吃蛇...

说一下原理,获取桌面的窗体句柄,展现图标的是一个ListView,获取这个句柄后向它发送移动图标的消息就行了。

下面贴一些源码,就C#吧。

public class WindowsAPI
    {
        #region WindowsApi
        const int WM_MOUSEWHEEL = 0x020A; //鼠标滚轮
        const int WM_LBUTTONDOWN = 0x0201;//鼠标左键
        const int WM_LBUTTONUP = 0x0202;
        const int WM_KEYDOWN = 0x0100;//模拟按键
        const int WM_KEYUP = 0x0101;
        const int MOUSEEVENTF_MOVE = 0x0001;//用于琴台鼠标移动
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;//前台鼠标单击
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int WM_SETTEXT = 0x000C;//设置文字
        const int WM_GETTEXT = 0x000D;//读取文字
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, string lParam);

        [DllImport("user32.dll")]//在窗口列表中寻找与指定条件相符的第一个子窗口
        public static extern int FindWindowEx(int hwndParent, // handle to parent window
                                                int hwndChildAfter, // handle to child window
                                                string className, //窗口类名            
                                                string windowName);

        [DllImport("user32.DLL")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent,
            IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
}

    public class Pos
    {
        public int x;
        public int y;
        public bool xl = false;
        public bool yt = true;
        public bool xr = false;
        public bool yb = false;
    }

        const uint LVM_SETITEMPOSITION = 0x1000 + 15;
        IntPtr hWnd;
        Thread thx;

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Monitor.Exit(monitor);
        }

        private static string monitor = ""; 
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Monitor.Enter(monitor);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IntPtr hWndParent = (IntPtr)WindowsAPI.FindWindowEx(0, 0, "WorkerW", null);
            IntPtr hWndItem;
            while (true)
            {
                hWndItem = (IntPtr)WindowsAPI.FindWindowEx((int)hWndParent, 0, "SHELLDLL_DefView", null);
                if (hWndItem != IntPtr.Zero)
                {
                    hWnd = (IntPtr)WindowsAPI.FindWindowEx((int)hWndItem, 0, "SysListView32", "FolderView");
                    break;
                }
                hWndParent = (IntPtr)WindowsAPI.FindWindowEx(0, (int)hWndParent, "WorkerW", null);
            }
            List<Pos> listPos = new List<Pos>() { };
            for (int i = 0; i < 7; i++)
            {
                listPos.Add(new Pos() { x = 0, y = i * 100 });
                listPos[i].xr = true;
                listPos[i].xl = false;
                listPos[i].yt = false;
                listPos[i].yb = false;
            }
            Monitor.Enter(monitor);
            thx = new Thread(() =>
            {
                while (true)
                {
                    Monitor.Enter(monitor);
                    for (int i = 0; i < 7; i++)
                    {
                        MovdDeskTopIons(i, listPos[i].x, listPos[i].y);
                        if (listPos[i].x >= 1200 && listPos[i].y <= 0)
                        {
                            listPos[i].xl = true;
                            listPos[i].xr = false;
                            listPos[i].yt = false;
                            listPos[i].yb = false;
                        }
                        if (listPos[i].x >= 1200 && listPos[i].y >= 900)
                        {
                            listPos[i].xl = false;
                            listPos[i].xr = false;
                            listPos[i].yt = false;
                            listPos[i].yb = true;
                        }
                        if (listPos[i].x <= 0 && listPos[i].y >= 900)
                        {
                            listPos[i].xl = false;
                            listPos[i].xr = true;
                            listPos[i].yt = false;
                            listPos[i].yb = false;
                        }
                        if (listPos[i].x <= 0 && listPos[i].y <= 0)
                        {
                            listPos[i].xl = false;
                            listPos[i].xr = false;
                            listPos[i].yt = true;
                            listPos[i].yb = false;
                        }
                        if (listPos[i].yt)
                            listPos[i].x += 2;
                        else if (listPos[i].xl)
                            listPos[i].y += 2;
                        else if (listPos[i].yb)
                            listPos[i].x -= 2;
                        else if (listPos[i].xr)
                            listPos[i].y -= 2;
                    }
                    Monitor.Exit(monitor);
                    Thread.Sleep(1);
                }
            });
            thx.IsBackground = false;
            thx.Start();
        }

猜你喜欢

转载自blog.csdn.net/qq_28194303/article/details/82255585