【代码分享】在任务栏显示一言

先上效果

上班摸鱼无聊搓代码练练手,在任务栏显示东西,最好的方案是使用deskband(有点烧脑细胞),我找了另一个方法实现 https://blog.csdn.net/Factor_/article/details/84799332
可以在任务栏显示无边框窗体

核心代码【winform】

       [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", EntryPoint = "SetParent")]
        static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);
        [DllImport("user32.dll")]
        static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

		public MainForm()
		{
			InitializeComponent();
			label1.Text=GetWebClient("https://international.v1.hitokoto.cn/?c=i&encode=text");
			label1.BackColor=  Color.FromArgb(129,148,170);
			
			
			IntPtr hShell = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            IntPtr hBar = FindWindowEx(hShell, IntPtr.Zero, "ReBarWindow32", null);
            IntPtr hMin = FindWindowEx(hBar, IntPtr.Zero, "MSTaskSwWClass", null);
				
            Rectangle rcMin= new Rectangle();   
            Rectangle rcShell= new Rectangle();   
            GetWindowRect(hMin, ref rcMin);
            GetWindowRect(hShell, ref rcShell);

            //Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
            Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            
            MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left - this.Width, rcMin.Bottom - rcMin.Top, true);
			SetParent(this.Handle, hBar );
			MoveWindow(this.Handle, rcMin.Right - rcMin.Left  , (ScreenArea.Height - rcShell.Y - this.Height)/2 +2, this.Width, this.Height, true);

		}
		private string GetWebClient(string url)
		{
        	ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
		    
		    string strHTML = "";
		    WebClient myWebClient = new WebClient();
		    Stream myStream = myWebClient.OpenRead(url);
		    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
		    strHTML = sr.ReadToEnd();
		    myStream.Close();
		    return strHTML;
		}

附上源码

更多脑洞期待你的发掘