📄 form1.cs
字号:
this.passTextBox.TabIndex = 3;
this.passTextBox.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 320);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 4;
this.label1.Text = "Mail";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 344);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 5;
this.label2.Text = "Password";
//
// showlistButton
//
this.showlistButton.Location = new System.Drawing.Point(208, 376);
this.showlistButton.Name = "showlistButton";
this.showlistButton.Size = new System.Drawing.Size(120, 23);
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(16, 24);
this.contactListView.MultiSelect = false;
this.contactListView.Name = "contactListView";
this.contactListView.Size = new System.Drawing.Size(320, 136);
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(16, 8);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(264, 16);
this.label3.TabIndex = 8;
this.label3.Text = "Contactlist (double-click to send message)";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 208);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 16);
this.label4.TabIndex = 9;
this.label4.Text = "Log";
//
// SendFileButton
//
this.SendFileButton.Location = new System.Drawing.Point(16, 168);
this.SendFileButton.Name = "SendFileButton";
this.SendFileButton.TabIndex = 10;
this.SendFileButton.Text = "Send file";
this.SendFileButton.Click += new System.EventHandler(this.SendFileButton_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(344, 411);
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";
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);
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 += "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
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -