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

一蕭煙雨發表於2014-07-19

功能簡介

  當語音客服系統登入成功進入主介面時,本聊天工具將會自動隱藏在左下角位置,當滑鼠移動到左下角時,自動彈出,當滑鼠移開聊天窗體時,自動隱藏。如果想讓聊天窗體固定在桌面,只要拖動一下聊天視窗,讓它不停留在邊界位置就可以了。隱藏和懸浮方式型別QQ。


1. 系統主介面

   

當點選最小化按鈕時, 在電腦右下角會顯示任務圖示,點選任務圖示,將會在左下角位置彈出。

 

主介面各部分介紹:

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

b) 傳送訊息:輸入要傳送的訊息進行傳送,預設群聊,輸入訊息後,按Enter鍵或者點選“傳送”按鈕,將進行訊息傳送。

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

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

實現浮動:

        #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;
                }
            }
        }


相關文章