MSN是目前網路上廣泛使用的一個即時資訊交流工具(IM),筆者就常用它與同事或朋友聯系,但是在使用過程中發現缺乏一個群發資訊的功能,於是筆者尋思著自己編寫一個MSN資訊群發的軟體,在查閱了一番資料之後,終於寫出來了。下面大家和我一起動手來自己做一個MSN的資訊群發工具。
第一步:新建一個工程。啟動VB,選擇“文件”菜單的“新建”子菜單新建一個VB工程,系統回自動添加一個窗體,並且取名叫Form1。
第二步:添加MSN接口的引用。點擊VB的IDE環境的菜單中的工程菜單,在彈出的下拉方塊中選擇“引用(N)...”子菜單。在彈出的“引用”窗體中的“可用的引用”下拉清單中找到“Messenger API Type Library” 項,將起前面的鉤打上,然後關閉“引用”窗口。參見圖1

圖1
第三步:設定窗體,添加控件。首先在vb的工程管理器中連續按兩下Form1,打開窗體設計環境。選中窗體,將它的Caption值改為“MSN消息群發”。然後在窗體上添加控件,並且設定其初始屬性。要添加的控件的資訊見下表:
|
名稱
|
類型
|
Caption屬性的值
|
|
Label1
|
Label
|
群發對象:
|
|
Combo1
|
ComboBox
|
|
|
Check1
|
CheckBox
|
隻發送在線的
|
|
Label2
|
Label
|
消息內容:
|
|
Text1
|
TextBox
|
|
|
Command1
|
CommandButton
|
發送[&O]
|
|
Command2
|
CommandButton
|
退出[&X]
|
添加完控件後調整其位置,最後形成圖2的效果:

圖2
第四步:編寫代碼。
Dim m_MSG As New MessengerAPI.Messenger 'MSN的Com對象
Dim m_Groups As MessengerAPI.IMessengerGroups 'MSN中的分組
Dim m_Group As MessengerAPI.IMessengerGroup 'MSN中組的內容
Dim m_Contracts As MessengerAPI.IMessengerContacts 'MSN中的所有的好友的資訊
Dim m_Contract As MessengerAPI.IMessengerContact 'MSN中每個好友對象的內容
Private Sub Command1_Click()
Dim i As Integer
'偵測需要發送的資訊是否合法
If Trim(Text1.Text) = "" Then
MsgBox "發送的資訊不能為空!", vbInformation, "提示"
Text1.SetFocus
Exit Sub
End If
'判斷消息的發送對象是全部好友還是某個組的成員
If Combo1.ListIndex = 0 Then
Set m_Contracts = m_MSG.MyContacts
Else
Set m_Groups = m_MSG.MyGroups
Set m_Group = m_Groups.Item(Combo1.ListIndex - 1)
Set m_Contracts = m_Group.Contacts
End If
'遍歷要發送的對象,發送資訊
For i = 0 To m_Contracts.Count - 1
Set m_Contract = m_Contracts.Item(i)
If Check1.Value = 1 Then
If m_Contract.Status = 2 Then
m_MSG.InstantMessage m_Contract '打開要發送的好友窗體
DoEvents
SendKeys Text1.Text '寫入資訊
DoEvents
SendKeys "{enter}" '發送出資訊
DoEvents
SendKeys "%{F4}" '關閉好友窗口
End If
Else
m_MSG.InstantMessage m_Contract
DoEvents
SendKeys Text1.Text
DoEvents
SendKeys "{enter}"
DoEvents
SendKeys "%{F4}"
End If
Next i
'成功發送完畢資訊
If MsgBox("發送完畢!是否清空消息?", vbInformation + vbYesNo, "提示") = vbYes Then
Text1.Text = ""
Text1.SetFocus
Else
Text1.SetFocus
End If
End Sub
Private Sub Command2_Click()
Unload Me
End
End Sub
'初始化控件
Private Sub Form_Load()
Dim i As Integer
'初始化發送對象的下拉框
Set m_Groups = m_MSG.MyGroups
With Combo1
.AddItem "全部的組"
For i = 0 To m_Groups.Count - 1
Set m_Group = m_Groups.Item(i)
.AddItem m_Group.Name
Next i
.ListIndex = 0
End With
End Sub
'釋放變量
Private Sub Form_Unload(Cancel As Integer)
Set m_MSG = Nothing
Set m_Groups = Nothing
Set m_Group = Nothing
Set m_Contracts = Nothing
Set m_Contract = Nothing
End Sub
第五步:編譯運行。選擇“文件”菜單的產生“工程1.exe”菜單項,一個屬於你的MSN資訊群發軟體就完成了。運行這個exe介面如下:

文章定位: