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

📄 mainform.cs

📁 网络考试系统 系统基于 .NET 框架开发
💻 CS
📖 第 1 页 / 共 3 页
字号:
		private void ConnectionToClient(){
			//try{
				socket.Connect(po);
				if (socket.Connected){
					Byte[] fsbyte=	System.Text.Encoding.Unicode.GetBytes(this.cuMSG);
					socket.Send(fsbyte,fsbyte.Length,0);
				}	
			//}catch(Exception ex){
				//MessageBox.Show("连接客户机时出错:"+ex.Message.ToString());
				//return;
			//}
		}

		#region 监听主考机或应考机消息
		/// <summary>
		/// 供主考官链接
		/// </summary>
		private void ListenServer() {

			try{

				while(true) {

					tempsocket = socket__.Accept();

					if (tempsocket.Connected) {

						Byte[] b=new byte[4000];
						tempsocket.Receive(b,b.Length,0);
						string str = System.Text.Encoding.Unicode.GetString(b);
						int    sI  = str.IndexOf("}",2);
						string odr        = str.Substring(3,sI-3);

						this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

						switch(odr){

							case "QUESTION":   //考题

								//
								this.q.Text = str.Replace("{{{QUESTION}}}","");
								this.a.Text = "";

								break;

							case "QUESTION_QUICK":  //抢答通讯

								this.keySpace = true;
								this.isAlert  = true;
								this.button33.Enabled = false;
								this.button34.Enabled = false;
								this.a.Text = "";
								this.isW    = false;
								this.timer.Stop();
								this.ksp.Focus();

								this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
								this.q.Text = str.Replace("{{{QUESTION_QUICK}}}","");

								break;

							case "BK_SELECT":


								this.ClearLabel();

								//this.button101.Visible  = true;
								//this.button101.Location = new Point(776, 656);
								//this.button101.Size     = new Size(184, 40);
								//this.button101.Text    = "确定考题";

								this.isW = true;
								this.timer.Start();

								BK_SelectExam bs = new BK_SelectExam();
								bs.ShowDialog();

								this.ipStr.Clear();
								this.ipStr.Add(this.serverIP + "->");
								this.cuMSG = "{{{YES_BK_SELECT}}}XXXXX";
								this.SendToClient();

								this.isW = false;

								break;

							case "RANDOM":

								this.isW = true;
								this.timer.Interval = 10;
								this.timer.Start();
								break;

							case "UN_BK_SELECT":

								
								this.timer.Stop();
								this.ClearLabel();
								this.button101.Visible = false;
								MyMessage mm = new MyMessage("已被主考官结束选题操作");
								mm.ShowDialog();

								break;

							case "YES_BK_SELECT":

								//出现选题按钮
								this.isSelectExam = true;
								this.BK_Select();

								break;

							case "ANSWER":

								this.a.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
								this.a.Text = str.Replace("{{{ANSWER}}}","");

								break;

							case "TIMEOUT":  //抢答超时

								this.button33.Enabled = true;
								this.button34.Enabled = true;
								this.keySpace = false;
								this.isW      = false;
								this.timer.Stop();
								mm = new MyMessage("本道题抢答时间已到");
								mm.ShowDialog();

								break;

							case "CLEARFORM":  //通知客户端清空界面

								this.ClearLabel();

								break;

							case "SELECTFEN":  //选择分数级别

								se.fenType.SelectedIndex = -1;
								se.examC_.Items.Clear();
								se.examC_.SelectedIndex  = -1;
								se.examC.Text            = "";
								se.actionS               = true;
								se.label2.Text           = "你选择了{未操作},目前该分数级别的题库中还有{0}道题,请在下面的选择框中选择{0}之间的任一数字!";
								se.ShowDialog();

								break;

							case "QUICK":

								if(this.isAlert){
									string ipStr_ = str.Replace("{{{QUICK}}}","").Trim();
									ipStr_ = this.GetIP(ipStr_);
									ipStr_ = ipStr_ + "->" + this.GetName(ipStr_);
									this.cuMSG    = "{{{UNQUICK}}}" + ipStr_;
									this.alert    = false;
									this.isAlert  = false;
									this.SendToClient();
									QuickForm qf = new QuickForm(ipStr_);
									qf.ShowDialog();
								}

								break;

							case "UNQUICK":  

								string ipStr__ = str.Replace("{{{UNQUICK}}}","");
								this.keySpace = false;
								this.button33.Enabled = true;
								this.button34.Enabled = true;
								QuickForm qf_  = new QuickForm(ipStr__);
								qf_.ShowDialog();

								break;

							case "FEN":  //返回符合分数级别的试题数目

								
								string str_1 = str.Replace("{{{FEN}}}","").Trim();

								str_1 = str_1.Substring(0,4);

								string sql  = "select * from ExamItem where fenType = '" + str_1.ToString() + "' and  state='可用'";

								this.conn.Open();
								OleDbDataAdapter odp = new OleDbDataAdapter(sql,conn);
								DataSet ds = new DataSet();
								odp.Fill(ds,"EXAMITEM");
								this.conn.Close();

								if(ds.Tables[0].Rows.Count>0){
									this.cuMSG = "{{{EXAMCOUNT}}}" + ds.Tables[0].Rows.Count.ToString();
									this.SendToClient();
								}else{
									mm = new MyMessage("应考者在选择试题,但目前题库中已没有可用试题");
									mm.ShowDialog();
								}

								break;

							case "EXAMCOUNT":

								
								str_1 = str.Replace("{{{EXAMCOUNT}}}","");
								this.examC     = str_1;
								Config.examCou = str_1;
								se.ShowDialog();

								break;

							case "NOSELECTEXAM":

								mm = new MyMessage("对方选择了放弃");
								mm.ShowDialog();
							
								this.button101.Enabled = true;
								this.button102.Enabled = true;
								this.button104.Enabled = true;
								this.button105.Enabled = true;

								this.button103.ForeColor = Color.Black;

								break;

							case "SELECTEXAMCOUNT":

								str_1 = str.Replace("{{{SELECTEXAMCOUNT}}}","");
								int iIndex   = str_1.IndexOf("分题");
								str_1        = str_1.Substring(0,iIndex + 2);

								sql  = "select * from ExamItem where fenType = '"+ PublicApplication.GetFormatStr(str_1.ToString(),"|||","end") +"' and  state='可用'";
								this.conn.Open();
								odp = new OleDbDataAdapter(sql,conn);
								ds = new DataSet();
								odp.Fill(ds,"EXAMITEM");
								this.conn.Close();
								if(Int32.Parse(PublicApplication.GetFormatStr(str_1,"|||","start")) <= ds.Tables[0].Rows.Count){
									DataRow row      = ds.Tables[0].Rows[Int32.Parse(PublicApplication.GetFormatStr(str_1,"|||","start")) -1];
									this.currentExam = row;
									this.q.Text      = PublicApplication.GetFormatStr(str_1.ToString(),"|||","end") + ":\n" + row["qStr"].ToString();
									//this.a.Text      = row["aStr"].ToString();
									this.cuMSG       = "{{{QUESTION}}}" + this.q.Text;
									this.SendToClient();

									sql = "update ExamItem set state = '作废' where eID=" + row["eID"].ToString();
									this.conn.Open();
									OleDbCommand odc = new OleDbCommand(sql,conn);
									odc.ExecuteNonQuery();
									this.conn.Close();

									this.button101.Enabled = true;
									this.button102.Enabled = true;
									this.button104.Enabled = true;
									this.button105.Enabled = true;

									this.button103.ForeColor = Color.Black;

								}else{
									mm = new MyMessage("应考者选择试题失败,请重新选择!\n\n原因:输入了题库外的数字");
									mm.ShowDialog();
								}

								break;

							default:
								//无操作
								break;
						}
					}
				}

			}catch(Exception ex){

				MessageBox.Show("与主考官电脑连接时出错:"+ex.Message.ToString());
			}
		}
		#endregion

		/// <summary>
		/// 按下空格键抢答
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MainForm_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
			if(this.keySpace && e.KeyChar==' '){
				this.ipStr.Clear();
				this.ipStr.Add(this.serverIP + "->");
				this.cuMSG = "{{{QUICK}}}" + this.localIP;
				this.SendToClient();
			}
			this.keySpace = false;
		}

		private void ksp_Click(object sender, System.EventArgs e) {
			if(this.keySpace){
				this.ipStr.Clear();
				this.ipStr.Add(this.serverIP + "->");
				this.cuMSG = "{{{QUICK}}}" + this.localIP;
				this.SendToClient();
			}
			this.keySpace = false;
		}

		/// <summary>
		/// 抢答题到期
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timeOutCon_Tick(object sender, System.EventArgs e) {

			this.timeOutCon.Stop();

			if(this.alert){
				this.cuMSG = "{{{TIMEOUT}}}XXXX";
				this.SendToClient();

				MainForm.PlaySound(Application.StartupPath + @"\timeout.wav",0,0);
				MessageBox.Show("本题抢答时间到!");
				this.button102.Text    = "(抢答)随机抽题";
				this.button102.ForeColor = Color.Black;
				this.button101.Enabled = true;
				this.button103.Enabled = true;
				this.button104.Enabled = true;
				this.isW   = false;
				this.alert = false;
			}
		}

		//
		public void CloseApp(){
			Application.Exit();
		}

		/// <summary>
		/// 从IP找姓名
		/// </summary>
		/// <param name="ipstr"></param>
		private string GetName(string ipstr){
			for(int i=0;i<this.clientIPs.Length;i++){
				if(ipstr.IndexOf(this.clientIPs[i].ToString()) >=0 ){
					return  this.clieckNas[i].ToString();
				}
			}

			return ipstr;
		}

		/// <summary>
		/// 取IP地址
		/// </summary>
		/// <param name="ipstr"></param>
		/// <returns></returns>
		private string GetIP(string ipstr){
			string newS = "";
			char[] arrs = ipstr.ToCharArray();
			for(int i=0;i<arrs.Length;i++){
				if(PublicApplication.IsNumeric(arrs[i].ToString()) || arrs[i].ToString() == "."){
					newS += arrs[i].ToString();
					if(i > 20){
						return newS;
						break;
					}
				}
			}
			return newS;
		}

		[DllImport("winmm.dll")] 
		public static extern long PlaySound(String fileName,long a,long b);

		//序列化保存系统配置
		public void SaveSysteSet(){
			try{

				//
				this.ps.clientIPs = this.clientIPs;
				this.ps.clientNas = this.clieckNas;
				this.ps.localIP   = this.localIP;
				this.ps.serverIP  = this.serverIP;
				this.ps.timeOuts  = this.timeOut.ToString();

				//
				string defaultDirectory = Application.StartupPath;
				string fileName         = Application.StartupPath + "\\PropertyStatic.config";
				Stream s = File.Open(fileName,FileMode.Create);
				BinaryFormatter b =new BinaryFormatter();
				b.Serialize(s,this.ps);
				s.Close();
			}catch(Exception ex){
				MessageBox.Show("不能保存论坛集合到硬盘,错误信息:\n\n" + ex.Message.ToString());
			}
		}

		//打开配置
		public void OpenSystemSet(){
			try{
				string defaultDirectory = Application.StartupPath;
				string fileName         = defaultDirectory + "\\PropertyStatic.config";
				if(File.Exists(fileName)){ //如果存在,打开文件
					Stream s = File.Open(fileName,FileMode.Open);
					BinaryFormatter b = new BinaryFormatter();
					this.ps           = (PropertyStatic)b.Deserialize(s);
					s.Close();

					this.clientIPs = this.ps.clientIPs;
					this.clieckNas = this.ps.clientNas;
					this.localIP   = this.ps.localIP;
					this.serverIP  = this.ps.serverIP;
					this.timeOut   = Int32.Parse(this.ps.timeOuts);
				}
			}catch(Exception ex){
				MessageBox.Show("不能从硬盘中打开论坛集合,错误信息:\n\n" + ex.Message.ToString());
			}
		}

		private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

			if(!this.isW){

				this.timer.Stop();
				this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

				if(this.softType== "SERVER"){
					this.GetExamItem();
					this.button31.Enabled = true;
					this.button32.Enabled = true;
					this.button33.Enabled = true;
					this.button34.Enabled = true;
					this.button105.Enabled = true;
				}

			}else{

				if(this.softType == "SERVER"){
					this.button31.Enabled = false;
					this.button32.Enabled = false;
					this.button33.Enabled = false;
					this.button34.Enabled = false;
					this.button105.Enabled = false;
				}

				this.q.Font = new System.Drawing.Font("宋体",120F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
				this.q.Text = GetRandom(10);
				//this.q.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

				//this.cuMSG = "{{{RANDOM_COUNT}}}" + this.q.Text;
				//this.SendToClient();
			}
		}
	}

	public class KeyFilterHander : IMessageFilter {
		public bool PreFilterMessage(ref Message m) {
			Keys keys = ((Keys)(m.WParam.ToInt32())) | Control.ModifierKeys;
			if (keys == (Keys.F4 | Keys.Alt)) {
				return true;
			}
			return false;

		}

	}
}

⌨️ 快捷键说明

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