Winform WebSocketSharp(客户端)连接JAVA WebSocket (服务端)的消息推送桌面弹框提醒

WebSocketSharp 请参考官网
https://github.com/sta/websoc...
坑:

ws.OnMessage += (sender, e) => { BalloonForm balloon = new
BalloonForm("标题", "内容", "底部");

  balloon.Visible = false;
  balloon.ShowDialog();//此处弹窗必须使用模式对话框,否则窗体打开里面控件样式不显示,鼠标一直显示加载状态 

}

弹窗代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xxxx.page
{
   public class BalloonForm : Form
    {
        private System.ComponentModel.IContainer components = null;
        
        private Label title;
        private Label textContent;
        private Label staticTime;
        private Timer timer1;
        private Timer timer2;
        private Timer timer3;

        public int StayTime = 5000;

        private int heightMax, widthMax;
        
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.title = new Label();
            this.textContent = new Label();
            this.staticTime = new Label();
            this.timer1 = new Timer(this.components);
            this.timer2 = new Timer(this.components);
            this.timer3 = new Timer(this.components);
            this.SuspendLayout();
            // 
            // title
            // 
            this.title.AutoSize = true;
            this.title.Location = new Point(10, 10);
            this.title.Name = "title";
            this.title.TabIndex = 0;
            this.title.Text = "标题";

            // 
            // textContent
            // 
            this.textContent.AutoSize = true;
            this.textContent.Location = new Point(30, 40);
            this.textContent.Font= new Font("宋体",12, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
            this.textContent.Name = "textContent";
            this.textContent.TabIndex = 0;
            this.textContent.Text = "内容";

            // 
            // staticTime
            // 
            this.staticTime.AutoSize = true;
            this.staticTime.Location = new Point(120, 90);
            this.staticTime.Name = "staticTime";
            this.staticTime.TabIndex = 0;
            this.staticTime.Text = "开始时间";

            // 
            // timer1
            // 
            this.timer1.Interval = 10;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // timer2
            // 
            this.timer2.Interval = 10;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
            // 
            // timer3
            // 
            this.timer3.Interval = 10;
            this.timer3.Tick += new System.EventHandler(this.timer3_Tick);
            // 
            // BalloonForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(200, 120);
            this.Controls.Add(this.title);
            this.Controls.Add(this.textContent);
            this.Controls.Add(this.staticTime);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "BalloonForm";
            this.Opacity = 1;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "BalloonForm";
            this.TopMost = true;
            this.Load += new System.EventHandler(this.BalloonForm_Load);
            //this.ResumeLayout(false);
            //this.PerformLayout();
        }

        #endregion
        public string BalloonText
        {
            get { return this.title.Text; }
            set { this.title.Text = value; }
        }

        public Color BalloonBackColor
        {
            get { return this.BackColor; }
            set { this.BackColor = value; }
        }

        public Color BalloonForeColor
        {
            get { return this.title.ForeColor; }
            set { this.title.ForeColor = value; }
        }


        public BalloonForm(string title,string textContent,string staticTime)
        {
            //方法一:不进行跨线程安全检查  
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            this.title.Text = title;
            this.textContent.Text = textContent;
            this.staticTime.Text = staticTime;
            this.HeightMax = 120;//窗体滚动的高度
            this.WidthMax = 260;//窗体滚动的宽度
            this.ScrollShow();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ScrollUp();

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            timer3.Enabled = true;

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            ScrollDown();

        }

        public int HeightMax
        {
            set
            {
                heightMax = value;
            }
            get
            {
                return heightMax;
            }
        }

        public int WidthMax
        {
            set
            {
                widthMax = value;
            }
            get
            {
                return widthMax;
            }
        }
        public void ScrollShow()
        {
            this.Width = widthMax;
            this.Height = 0;
            this.Show();
            this.timer1.Enabled = true;
        }
        private void ScrollUp()
        {
            if (Height < heightMax)
            {
                this.Height += 3;
                this.Location = new Point(this.Location.X, this.Location.Y - 3);
            }
            else
            {
                this.timer1.Enabled = false;
                this.timer2.Enabled = true;
            }
        }

        private void ScrollDown()
        {
            if (Height > 3)
            {
                this.Height -= 3;
                this.Location = new Point(this.Location.X, this.Location.Y + 3);
            }
            else
            {
                this.timer3.Enabled = false;
                this.Close();
            }
        }

        private void BalloonForm_Load(object sender, EventArgs e)
        {
            Screen screen = Screen.PrimaryScreen; ;//获取屏幕变量
            this.Location = new Point(screen.WorkingArea.Width - widthMax - 20, screen.WorkingArea.Height - 38);//WorkingArea为Windows桌面的工作区
            this.timer2.Interval = StayTime;

        }

    }
}


你可能感兴趣的:(winform)