• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张继东
看铁蹄铮铮,踏遍万里河山。磨不破,打不烂。
   首页    新随笔    联系   管理    订阅  订阅

关于dotmsn组件在msn中的应用

很早就想弄个及时通讯的东西。也下了一个,自己也加了些东西,慢慢扩充嘛。

/*
 * dotMSN example
 * Last modified : 22 March 2004
 * Created by    : Bas Geertsema, Xih Productions <orphix@planet.nl>
 * DotMSN site  : http://members.home.nl/b.geertsema/dotMSN
 *
 * **** A short note about sending messages ****
 *
 * The 'workflow' of sending messages is:
 * - Request a conversation through Messenger.RequestConversation(..)
 *  You can setup eventhandlers to the Conversation object you receive from this function.
 * - Catch the Messenger.ContactJoined(..) event. After the first contact has joined you can start
 *  sending messages. Sending messages earlier in this process will fail.
 * - Use Conversation.SendMessage(..) to send messages to all other participants in the conversation
 *
 * When someone invites you:
 * - Catch the Messenger.ConversationCreated(..) event. In this event both parties have agreed
 *  to create a conversation and therefore a DotMSN.Conversation object is created which you can initalize.
 *  Note that this event is also called when you send the conversation request.
 *
 * **** About file transfers ****
 *
 * You can only send file transfers after you have established a conversation. You can check Messenger.Conversations
 * to check whether you a conversation with the contactperson already exists or you have to create a new conversation.
 *
 * I hope this example will provide a good basic understanding of how DotMSN works. If you come across bugs, drop me a mail.
 */

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using DotMSN;

namespace dotMSNFramework
{
 public class Form1 : System.Windows.Forms.Form
 {
  #region Variables
  private System.Windows.Forms.TextBox Log;
  private System.Windows.Forms.Button StartButton;
  private System.Windows.Forms.TextBox mailTextBox;
  private System.Windows.Forms.TextBox passTextBox;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Button showlistButton;
  private System.Windows.Forms.ListView contactListView;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Button SendFileButton;
  private System.Windows.Forms.TextBox tbxMysay;
  private System.Windows.Forms.Button btnSay;
  private System.Windows.Forms.StatusBar statusBarMain;
  private System.Windows.Forms.StatusBarPanel statusBarPanel1;
  private System.Windows.Forms.StatusBarPanel statusBarPanel2;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Button btnAddPerson;
  private System.Windows.Forms.TextBox tbxPersonEmail;
  private System.Windows.Forms.TextBox tbxGroup;
  private System.Windows.Forms.Button btnAddGroup;

  private System.ComponentModel.Container components = null;
  #endregion
  
  #region Standard windows.forms code
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();   
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #endregion


  // this object will be the interface to the dotMSN library
  private DotMSN.Messenger messenger = new Messenger();

  // Called when the button 'Connected' is clicked
  private void StartMSN()
  {      
   messenger = new Messenger();
   try
   {    
    // make sure we don't use the default settings, since they're invalid
    if(mailTextBox.Text == "yourmail@hotmail.com")
     MessageBox.Show(this, "Fill in your own passport details to connect to the messenger service");
    else
    {
     // setup the callbacks
     // we log when someone goes online
     messenger.ContactOnline += new Messenger.ContactOnlineHandler(ContactOnline);

     // we want to do something when we have a conversation
     messenger.ConversationCreated += new Messenger.ConversationCreatedHandler(ConversationCreated);

     // notify us when synchronization is completed
     messenger.SynchronizationCompleted += new Messenger.SynchronizationCompletedHandler(OnSynchronizationCompleted);

     // everything is setup, now connect to the messenger service
     messenger.Connect(mailTextBox.Text, passTextBox.Text);     
     Log.Text += "Connected!\r\n";

     // synchronize the whole list.
     // remember you can only do this once per session!
     // after synchronizing the initial status will be set.
     messenger.SynchronizeList();
     

     /* uncomment this when you want to automatically add
      * people who have added you to their contactlist on your own
      * contactlist. (remember the pop-up dialog in MSN Messenger client when someone adds you, this is the 'automatic' method)     
     foreach(Contact contact in
      messenger.GetListEnumerator(MSNList.ReverseList))
     {      
      messenger.AddContact(contact.Mail);
     }
     */
    }
   }
   catch(MSNException e)
   {
    // in case of an error, report this to the user (or developer)
    MessageBox.Show(this, "Connecting failed: " + e.ToString());
   }   

  }

  /// <summary>
  /// When the MSN server responds we can setup a conversation (the other party agreed)
  /// the Messenger.ConversationCreated event is called so we can initialize the
  /// Conversation object.
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ConversationCreated(Messenger sender, ConversationEventArgs e)
  {
   // we request a conversation or were asked one. Now log this
   Log.Text += "Conversation object created\r\n";

   // remember there are not yet users in the conversation (except ourselves)
   // they will join _after_ this event. We create another callback to handle this.
   // When user(s) have joined we can start sending messages.
   e.Conversation.ContactJoin += new Conversation.ContactJoinHandler(ContactJoined);   

   // log the event when the two clients are connected
   e.Conversation.ConnectionEstablished += new Conversation.ConnectionEstablishedHandler(ConnectionEstablished);

   // notify us when the other contact is typing something
   
   e.Conversation.UserTyping  += new Conversation.UserTypingHandler(ContactTyping);
   
  //notify and log the message that we received

   e.Conversation.MessageReceived+=new DotMSN.Conversation.MessageReceivedHandler(Conversation_MessageReceived);
   // we want to be accept filetransfer invitations
   e.Conversation.FileTransferHandler.InvitationReceived +=new DotMSN.FileTransferHandler.FileTransferInvitationHandler(FileTransferHandler_FileTransferInvitation);
  }

  /// <summary>
  /// Log when the connection is actually established between the two clients.
  /// You can not yet send messages, the other contact must join first (if you have initiated the conversation)
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ConnectionEstablished(Conversation sender, EventArgs e)
  {
   Log.Text += "connection established.\r\n";
  }

  // this is actually just annoying but it proves the concept
  private void ContactTyping(Conversation sender, ContactEventArgs e)
  {
   statusBarMain.Panels[0].Text=e.Contact.Name + " is typing!";
  }

  // log the event when a contact goed online
  private void ContactOnline(Messenger sender, ContactEventArgs e)
  {
   Log.Text += e.Contact.Name + " went online\r\n";
  }

  /// <summary>
  /// After the first contact has joined you can actually send messages to the
  /// other contact(s)!
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ContactJoined(Conversation sender, ContactEventArgs e)
  {
   // someone joined our conversation! remember that this also occurs when you are
   // only talking to 1 other person. Log this event.
   Log.Text += e.Contact.Name + " joined the conversation.\r\n";
   // now say something back. You can send messages using the Conversation object.
   string mySay=tbxMysay.Text.Trim();
   sender.SendMessage(mySay);
  }
  
  

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.Log = new System.Windows.Forms.TextBox();
   this.StartButton = new System.Windows.Forms.Button();
   this.mailTextBox = new System.Windows.Forms.TextBox();
   this.passTextBox = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.showlistButton = new System.Windows.Forms.Button();
   this.contactListView = new System.Windows.Forms.ListView();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.SendFileButton = new System.Windows.Forms.Button();
   this.tbxMysay = new System.Windows.Forms.TextBox();
   this.btnSay = new System.Windows.Forms.Button();
   this.statusBarMain = new System.Windows.Forms.StatusBar();
   this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
   this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
   this.label5 = new System.Windows.Forms.Label();
   this.btnAddPerson = new System.Windows.Forms.Button();
   this.tbxPersonEmail = new System.Windows.Forms.TextBox();
   this.tbxGroup = new System.Windows.Forms.TextBox();
   this.btnAddGroup = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
   this.SuspendLayout();
   //
   // Log
   //
   this.Log.Location = new System.Drawing.Point(19, 232);
   this.Log.Multiline = true;
   this.Log.Name = "Log";
   this.Log.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.Log.Size = new System.Drawing.Size(384, 95);
   this.Log.TabIndex = 0;
   this.Log.Text = "";
   //
   // StartButton
   //
   this.StartButton.Location = new System.Drawing.Point(144, 392);
   this.StartButton.Name = "StartButton";
   this.StartButton.Size = new System.Drawing.Size(90, 25);
   this.StartButton.TabIndex = 1;
   this.StartButton.Text = "Connect";
   this.StartButton.Click += new System.EventHandler(this.StartButton_Click);
   //
   // mailTextBox
   //
   this.mailTextBox.Location = new System.Drawing.Point(106, 336);
   this.mailTextBox.Name = "mailTextBox";
   this.mailTextBox.Size = new System.Drawing.Size(297, 21);
   this.mailTextBox.TabIndex = 2;
   this.mailTextBox.Text = "zjd157@hotmail.com";
   //
   // passTextBox
   //
   this.passTextBox.Location = new System.Drawing.Point(106, 360);
   this.passTextBox.Name = "passTextBox";
   this.passTextBox.PasswordChar = '*';
   this.passTextBox.Size = new System.Drawing.Size(297, 21);
   this.passTextBox.TabIndex = 3;
   this.passTextBox.Text = "shandongdi";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(19, 336);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(67, 17);
   this.label1.TabIndex = 4;
   this.label1.Text = "Mail";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(19, 360);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(67, 18);
   this.label2.TabIndex = 5;
   this.label2.Text = "Password";
   //
   // showlistButton
   //
   this.showlistButton.Location = new System.Drawing.Point(250, 392);
   this.showlistButton.Name = "showlistButton";
   this.showlistButton.Size = new System.Drawing.Size(144, 25);
   this.showlistButton.TabIndex = 6;
   this.showlistButton.Text = "Show online contacts";
   this.showlistButton.Click += new System.EventHandler(this.showlistButton_Click);
   //
   // contactListView
   //
   this.contactListView.Location = new System.Drawing.Point(19, 26);
   this.contactListView.MultiSelect = false;
   this.contactListView.Name = "contactListView";
   this.contactListView.Size = new System.Drawing.Size(384, 110);
   this.contactListView.TabIndex = 7;
   this.contactListView.View = System.Windows.Forms.View.List;
   this.contactListView.DoubleClick += new System.EventHandler(this.contactListView_DoubleClick);
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(19, 9);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(317, 17);
   this.label3.TabIndex = 8;
   this.label3.Text = "Contactlist (double-click to send message)";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(19, 208);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(120, 17);
   this.label4.TabIndex = 9;
   this.label4.Text = "Log";
   //
   // SendFileButton
   //
   this.SendFileButton.Location = new System.Drawing.Point(424, 248);
   this.SendFileButton.Name = "SendFileButton";
   this.SendFileButton.Size = new System.Drawing.Size(90, 25);
   this.SendFileButton.TabIndex = 10;
   this.SendFileButton.Text = "Send file";
   this.SendFileButton.Click += new System.EventHandler(this.SendFileButton_Click);
   //
   // tbxMysay
   //
   this.tbxMysay.Location = new System.Drawing.Point(16, 144);
   this.tbxMysay.Multiline = true;
   this.tbxMysay.Name = "tbxMysay";
   this.tbxMysay.Size = new System.Drawing.Size(272, 32);
   this.tbxMysay.TabIndex = 11;
   this.tbxMysay.Text = "";
   this.tbxMysay.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxMysay_KeyDown);
   //
   // btnSay
   //
   this.btnSay.Location = new System.Drawing.Point(344, 152);
   this.btnSay.Name = "btnSay";
   this.btnSay.Size = new System.Drawing.Size(56, 23);
   this.btnSay.TabIndex = 12;
   this.btnSay.Text = "Say";
   this.btnSay.Click += new System.EventHandler(this.btnSay_Click);
   //
   // statusBarMain
   //
   this.statusBarMain.Location = new System.Drawing.Point(0, 420);
   this.statusBarMain.Name = "statusBarMain";
   this.statusBarMain.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                        this.statusBarPanel1,
                        this.statusBarPanel2});
   this.statusBarMain.ShowPanels = true;
   this.statusBarMain.Size = new System.Drawing.Size(648, 22);
   this.statusBarMain.TabIndex = 13;
   this.statusBarMain.Text = "hello";
   //
   // statusBarPanel1
   //
   this.statusBarPanel1.Text = "666";
   this.statusBarPanel1.ToolTipText = "ttt";
   //
   // statusBarPanel2
   //
   this.statusBarPanel2.Text = "777";
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(160, 184);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(160, 23);
   this.label5.TabIndex = 14;
   this.label5.Text = "press Enter key to send!";
   //
   // btnAddPerson
   //
   this.btnAddPerson.Location = new System.Drawing.Point(424, 120);
   this.btnAddPerson.Name = "btnAddPerson";
   this.btnAddPerson.Size = new System.Drawing.Size(96, 23);
   this.btnAddPerson.TabIndex = 15;
   this.btnAddPerson.Text = "add a person";
   this.btnAddPerson.Click += new System.EventHandler(this.btnAddPerson_Click);
   //
   // tbxPersonEmail
   //
   this.tbxPersonEmail.Location = new System.Drawing.Point(424, 88);
   this.tbxPersonEmail.Name = "tbxPersonEmail";
   this.tbxPersonEmail.Size = new System.Drawing.Size(192, 21);
   this.tbxPersonEmail.TabIndex = 16;
   this.tbxPersonEmail.Text = "";
   //
   // tbxGroup
   //
   this.tbxGroup.Location = new System.Drawing.Point(424, 160);
   this.tbxGroup.Name = "tbxGroup";
   this.tbxGroup.Size = new System.Drawing.Size(192, 21);
   this.tbxGroup.TabIndex = 18;
   this.tbxGroup.Text = "";
   //
   // btnAddGroup
   //
   this.btnAddGroup.Location = new System.Drawing.Point(424, 192);
   this.btnAddGroup.Name = "btnAddGroup";
   this.btnAddGroup.Size = new System.Drawing.Size(96, 23);
   this.btnAddGroup.TabIndex = 17;
   this.btnAddGroup.Text = "add a group";
   this.btnAddGroup.Click += new System.EventHandler(this.btnAddGroup_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(648, 442);
   this.Controls.Add(this.tbxGroup);
   this.Controls.Add(this.btnAddGroup);
   this.Controls.Add(this.tbxPersonEmail);
   this.Controls.Add(this.btnAddPerson);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.statusBarMain);
   this.Controls.Add(this.btnSay);
   this.Controls.Add(this.tbxMysay);
   this.Controls.Add(this.SendFileButton);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.contactListView);
   this.Controls.Add(this.showlistButton);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.passTextBox);
   this.Controls.Add(this.mailTextBox);
   this.Controls.Add(this.Log);
   this.Controls.Add(this.StartButton);
   this.Name = "Form1";
   this.Text = "dotMSN example";
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  private void StartButton_Click(object sender, System.EventArgs e)
  {
   // Start our journey
   StartMSN();
  }

  /// <summary>
  /// The user clicks on the button to show all online contacts in the contact list
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void showlistButton_Click(object sender, System.EventArgs e)
  {
   FillListview();
  }

  /// <summary>
  /// Fill the listview with the users who are online. By double-clicking on a listview-item
  /// a conversation is created with the corresponding contact.
  /// </summary>
  private void FillListview()
  {
   contactListView.Clear();
   foreach(Contact contact in messenger.GetListEnumerator(MSNList.ForwardList))
   {
    
    // if the contact is not offline we can send messages and we want to show
    // it in the contactlistview
    if(contact.Status != MSNStatus.Offline)
    {
     // add this contact to the listview,
     // we 'tag' the listitem with the contact object
     ListViewItem item = new ListViewItem(contact.Name);
     item.Tag    = contact;
     contactListView.Items.Add(item);
    }
   }
  }

  /// <summary>
  /// Called when the synchronization is completed. When this happens
  /// we want to fill the listbox on the form.
  /// </summary>
  /// <param name="sender">The messenger object</param>
  /// <param name="e">Contains nothing important</param>
  private void OnSynchronizationCompleted(Messenger sender, EventArgs e)
  {
   // first show all people who are on our forwardlist. This is the 'main' contactlist
   // a normal person would see when logging in.
   // if you want to get all 'online' people enumerate trough this list and extract
   // all contacts with the right DotMSN.MSNStatus  (eg online/away/busy)
   foreach(Contact contact in messenger.GetListEnumerator(MSNList.ForwardList))
   {     
    this.Log.Text += "FL > " + contact.Name + " (" + contact.Status + ")\r\n";
    FillListview();
   }

   // now get the reverse list. This list shows all people who have you on their
   // contactlist.
   foreach(Contact contact in messenger.ReverseList)
   {
    this.Log.Text += "RL > " + contact.Name + " (" + contact.Status + ")\r\n";
   }

   // we follow with the blocked list. this shows all the people who are blocked
   // by you
   foreach(Contact contact in messenger.BlockedList)
   {
    this.Log.Text += "BL > " + contact.Name + " (" + contact.Status + ")\r\n";
   }

   // when the privacy of the client is set to MSNPrivacy.NoneButAllowed then only
   // the contacts on the allowedlist are able to see your status
   foreach(Contact contact in messenger.AllowedList)
   {
    this.Log.Text += "AL > " + contact.Name + " (" + contact.Status + ")\r\n";
   }

   // now set our initial status !
   // we must set an initial status otherwise
   messenger.SetStatus(MSNStatus.Online);
   // we an use  say 'messenger.SetStatus(MSNStatus.Busy);'  
   Log.Text += "Status set to online!\r\n";


   /* the alllist just enumerates all possible contacts. it is not very usefull in
     * this sample so i've commented it out.
    foreach(Contact contact in messenger.AllList)
    {
     this.Log.Text += "AL > " + contact.Name + " (" + contact.Status + ")";
    } */
  }

  /// <summary>
  /// Called when the user doubleclicks on a contact in the contactlist.
  /// We will send a message to the contact which have been selected.
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void contactListView_DoubleClick(object sender, System.EventArgs e)
  {
//   // when this contact we inserted in the listview we let the 'tag' property
//   // point to the corresponsing Contact object of the dotMSN library.
//   Contact contact = (DotMSN.Contact)contactListView.SelectedItems[0].Tag;
//
//   // Do the conversation request. You can optionally define clientdata,
//   // for example your own conversation object, such that the request can
//   // be identified in events later on.
//   messenger.RequestConversation(contact.Mail);   
  }

  private void FileTransferHandler_FileTransferInvitation(FileTransferHandler sender, FileTransferInvitationEventArgs e)
  {
   // We want to accept it. You can check the e.FileTransfer object for more info.   
   e.Accept = true;
   // uncomment this to save the file to your C-drive
   e.FileTransfer.ReceiveStream = new FileStream("C:\\" + e.FileTransfer.FileName, FileMode.Create, FileAccess.ReadWrite);
   Log.Text += e.FileTransfer.Sender.Name+":"+"Invited filetransfer " + e.FileTransfer.FileName + "\r\n";
  }

  
  #region Filetransfer code

  /// <summary>
  /// When a contact is selected in the listview a file-dialog will be presented
  /// and a file will be send to the contact.
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void SendFileButton_Click(object sender, System.EventArgs e)
  {
   if(contactListView.SelectedItems.Count == 0) return;

   FileDialog dialog = new OpenFileDialog();
   if(dialog.ShowDialog() != DialogResult.OK)
    return;

   // when this contact we inserted in the listview we let the 'tag' property
   // point to the corresponsing Contact object of the dotMSN library.
   Contact contact = (DotMSN.Contact)contactListView.SelectedItems[0].Tag;

   // first check whether we already have a session we can capture to send the file
   foreach(Conversation conversation in messenger.Conversations)
   {
    if(conversation.Users.ContainsKey(contact.Mail))
    {
     SendFile(conversation, contact, dialog.FileName);
     return;
    }     
   }

   // we have to create a conversation (session) before we can send a file
   Conversation newConversation = messenger.RequestConversation(contact.Mail, dialog.FileName);
   
   // set this callback so we can start the file as soon as the other contact has joined the conversation
   newConversation.ContactJoin +=new DotMSN.Conversation.ContactJoinHandler(filetransferConversation_ContactJoin);
  }

  /// <summary>
  /// Called when a session/conversation has been created in order to send a file. After the contact has joined
  /// we are going to send the file which is stored in the Conversation.ClientData field.
  /// </summary>
  private void filetransferConversation_ContactJoin(Conversation sender, ContactEventArgs e)
  {
   SendFile(sender, e.Contact, (string)sender.ClientData);
  }

  /// <summary>
  /// Send the file to the specified conversation/contact combination
  /// </summary>  
  private void SendFile(Conversation conversation, Contact contact, string filename)
  {
   conversation.FileTransferHandler.TransferFile(contact.Mail, filename);
  }

  #endregion

  private void btnSay_Click(object sender, System.EventArgs e)
  {
    sendMessage();
  }
  private void sendMessage()
  {
   if(contactListView.SelectedItems.Count==0)
   {
    MessageBox.Show("请选择对象!");
    return;
   }
   else
   {
    // when this contact we inserted in the listview we let the 'tag' property
    // point to the corresponsing Contact object of the dotMSN library.
    Contact contact = (DotMSN.Contact)contactListView.SelectedItems[0].Tag;

    // Do the conversation request. You can optionally define clientdata,
    // for example your own conversation object, such that the request can
    // be identified in events later on.
    messenger.RequestConversation(contact.Mail);
   }
  }
/// <summary>
///  when received message
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
  private void Conversation_MessageReceived(Conversation sender, MessageEventArgs e)
  {
   Log.Text = e.Sender.Name+" say:"+e.Message.Text+"\r\n"+Log.Text.ToString();
  }

  private void tbxMysay_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
   if(e.KeyCode==Keys.Enter)
   {
     sendMessage();
   }
  }
/// <summary>
/// add a person
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
  private void btnAddPerson_Click(object sender, System.EventArgs e)
  {
   string strMail=tbxPersonEmail.Text.Trim();
   messenger.AddContact(strMail);
  }
/// <summary>
/// add a group
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
  private void btnAddGroup_Click(object sender, System.EventArgs e)
  {
   string strGroup=tbxGroup.Text.Trim();
   messenger.AddGroup(strGroup);
  }
 }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//其实完全把它弄成一个类似木马的东西。
//因为当有信息传过来的时候
//private void Conversation_MessageReceived(Conversation sender, MessageEventArgs e)
//  {
//   Log.Text = e.Sender.Name+" say:"+e.Message.Text+"\r\n"+Log.Text.ToString();
//  }
//就会被调用,
//完全可以在这里做文章
//我改为:
///// <summary>
///  when received message
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
  private void Conversation_MessageReceived(Conversation sender, MessageEventArgs e)
  {
   string strRevM=e.Message.Text;
   Log.Text = e.Sender.Name+" say:"+strRevM+"\r\n"+Log.Text.ToString();
   if(strRevM=="open")
   {
    dosomething();
   }
  }
  /// <summary>
  /// start do something
  /// </summary>
  ///private void dosomething()
  ///{
  /// System.Diagnostics. Process.Start("IExplore.exe", "http://cnblogs.com/zhangjidong");
 /// }
//当对方信息是“open”的时候,用ie打开一个网站。当然还可以干些别的了。

posted @ 2005-06-10 11:10  简单生活  阅读(1220)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3