QQ揭祕:如何實現托盤閃動訊息提醒?【低調贈送:QQ高仿版GG 4.1 最新原始碼】

C#開源即時通訊GGTalk發表於2014-12-31

  當QQ收到好友的訊息時,托盤的圖示會變成好友的頭像,並閃動起來,點選托盤,就會彈出與好友的聊天框,隨即,托盤恢復成QQ的圖示,不再閃動。當然,如果還有其它的好友的訊息沒有提取,托盤的圖示會變成另一個好友的圖示,並繼續閃動。那麼,QQ的這一效果是如何實現的了?我在QQ高仿GG2014中實現了同樣的效果,這裡我就詳細地介紹一下。另外,文末最後會奉上GG最新版本4.1的原始碼,這次甚至包含了JustLib專案的原始碼哦!

      想要直接下載體驗的朋友請點選:“下載中心”

一.TwinkleNotifyIcon的實現原理

  這個會閃動的托盤圖示,我將其定義為一個元件TwinkleNotifyIcon,我們先看TwinkleNotifyIcon的類圖:

  

   從TwinkleNotifyIcon類圖,我們已經可以看出大致的實現方案:

(1)TwinkleNotifyIcon 內部使用了NotifyIcon,以顯示在右下角的托盤。

(2)使用一個Timer定時器來控制托盤的閃動。

(3)使用一個佇列friendQueue來存放待提取的好友訊息。

(4)使用另一個佇列groupQueue來存放待提取的群訊息。

(5)當網路引擎接收到一個好友/群訊息時,我們就呼叫PushFriendMessage/PushGroupMessage方法,將其壓入friendQueue/groupQueue,並開始閃動圖示。

(6)當托盤被點選時,就從Queue中提取最早的一個訊息,並將其交給對應的聊天視窗去處理。

二.TwinkleNotifyIcon 實現要點

  我們順著以下的順序來研究TwinkleNotifyIcon的實現程式碼,就很容易了:

(1)壓入好友/群訊息。

(2)點選托盤,提取訊息。

(3)重新判斷Queue中是否還有待提取的訊息,以設定托盤的狀態。

1. 壓入訊息

  我們以PushFriendMessage方法為例,PushGroupMessage的道理是一樣的。

    public void PushFriendMessage(string userID, int informationType, byte[] info, object tag)
    {           
        lock (this.locker)
        {
            try
            {
                this.twinkleNotifySupporter.PlayAudioAsyn(); //播放訊息提示音
                //首先檢視是否已經存在對應的聊天視窗
                IChatForm form = this.twinkleNotifySupporter.GetExistedChatForm(userID); 
                if (form != null)
                {
                    form.HandleReceivedMessage(informationType, info, tag);
                    return;
                }

                //接下來準備將訊息壓入queue
                UnhandleFriendMessageBox cache = null;
                lock (this.locker)
                {
                    //先檢視queue中目標好友對應的Cache是否存在
                    for (int i = 0; i < this.friendQueue.Count; i++) 
                    {
                        if (this.friendQueue[i].User == userID)
                        {
                            cache = this.friendQueue[i];
                            break;
                        }
                    }

                    if (cache == null) //如果不存在,則為好友新建一個Cache
                    {
                        cache = new UnhandleFriendMessageBox(userID);
                        this.friendQueue.Add(cache);
                        //觸發UnhandleMessageOccured事件
                        if (this.UnhandleMessageOccured != null)
                        {
                            this.UnhandleMessageOccured(UnhandleMessageType.Friend, userID); 
                        }
                    }

                    cache.MessageList.Add(new Parameter<int, byte[], object>(informationType, info, tag));
                }

                string userName = this.twinkleNotifySupporter.GetFriendName(userID);
                this.notifyIcon1.Text = string.Format("{0}({1})  {2}條訊息", userName, userID, cache.MessageList.Count);
                //獲取好友的頭像,將其作為托盤圖示
                this.twinkleIcon = this.twinkleNotifySupporter.GetHeadIcon(userID);
                this.ControlTimer(true); //啟動閃爍
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
    }

(1)在壓入訊息的時候,先要播放訊息提示音,以通知使用者收到了新的訊息。

(2)首先要判斷訊息的來源好友是否正在和自己聊天(已經開啟了與對方的聊天視窗),如果是,則直接將訊息交給聊天視窗去顯示。否則,進入下一步。

(3)看當前的佇列中是否已經存在了目標好友的Cache,因為可能已經有了待提取的來自該好友的訊息了。如果已經存在這個Cache,則將訊息直接放入Cache,否則,為之新建一個,再放入。

(4)之所以要觸發UnhandleMessageOccured事件,是為了通知外面,有待提取的訊息出現了。比如,MainForm就會預定這個訊息,然後使得好友列表中對應的頭像閃動。

    void notifyIcon_UnhandleMessageOccured(UnhandleMessageType type, string friendOrGroupID)
    {
        if (type == UnhandleMessageType.Friend)
        {
            this.friendListBox1.SetTwinkleState(friendOrGroupID, true); 
            this.recentListBox1.SetTwinkleState(friendOrGroupID, false, true);
            return;
        }

        if (type == UnhandleMessageType.Group)
        {
            this.groupListBox.SetTwinkleState(friendOrGroupID, true);
            this.recentListBox1.SetTwinkleState(friendOrGroupID, true, true);
            return;
        }
    }

  上面的UnhandleMessageOccured事件處理函式,不僅僅使得好友列表中對應的頭像閃動,即使是最近聯絡人中,如果存在目標好友,也會使其頭像閃動。

(5)將托盤圖示設定為目標好友的頭像,並將ToolTip設定為好友的名稱及待提取的訊息數量。

(6)使用定時器閃動圖示。

2.點選托盤,提取訊息

  如果托盤正在閃動,表明有待提取的訊息,此時點選托盤,將提取佇列中最早壓入的好友的訊息。

    void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            lock (this.locker)
            {
                if (this.friendQueue.Count > 0)
                {
                    UnhandleFriendMessageBox cache = this.friendQueue[0];
                    this.friendQueue.RemoveAt(0);
                    IChatForm form = this.twinkleNotifySupporter.GetChatForm(cache.User);
                    if (form != null) //如果為null,表示剛刪除好友
                    {
                        form.HandleReceivedMessage(cache.MessageList);
                    }

                    this.DetectUnhandleMessage();

                    if (this.UnhandleMessageGone != null)
                    {
                        this.UnhandleMessageGone(UnhandleMessageType.Friend, cache.User);
                    }
                    return;
                }

                if (this.groupQueue.Count > 0)
                {
                    UnhandleGroupMessageBox cache = this.groupQueue[0];
                    this.groupQueue.RemoveAt(0);
                    IGroupChatForm form = this.twinkleNotifySupporter.GetGroupChatForm(cache.Group);
                    form.HandleReceivedMessage(cache.MessageList);

                    this.DetectUnhandleMessage();

                    if (this.UnhandleMessageGone != null)
                    {
                        this.UnhandleMessageGone(UnhandleMessageType.Group, cache.Group);
                    }
                    return;
                }
            }

            if (this.MouseClick != null)
            {
                this.MouseClick(sender, e);
            }
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message + " - " + ee.StackTrace);
        }
    }

(1)從上面程式碼執行的順序來看,是優先提取好友訊息,當所有的好友訊息提取完後,才提取群訊息。

(2)提取訊息時,會呼叫twinkleNotifySupporter的GetChatForm方法來建立與目標好友的聊天視窗,並將提取的訊息交給這個視窗去處理。

(3)當一個好友的訊息被提取後,會觸發UnhandleMessageGone事件,以通知外部訊息已經被提取了。比如,MainForm就會預定這個訊息,然後使得好友列表中對應的頭像不再閃動。

    void notifyIcon_UnhandleMessageGone(UnhandleMessageType type, string friendOrGroupID)
    {
        if (type == UnhandleMessageType.Friend)
        {
            this.friendListBox1.SetTwinkleState(friendOrGroupID, false); 
            this.recentListBox1.SetTwinkleState(friendOrGroupID, false, false);
            return;
        }

        if (type == UnhandleMessageType.Group)
        {
            this.groupListBox.SetTwinkleState(friendOrGroupID, false);
            this.recentListBox1.SetTwinkleState(friendOrGroupID, true, false);
            return;
        }
    }

(4)同時,會重新掃描佇列中待提取訊息的狀況,重設托盤圖示的狀態,這就是DetectUnhandleMessage方法做的事情。

3.重新判斷Queue中是否還有待提取的訊息,以設定托盤的狀態

  每當有訊息被提取後,我們都需要重新掃描Queue中是否還有其它的待提取訊息,DetectUnhandleMessage方法會被經常呼叫。

    private void DetectUnhandleMessage()
    {
        if (this.friendQueue.Count == 0 && this.groupQueue.Count == 0)
        {
            this.ControlTimer(false);
        }
        else if (this.friendQueue.Count > 0)
        {
            UnhandleFriendMessageBox cache = this.friendQueue[0];
            string userName = this.twinkleNotifySupporter.GetFriendName(cache.User);
            this.notifyIcon1.Text = string.Format("{0}({1})  {2}條訊息", cache.User, userName, cache.MessageList.Count);
            this.twinkleIcon = this.twinkleNotifySupporter.GetHeadIcon(cache.User);
        }
        else
        {
            UnhandleGroupMessageBox cache = this.groupQueue[0];
            string groupName = this.twinkleNotifySupporter.GetGroupName(cache.Group);
            this.notifyIcon1.Text = string.Format("{0}({1})  {2}條訊息", groupName, cache.Group, cache.MessageList.Count);
            this.twinkleIcon = this.twinkleNotifySupporter.GroupIcon;
        }
    }

(1)如果好友訊息的Queue和群訊息的Queue當中都沒有任何訊息了,則不再閃動托盤圖示。

(2)再依次掃描好友訊息的Queue和群訊息的Queue,如果發現還有待提取的訊息,則設定托盤圖示的影象,設定ToolTip,並開始閃動圖示。

三.GG V4.1 原始碼 

   下載最新版本,請轉到這裡。 

   

歡迎和我探討關於GG的一切,我的QQ:2027224508,多多交流!  

大家有什麼問題和建議,可以留言,也可以傳送email到我郵箱:ggim2013@163.com。 

如果你覺得還不錯,請粉我,順便再頂一下啊,呵呵  

相關文章