當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。
如果你覺得還不錯,請粉我,順便再頂一下啊,呵呵