在任务栏中添加控件 如360小助手、QQ桌面小助手


当点击 右下角的三角形时 任务栏刷新后  就重新排版了 按钮就看不见或者看不全了 可以进行定时设置(有更好的方法请告诉我一声··)

    bool ReBarWinMoveResult = Win32.MoveWindow(ReBarWindow32, rc_temp.X + rect.X, 0, rc_temp.Width - rect.Width, rc_temp.Height, true);


private void Form1_Load(object sender, EventArgs e)
        {
            IntPtr Shell_TrayWnd = Win32.FindWindow("Shell_TrayWnd", null);
            IntPtr ReBarWindow32 = Win32.FindWindowEx(Shell_TrayWnd, IntPtr.Zero, "ReBarWindow32", null);
     
            Rectangle rect = new Rectangle(50,0,100,30);
            uint style = (uint)(Aid.Win32.WindowStyles.WS_CHILD | Aid.Win32.WindowStyles.WS_VISIBLE | Aid.Win32.WindowStyles.WS_CLIPSIBLINGS); 
            IntPtr hwnd = Win32.CreateWindowEx(0, "Button",
                       "测试按钮",
                       style,
                       rect.X, rect.Y, rect.Width, rect.Height,
                       Shell_TrayWnd,
                       IntPtr.Zero,
                      IntPtr.Zero,
                      0);
            Rectangle rc_temp = getREC(ReBarWindow32);
            bool ReBarWinMoveResult = Win32.MoveWindow(ReBarWindow32, rc_temp.X + rect.X, 0, rc_temp.Width - rect.Width, rc_temp.Height, true);
        }
        public Rectangle getREC(IntPtr intptr)
        {
            Rectangle rc_temp = new Rectangle();
            Win32.GetWindowRect(intptr, ref rc_temp);
            rc_temp.Size = new Size(rc_temp.Width - rc_temp.X, rc_temp.Height - rc_temp.Top);
            return rc_temp;
        }












win32.cs

class Win32
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hwnd, int wMsg, int wParam, ref int lParam);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hwnd, int wMsg, string wParam, string lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }


        //获取窗口标题

        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
        IntPtr hWnd, //窗口句柄
        StringBuilder lpString, //标题
        int nMaxCount //最大值
        );
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr cilHwin, IntPtr parHwin);
        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr chiHwin);
        //[DllImport("user32.dll")]
        //public static extern bool GetWindowRect(IntPtr Hwin, ref Rectangle rect);
        [DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public static extern IntPtr GetWindowRect(IntPtr hwnd, ref Rectangle lpRect);
        //IntPtr inp = IntPtr.Zero;
        [DllImport("user32.dll")]
        public extern static IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public extern static bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

        public static uint LWA_COLORKEY = 0x00000001;
        public static uint LWA_ALPHA = 0x00000002;
        public static uint WS_EX_LAYERED = 0x80000;
        [DllImport("user32.dll")]
        public extern static uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
        [DllImport("user32.dll")]
        public extern static uint GetWindowLong(IntPtr hwnd, int nIndex);
        public enum WindowStyle : int { GWL_EXSTYLE = -20 }
        public enum ExWindowStyle : uint { WS_EX_LAYERED = 0x00080000 }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateWindowEx(
            int dwExStyle,                                //窗口的扩展风格
            string lpszClassName,                         //指向注册类名的指针
            string lpszWindowName,                        //指向窗口名称的指针
            int style,                                    //窗口风格
            int x,                                        //窗口的水平位置
            int y,                                        //窗口的垂直位置
            int width,                                    //窗口的宽度
            int height,                                   //窗口的高度
            IntPtr hWndParent,                            //父窗口的句柄
            IntPtr hMenu,                                 //菜单的句柄或是子窗口的标识符
            IntPtr hInst,                                 //应用程序实例的句柄
            [MarshalAs(UnmanagedType.AsAny)] object pvParam//指向窗口的创建数据
            ); 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateWindowEx(
            uint dwExStyle,                                //窗口的扩展风格
            string lpszClassName,                         //指向注册类名的指针
            string lpszWindowName,                        //指向窗口名称的指针
            uint style,                                    //窗口风格
            int x,                                        //窗口的水平位置
            int y,                                        //窗口的垂直位置
            int width,                                    //窗口的宽度
            int height,                                   //窗口的高度
            IntPtr hWndParent,                            //父窗口的句柄
            IntPtr hMenu,                                 //菜单的句柄或是子窗口的标识符
            IntPtr hInst,                                 //应用程序实例的句柄
            [MarshalAs(UnmanagedType.AsAny)] object pvParam//指向窗口的创建数据
            );
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateWindowEx(
            string lpszClassName,                         //指向注册类名的指针
            string lpszWindowName,                        //指向窗口名称的指针
            uint style,                                    //窗口风格
            int x,                                        //窗口的水平位置
            int y,                                        //窗口的垂直位置
            int width,                                    //窗口的宽度
            int height,                                   //窗口的高度
            IntPtr hWndParent,                            //父窗口的句柄
            IntPtr hMenu,                                 //菜单的句柄或是子窗口的标识符
            IntPtr hInst,                                 //应用程序实例的句柄
            [MarshalAs(UnmanagedType.AsAny)] object pvParam//指向窗口的创建数据
            );

 

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct CREATESTRUCT
        {
            /// <summary>
            /// 指向将被用于创建窗口的数据的指针。 
            /// </summary>
            public IntPtr lpCreateParams;
            /// <summary>
            /// 指向将被用于创建窗口的数据的指针。 
            /// </summary>
            public IntPtr hInstance;
            /// <summary>
            /// 指向将被用于创建窗口的数据的指针。 
            /// </summary>
            public IntPtr hMenu;
            /// <summary>
            /// 指向将被用于创建窗口的数据的指针
            /// </summary>
            public IntPtr hwndParent;
            public int cy;
            public int cx;
            public int y;
            public int x;
            /// <summary>
            /// 指定了新窗口的风格。
            /// </summary>
            public uint style;
            /// <summary>
            /// 指向一个以null结尾的字符串,指定了新窗口的名字。 
            /// </summary>
            public string lpszName;
            /// <summary>
            /// 指向一个以null结尾的字符串,指定了新窗口的Windows类名(一个WNDCLASS结构;更多的信息参见Win32 SDK文档)
            /// </summary>
            public string lpszClass;
            /// <summary>
            /// 指向一个以null结尾的字符串,指定了新窗口的Windows类名(一个WNDCLASS结构;更多的信息参见Win32 SDK文档)
            /// </summary>
            public int dwExStyle;
        }

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
        public enum WindowStyles : uint
        {
            WS_OVERLAPPED = 0x00000000,
            WS_POPUP = 0x80000000,
            WS_CHILD = 0x40000000,
            WS_MINIMIZE = 0x20000000,
            WS_VISIBLE = 0x10000000,
            WS_DISABLED = 0x08000000,
            WS_CLIPSIBLINGS = 0x04000000,
            WS_CLIPCHILDREN = 0x02000000,
            WS_MAXIMIZE = 0x01000000,
            WS_BORDER = 0x00800000,
            WS_DLGFRAME = 0x00400000,
            WS_VSCROLL = 0x00200000,
            WS_HSCROLL = 0x00100000,
            WS_SYSMENU = 0x00080000,
            WS_THICKFRAME = 0x00040000,
            WS_GROUP = 0x00020000,
            WS_TABSTOP = 0x00010000,

            WS_MINIMIZEBOX = 0x00020000,
            WS_MAXIMIZEBOX = 0x00010000,

            WS_CAPTION = WS_BORDER | WS_DLGFRAME,
            WS_TILED = WS_OVERLAPPED,
            WS_ICONIC = WS_MINIMIZE,
            WS_SIZEBOX = WS_THICKFRAME,
            WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW,

            WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
            WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
            WS_CHILDWINDOW = WS_CHILD,

            //Extended Window Styles

            WS_EX_DLGMODALFRAME = 0x00000001,
            WS_EX_NOPARENTNOTIFY = 0x00000004,
            WS_EX_TOPMOST = 0x00000008,
            WS_EX_ACCEPTFILES = 0x00000010,
            WS_EX_TRANSPARENT = 0x00000020,

            //#if(WINVER >= 0x0400)

            WS_EX_MDICHILD = 0x00000040,
            WS_EX_TOOLWINDOW = 0x00000080,
            WS_EX_WINDOWEDGE = 0x00000100,
            WS_EX_CLIENTEDGE = 0x00000200,
            WS_EX_CONTEXTHELP = 0x00000400,

            WS_EX_RIGHT = 0x00001000,
            WS_EX_LEFT = 0x00000000,
            WS_EX_RTLREADING = 0x00002000,
            WS_EX_LTRREADING = 0x00000000,
            WS_EX_LEFTSCROLLBAR = 0x00004000,
            WS_EX_RIGHTSCROLLBAR = 0x00000000,

            WS_EX_CONTROLPARENT = 0x00010000,
            WS_EX_STATICEDGE = 0x00020000,
            WS_EX_APPWINDOW = 0x00040000,

            WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE),
            WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST),

            //#endif /* WINVER >= 0x0400 */

            //#if(WIN32WINNT >= 0x0500)

            WS_EX_LAYERED = 0x00080000,

            //#endif /* WIN32WINNT >= 0x0500 */

            //#if(WINVER >= 0x0500)

            WS_EX_NOINHERITLAYOUT = 0x00100000, // Disable inheritence of mirroring by children
            WS_EX_LAYOUTRTL = 0x00400000, // Right to left mirroring

            //#endif /* WINVER >= 0x0500 */

            //#if(WIN32WINNT >= 0x0500)

            WS_EX_COMPOSITED = 0x02000000,
            WS_EX_NOACTIVATE = 0x08000000

            //#endif /* WIN32WINNT >= 0x0500 */

        }
 

        //public const int LWA_ALPHA = 0x2;
        //public const int LWA_COLORKEY = 0x1;


        //[DllImport("user32", EntryPoint = "SetWindowLong")]
        //public static extern uint SetWindowLong(
        //IntPtr hwnd,
        //int nIndex,
        //uint dwNewLong
        //);

        //[DllImport("user32", EntryPoint = "GetWindowLong")]
        //public static extern uint GetWindowLong(
        //IntPtr hwnd,
        //int nIndex
        //);

        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        public static extern int SetLayeredWindowAttributes(
        IntPtr hwnd,
        int crKey,
        int bAlpha,
        int dwFlags
        );




 
    }


你可能感兴趣的:(在任务栏中添加控件 如360小助手、QQ桌面小助手)