用VB設(shè)計(jì)MSN信息群發(fā)軟件

字號:

MSN是目前網(wǎng)絡(luò)上廣泛使用的一個即時信息交流工具(IM),筆者就常用它與同事或朋友聯(lián)系,但是在使用過程中發(fā)現(xiàn)缺乏一個群發(fā)信息的功能,于是筆者尋思著自己編寫一個MSN信息群發(fā)的軟件,在查閱了一番資料之后,終于寫出來了。下面大家和我一起動手來自己做一個MSN的信息群發(fā)工具。
    第一步:新建一個工程。啟動VB,選擇“文件”菜單的“新建”子菜單新建一個VB工程,系統(tǒng)回自動添加一個窗體,并且取名叫Form1。
    第二步:添加MSN接口的引用。點(diǎn)擊VB的IDE環(huán)境的菜單中的工程菜單,在彈出的下拉菜單中選擇“引用(N)...”子菜單。在彈出的“引用”窗體中的“可用的引用”下拉列表中找到“Messenger API Type Library” 項(xiàng),將起前面的鉤打上,然后關(guān)閉“引用”窗口。
    第三步:設(shè)置窗體,添加控件。首先在VB的工程管理器中雙擊Form1,打開窗體設(shè)計(jì)環(huán)境。選中窗體,將它的Caption值改為“MSN消息群發(fā)”。然后在窗體上添加控件,并且設(shè)置其初始屬性。要添加的控件的信息見下表:
    名稱 類型 Caption屬性的值
    Label1 Label 群發(fā)對象:
    Combo1 ComboBox
    Check1 CheckBox 只發(fā)送在線的
    Label2 Label 消息內(nèi)容:
    Text1 TextBox
    Command1 CommandButton 發(fā)送[&O]
    Command2 CommandButton 退出[&X]
    添加完控件后調(diào)整其位置,
    第四步:編寫代碼。
    以下是引用片段:
    Dim m_MSG As New MessengerAPI.Messenger 'MSN的Com對象
    Dim m_Groups As MessengerAPI.IMessengerGroups 'MSN中的分組
    Dim m_Group As MessengerAPI.IMessengerGroup 'MSN中組的內(nèi)容
    Dim m_Contracts As MessengerAPI.IMessengerContacts 'MSN中的所有的好友的信息
    Dim m_Contract As MessengerAPI.IMessengerContact 'MSN中每個好友對象的內(nèi)容
    Private Sub Command1_Click()
    Dim i As Integer
    '檢測需要發(fā)送的信息是否合法
    If Trim(Text1.Text) = "" Then
    MsgBox "發(fā)送的信息不能為空!", VBInformation, "提示"
    Text1.SetFocus
    Exit Sub
    End If
    '判斷消息的發(fā)送對象是全部好友還是某個組的成員
    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
    '遍歷要發(fā)送的對象,發(fā)送信息
    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 '打開要發(fā)送的好友窗體
    DoEvents
    SendKeys Text1.Text '寫入信息
    DoEvents
    SendKeys "{enter}" '發(fā)送出信息
    DoEvents
    SendKeys "%{F4}" '關(guān)閉好友窗口
    End If
    Else
    m_MSG.InstantMessage m_Contract
    DoEvents
    SendKeys Text1.Text
    DoEvents
    SendKeys "{enter}"
    DoEvents
    SendKeys "%{F4}"
    End If
    Next i
    '成功發(fā)送完畢信息
    If MsgBox("發(fā)送完畢!是否清空消息?", 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
    '初始化發(fā)送對象的下拉框
    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
    第五步:編譯運(yùn)行。選擇“文件”菜單的生成“工程1.exe”菜單項(xiàng),一個屬于你的MSN信息群發(fā)軟件就完成了。