C#實現類似QQ的隱藏浮動窗口、消息閃動

功能簡單介紹

  當語音客服系統登陸成功進入主界面時,本聊天工具將會本身主動隱藏在左下角位置,當鼠標移動到左下角時,本身主動彈出,當鼠標移開聊天窗口時,本身主動隱藏。假設想讓聊天窗口固定在桌面。僅僅要拖動一下聊天窗口,讓它不停留在邊界位置就可以了。工具

隱藏和懸浮方式類型QQ。this


1. 系統主界面

   

當點擊最小化button時, 在電腦右下角會顯示任務圖標,點擊任務圖標,將會在左下角位置彈出。spa

 

主界面各部分介紹:code

a) 消息列表:該區域的功能主要是顯示消息記錄。orm

b) 發送消息:輸入要發送的消息進行發送,默認羣聊。輸入消息後,按回車鍵或者點擊「發送」button,將進行消息發送。索引

c) 坐席人員:顯示所有已登陸客服系統的坐席人員,顯示方式:名稱(狀態)事件

d) 通知:記錄坐席上下線記錄圖片

實現浮動:it

        #region 停靠懸浮
        
        internal AnchorStyles StopDock = AnchorStyles.None;

        private void StopRectTimer_Tick(object sender, EventArgs e)
        {
            //假設鼠標在窗口上。則依據停靠位置顯示整個窗口
            if (this.Bounds.Contains(Cursor.Position))
            {
                switch (this.StopDock)
                {
                    case AnchorStyles.Top:
                        this.Location = new Point(this.Location.X, 0);
                        break;
                    case AnchorStyles.Bottom:
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
                        break;
                    case AnchorStyles.Left:
                        this.Location = new Point(0, this.Location.Y);
                        break;
                    case AnchorStyles.Right:
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
                        break;
                }
            }
            else  //假設鼠標離開窗口,則依據停靠位置隱藏窗口,但須留出部分窗口邊緣以便鼠標選中窗口
            {
                switch (this.StopDock)
                {
                    case AnchorStyles.Top:
                        this.Location = new Point(this.Location.X, (this.Height - 3) * (-1));
                        break;
                    case AnchorStyles.Bottom:
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - 5);
                        break;
                    case AnchorStyles.Left:
                        this.Location = new Point((-1) * (this.Width - 3), this.Location.Y);
                        break;
                    case AnchorStyles.Right:
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
                        break;
                }
            }
        }

        private void MainFrm_LocationChanged(object sender, EventArgs e)
        {
            if (this.Top <= 0)
            {
                this.StopDock = AnchorStyles.Top;
            }
            else if (this.Bottom >= Screen.PrimaryScreen.Bounds.Height)
            {
                this.StopDock = AnchorStyles.Bottom;
            }
            else if (this.Left <= 0)
            {
                this.StopDock = AnchorStyles.Left;
            }
            else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
            {
                this.StopDock = AnchorStyles.Right;
            }
            else
            {
                this.StopDock = AnchorStyles.None;
            }
        }

        #endregion

有消息來時,不斷閃動圖標。加入一個定時器,不斷切換該圖標,監聽,消息列表,假設有文字改變。則開啓定時器。

        int i = 0; //先設置一個全局變量 i ,用來控制圖片索引,而後建立定時事件,雙擊定時控件就可以編輯 
        private Icon ico1 = new Icon("img/q1.ico"); 
        private Icon ico2 = new Icon("img/q2.ico"); //兩個圖標 切換顯示 以達到消息閃動的效果

        //定時器 不斷閃動圖標
        private void timer1_Tick(object sender, EventArgs e)
        {
            //假設i=0則讓任務欄圖標變爲透明的圖標並且退出
            if (i < 1)
            {
                this.notifyIcon1.Icon = ico2;
                i++;
                return;
            }
            //假設i!=0,就讓任務欄圖標變爲ico1,並將i置爲0;
            else
                this.notifyIcon1.Icon = ico1;
            i = 0; 
        }

        //有消息來時 閃動
        private void ChatRoomMsg_TextChanged(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
        }

      private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.timer1.Enabled)
            {
                this.timer1.Enabled = false;
            }
            if (e.Button == MouseButtons.Left && this.WindowState == FormWindowState.Minimized)
            {
                //推斷是否已經最小化於托盤 
                if (WindowState == FormWindowState.Minimized)
                {
                    this.StopRectTimer.Enabled = false;

                    StopDock = AnchorStyles.None;
                    //還原窗口顯示 
                    WindowState = FormWindowState.Normal;
                    //激活窗口並給予它焦點 
                    //this.Activate();
                    //任務欄區顯示圖標 
                    this.ShowInTaskbar = false;
                    //托盤區圖標隱藏 
                    //notifyIcon1.Visible = true;

                    //默認顯示 左下角
                    this.Left = 0;
                    this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
                }
            }
        }
相關文章
相關標籤/搜索