⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Dot Msn Sample
💻 CS
📖 第 1 页 / 共 2 页
字号:
/* 
 * 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.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);			

			// 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)
		{
			MessageBox.Show(this, 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.
			sender.SendMessage("Hello world!");
		}
		
		

		#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.SuspendLayout();
			// 
			// Log
			// 
			this.Log.Location = new System.Drawing.Point(16, 224);
			this.Log.Multiline = true;
			this.Log.Name = "Log";
			this.Log.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.Log.Size = new System.Drawing.Size(320, 88);
			this.Log.TabIndex = 0;
			this.Log.Text = "";
			// 
			// StartButton
			// 
			this.StartButton.Location = new System.Drawing.Point(120, 376);
			this.StartButton.Name = "StartButton";
			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(88, 320);
			this.mailTextBox.Name = "mailTextBox";
			this.mailTextBox.Size = new System.Drawing.Size(248, 20);
			this.mailTextBox.TabIndex = 2;
			this.mailTextBox.Text = "yourmail@hotmail.com";
			// 
			// passTextBox
			// 
			this.passTextBox.Location = new System.Drawing.Point(88, 344);
			this.passTextBox.Name = "passTextBox";
			this.passTextBox.PasswordChar = '*';
			this.passTextBox.Size = new System.Drawing.Size(248, 20);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -