📄 winchatform.cs
字号:
/* File : winChatForm.cs
* Namespace : WinChatFormLibrary
* Classes : WinChatForm
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ClientServer;
namespace WinChatFormLibrary
{
/* Class : WinChatForm
* Base Class : System.Windows.Forms.Form
* Functionality : Handles all the Windows Form Controls for this program.
*/
/// <summary>
/// Summary description for Form1.
/// </summary>
public class WinChatForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.TextBox remoteTextBox;
private System.Windows.Forms.TextBox localTextBox;
private System.Windows.Forms.Label localMessageLabel;
private System.Windows.Forms.Label remoteMessageLabel;
private System.Windows.Forms.TextBox hostNameTextBox;
private System.Windows.Forms.TextBox typedMessage;
private System.Windows.Forms.MenuItem FileMenuItem;
private System.Windows.Forms.MenuItem closeMenuItem;
private System.Windows.Forms.MenuItem helpMenuItem;
private System.Windows.Forms.MenuItem aboutMenuItem;
private System.Windows.Forms.Button sendButton;
private System.Windows.Forms.Button exitButton;
private System.Windows.Forms.Button doneButton;
private System.Windows.Forms.Button notifyButton;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label TypedMessageLabel;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Label hostNameLabel;
private Thread remoteTextBoxThread;
private bool isCallingSide = false;
private bool invokedFromExitButton = false;
private enum whoIsCalling
{
exitButton,
hangupButton
}
public TcpUdpServer tcpUdpServer;
public TcpUdpClient tcpUdpClient;
public delegate void CallInitiationHandler();
public static event CallInitiationHandler callInitiation;
public static event CallInitiationHandler callEnding;
public WinChatForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
tcpUdpServer = new TcpUdpServer();
tcpUdpServer.start_servers();
tcpUdpClient = new TcpUdpClient();
//Notification Event Handler.
//Listen to the events TcpUdpServer.peerNotify and TcpUdpServer.peerHangup. Invokes
//OnPeerNotify and OnPeerHangup when those events happen.
TcpUdpServer.peerNotify += new TcpUdpServer.NotificationHandler(this.OnPeerNotify);
TcpUdpServer.peerHangup += new TcpUdpServer.NotificationHandler(this.OnPeerHangup);
//Call Initiation Event Handler.
//Listen to the events WinChatForm.callInitiation and WinChatForm.callEnding.
//Invokes tcpUdpServer.OnCallInitiation and tcpUdpServer.OnCallEnding when those
//events happen.
WinChatForm.callInitiation += new CallInitiationHandler(tcpUdpServer.OnCallInitiation);
WinChatForm.callEnding += new CallInitiationHandler(tcpUdpServer.OnCallEnding);
remoteTextBoxThread = new Thread(new ThreadStart(displayRemoteMessage));
remoteTextBoxThread.IsBackground = true;
remoteTextBoxThread.Start();
Application.ApplicationExit += new EventHandler(Form_OnExit);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#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.components = new System.ComponentModel.Container();
this.typedMessage = new System.Windows.Forms.TextBox();
this.TypedMessageLabel = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.FileMenuItem = new System.Windows.Forms.MenuItem();
this.closeMenuItem = new System.Windows.Forms.MenuItem();
this.helpMenuItem = new System.Windows.Forms.MenuItem();
this.aboutMenuItem = new System.Windows.Forms.MenuItem();
this.notifyButton = new System.Windows.Forms.Button();
this.sendButton = new System.Windows.Forms.Button();
this.remoteTextBox = new System.Windows.Forms.TextBox();
this.doneButton = new System.Windows.Forms.Button();
this.localMessageLabel = new System.Windows.Forms.Label();
this.hostNameLabel = new System.Windows.Forms.Label();
this.hostNameTextBox = new System.Windows.Forms.TextBox();
this.exitButton = new System.Windows.Forms.Button();
this.localTextBox = new System.Windows.Forms.TextBox();
this.remoteMessageLabel = new System.Windows.Forms.Label();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// typedMessage
//
this.typedMessage.Enabled = false;
this.typedMessage.Location = new System.Drawing.Point(24, 136);
this.typedMessage.Multiline = true;
this.typedMessage.Name = "typedMessage";
this.typedMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.typedMessage.Size = new System.Drawing.Size(440, 40);
this.typedMessage.TabIndex = 2;
this.typedMessage.Text = "Type Your Message Here.";
this.toolTip1.SetToolTip(this.typedMessage, "Enter your text message here.");
//
// TypedMessageLabel
//
this.TypedMessageLabel.AutoSize = true;
this.TypedMessageLabel.Location = new System.Drawing.Point(24, 112);
this.TypedMessageLabel.Name = "TypedMessageLabel";
this.TypedMessageLabel.Size = new System.Drawing.Size(133, 13);
this.TypedMessageLabel.TabIndex = 7;
this.TypedMessageLabel.Text = "Type Your Message Here";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.FileMenuItem,
this.helpMenuItem});
//
// FileMenuItem
//
this.FileMenuItem.Index = 0;
this.FileMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.closeMenuItem});
this.FileMenuItem.Text = "File";
//
// closeMenuItem
//
this.closeMenuItem.Index = 0;
this.closeMenuItem.Text = "Close";
this.closeMenuItem.Click += new System.EventHandler(this.menuItem8_Click);
//
// helpMenuItem
//
this.helpMenuItem.Index = 1;
this.helpMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.aboutMenuItem});
this.helpMenuItem.Text = "Help";
//
// aboutMenuItem
//
this.aboutMenuItem.Index = 0;
this.aboutMenuItem.Text = "About";
this.aboutMenuItem.Click += new System.EventHandler(this.aboutMenuItem_Click);
//
// notifyButton
//
this.notifyButton.Location = new System.Drawing.Point(248, 56);
this.notifyButton.Name = "notifyButton";
this.notifyButton.TabIndex = 1;
this.notifyButton.Text = "Notify";
this.toolTip1.SetToolTip(this.notifyButton, "Press this button to notify the remote side that you want to chat.");
this.notifyButton.Click += new System.EventHandler(this.notifyButton_Click);
//
// sendButton
//
this.sendButton.Enabled = false;
this.sendButton.Location = new System.Drawing.Point(200, 208);
this.sendButton.Name = "sendButton";
this.sendButton.TabIndex = 3;
this.sendButton.Text = "SEND!";
this.toolTip1.SetToolTip(this.sendButton, "Press this button when you want to send the text message to the remote side.");
this.sendButton.Click += new System.EventHandler(this.sendButton_Click);
//
// remoteTextBox
//
this.remoteTextBox.Location = new System.Drawing.Point(24, 400);
this.remoteTextBox.Multiline = true;
this.remoteTextBox.Name = "remoteTextBox";
this.remoteTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.remoteTextBox.Size = new System.Drawing.Size(440, 120);
this.remoteTextBox.TabIndex = 5;
this.remoteTextBox.Text = "Remote Side Message Appeared Here.";
this.toolTip1.SetToolTip(this.remoteTextBox, "Remote side\'s text message will appear here.");
//
// doneButton
//
this.doneButton.Enabled = false;
this.doneButton.Location = new System.Drawing.Point(88, 536);
this.doneButton.Name = "doneButton";
this.doneButton.TabIndex = 6;
this.doneButton.Text = "Hangup";
this.toolTip1.SetToolTip(this.doneButton, "Press this button when you want to hangup the call.");
this.doneButton.Click += new System.EventHandler(this.doneButton_Click);
//
// localMessageLabel
//
this.localMessageLabel.AutoSize = true;
this.localMessageLabel.ForeColor = System.Drawing.SystemColors.ControlText;
this.localMessageLabel.Location = new System.Drawing.Point(32, 216);
this.localMessageLabel.Name = "localMessageLabel";
this.localMessageLabel.Size = new System.Drawing.Size(80, 13);
this.localMessageLabel.TabIndex = 2;
this.localMessageLabel.Text = "Youe Message";
//
// hostNameLabel
//
this.hostNameLabel.AutoSize = true;
this.hostNameLabel.ForeColor = System.Drawing.SystemColors.ControlText;
this.hostNameLabel.Location = new System.Drawing.Point(24, 32);
this.hostNameLabel.Name = "hostNameLabel";
this.hostNameLabel.Size = new System.Drawing.Size(203, 13);
this.hostNameLabel.TabIndex = 5;
this.hostNameLabel.Text = "Remote Side\'s Hostname or IP Address";
//
// hostNameTextBox
//
this.hostNameTextBox.Location = new System.Drawing.Point(24, 56);
this.hostNameTextBox.Name = "hostNameTextBox";
this.hostNameTextBox.Size = new System.Drawing.Size(208, 20);
this.hostNameTextBox.TabIndex = 0;
this.hostNameTextBox.Text = "Hostname or IP Typed Here.";
this.toolTip1.SetToolTip(this.hostNameTextBox, "Enter the remote side\'s hostname or Ip address here.");
//
// exitButton
//
this.exitButton.Location = new System.Drawing.Point(328, 536);
this.exitButton.Name = "exitButton";
this.exitButton.TabIndex = 7;
this.exitButton.Text = "Exit";
this.toolTip1.SetToolTip(this.exitButton, "Press this button when you want to exit the program.");
this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
//
// localTextBox
//
this.localTextBox.Location = new System.Drawing.Point(24, 240);
this.localTextBox.Multiline = true;
this.localTextBox.Name = "localTextBox";
this.localTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.localTextBox.Size = new System.Drawing.Size(440, 120);
this.localTextBox.TabIndex = 4;
this.localTextBox.Text = "Your Message Appeared Here.";
this.toolTip1.SetToolTip(this.localTextBox, "Your text messages will appear here.");
//
// remoteMessageLabel
//
this.remoteMessageLabel.AutoSize = true;
this.remoteMessageLabel.ForeColor = System.Drawing.SystemColors.ControlText;
this.remoteMessageLabel.Location = new System.Drawing.Point(24, 376);
this.remoteMessageLabel.Name = "remoteMessageLabel";
this.remoteMessageLabel.Size = new System.Drawing.Size(126, 13);
this.remoteMessageLabel.TabIndex = 3;
this.remoteMessageLabel.Text = "Remote Side\'s Message";
//
// WinChatForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(552, 577);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.notifyButton,
this.doneButton,
this.exitButton,
this.TypedMessageLabel,
this.hostNameLabel,
this.remoteMessageLabel,
this.localMessageLabel,
this.typedMessage,
this.hostNameTextBox,
this.remoteTextBox,
this.sendButton,
this.localTextBox});
this.Menu = this.mainMenu1;
this.Name = "WinChatForm";
this.Text = "WinChat";
this.ResumeLayout(false);
}
#endregion
private void Form_OnExit(object sender, System.EventArgs e)
{
exit_program();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -