作者:[转载]  文章来源:http://www.alixixi.com/  更新时间:2006-3-19

  MSN是目前网络上广泛使用的一个即时信息交流工具(IM),笔者就常用它与同事或朋友联系,但是在使用过程中发现缺乏一个群发信息的功能,于是笔者寻思着自己编写一个MSN信息群发的软件,在查阅了一番资料之后,终于写出来了。下面大家和我一起动手来自己做一个MSN的信息群发工具。

  第一步:新建一个工程。启动VB,选择“文件”菜单的“新建”子菜单新建一个VB工程,系统回自动添加一个窗体,并且取名叫Form1。

  第二步:添加MSN接口的引用。点击VB的IDE环境的菜单中的工程菜单,在弹出的下拉菜单中选择“引用(N)...”子菜单。在弹出的“引用”窗体中的“可用的引用”下拉列表中找到“Messenger API Type Library” 项,将起前面的钩打上,然后关闭“引用”窗口。

  第三步:设置窗体,添加控件。首先在vb的工程管理器中双击Form1,打开窗体设计环境。选中窗体,将它的Caption值改为“MSN消息群发”。然后在窗体上添加控件,并且设置其初始属性。要添加的控件的信息见下表:

  名称 类型 Caption属性的值

  Label1 Label 群发对象:

  Combo1 ComboBox

  Check1 CheckBox 只发送在线的

  Label2 Label 消息内容:

  Text1 TextBox

  Command1 CommandButton 发送[&O]

  Command2 CommandButton 退出[&X]

  添加完控件后调整其位置,

  第四步:编写代码。

  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信息群发软件就完成了。

网友评论
相关搜索
阿里西西Baidu.com搜索