天天看點

有趣的Windows桌面圖示

突然閑了一兩天,盯着電腦桌面看的時候有些無聊(我原本的桌面圖示亂得不忍直視,現在為了讓它們“聽話”,都把其他轉移了。)

現在是這樣的

有趣的Windows桌面圖示

幹淨得過分了。前面我說要讓它們“聽話”,這是我的突發奇想吧,也許是看得無聊了。

現在我把它們變成這樣了。

有趣的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();
        }