颤栗效果的GUI使用c#编写的窗体
本文关键字:窗体 GUI 颤栗 使用 | 更新日期: 2023-09-27 18:17:41
对于客户端和服务器系统之间的文件传输,我们在客户端系统上使用c# windows窗体设计了一个GUI。我们添加了许多背景图像和文本框,使GUI令人印象深刻。但是无论何时我们执行客户端代码,GUI都需要几秒钟的时间来加载自己并保持稳定,然后才能从GUI中做出选择。这发生在客户端GUI发出每个请求之后。(就像所有的form元素在短时间内消失并再次出现一样)。请帮帮我。我可以知道原因吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Client405
{
public partial class RequestForm : Form
{
#region Variables
internal static ListBox FileListBox;
internal static byte[] RecievedFile = new byte[46000000];
internal static string StoragePath = string.Empty;
internal static bool CacheSelected = false;
internal static string ChosenTech = "Default";
internal static bool EnableCompression = false;
//internal static string FileType;
internal static string SelectedFile = string.Empty;
internal static Stopwatch watch = new Stopwatch();
#endregion
public RequestForm()
{
InitializeComponent();
this.Load += new EventHandler(RequestForm_Load);
this.DoubleBuffered = true;
}
void RequestForm_Load(object sender, EventArgs e)
{
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
FileListBox = new ListBox();
FileName_textBox.Enabled = true;
FileName_textBox.ReadOnly = true;
if (!Connections.SocketConnected())
{
Connections.EstablishConnectionWithServer();
}
if (LoginAuthenticator.registered)
{
EnableCompression = true;
CacheSelected = true;
RegisterlinkLabel.Visible = false;
HighRB.Enabled = true;
LowRB.Enabled = true;
MediumRB.Enabled = true;
CacheMsg_label.Text = "Caching Enabled";
}
else if (!LoginAuthenticator.registered)
{
RegisterlinkLabel.Visible = true;
RegisterlinkLabel.Show();
Logout_Label.Text = "Home Page";
Cache_checkBox.CheckState = CheckState.Unchecked;
Cache_checkBox.Enabled = false;
EnableCompr_checkBox.Enabled = false;
HighRB.Enabled = false;
LowRB.Enabled = false;
MediumRB.Enabled = false;
DefaultRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Enabled = false;
NoneRB.Enabled = false;
CacheMsg_label.Text = "Caching Disabled";
}
ChosenTech = "None";
LogUser_Label.Text = LogUser_Label.Text + LoginAuthenticator.myCurrentUser;
SetClientInfo();
}
private void SetClientInfo()
{
//LogUser_Label.Text = LoginAuthenticator.myCurrentUser;
HostValue_Label.Text = HelperInfo.HostNameValue;
//Add port info
Port_Value.Text = HelperInfo.PortNumber;
//Add IP address
IPAddr_value.Text = HelperInfo.IPAddress_client;
//Add Gateway info
Gateway_value.Text = HelperInfo.Default_Gateway;
//Add the ip address of server connected to
ServerIP_Value_Label.Text = HelperInfo.Connected_To(Connections.myClientSocket);
if (Connections.myClientSocket.Connected)
{
ONOFF_radio.Text = "Online";
}
else
{
ONOFF_radio.Text = "Offline";
}
}
private void GetFile_Button_Click(object sender, EventArgs e)
{
if ((AudioRB.Checked == false) && (ImageRB.Checked == false) && (TextRB.Checked == false))
{
MessageBox.Show("Please select a File Type");
return;
}
if (SelectedFile == string.Empty)
{
MessageBox.Show("Please Select a File");
return;
}
if (!LoginAuthenticator.registered)
{
FileTransfer.UnRegisteredTransfer();
this.Refresh();
MessageBox.Show("Transaction Completed");
}
if (LoginAuthenticator.registered)
{
DialogResult trans_reqd = DialogResult.No;
//check Cache
if (Cache_checkBox.CheckState == CheckState.Checked)
{
trans_reqd = FileCache.TransactionQuery(RequestForm.SelectedFile);
if (trans_reqd == DialogResult.No)
{
string[] files = { System.IO.Path.Combine(@"D:''FT''Cache", RequestForm.SelectedFile), System.IO.Path.Combine(@"D:''FT''Downloads", RequestForm.SelectedFile) };
if (System.IO.File.Exists(files[1]))
{
System.IO.File.Delete(files[1]);
}
System.IO.File.Copy(files[0], files[1]);
watch.Stop();
}
else //if not in cacheF
{
FileTransfer.RegisteredTransfer();
}
}
else //if caching not enabled
{
FileTransfer.RegisteredTransfer();
}
this.Refresh();
MessageBox.Show("Transaction Completed");
}
}
private void AudioRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (AudioRB.Checked == true)
{
ImageRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Visible = false;
EnableCompr_checkBox.Enabled = false;
FileListBox.Hide();
DisplayRadioButtons();
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
FileTransfer.FileType = "Audio";
//ViewFiles_button.Visible = true;
ViewFiles();
}
else if (AudioRB.Checked == false)
{
//ViewFiles_button.Visible = false;
EnableCompr_checkBox.Visible = true;
EnableCompr_checkBox.Enabled = true;
FileListBox.Hide();
}
}
private void ViewFiles()
{
FileTransfer.GetFileNames();
this.Controls.Add(RequestForm.FileListBox);
RequestForm.FileListBox.BringToFront();
RequestForm.FileListBox.Visible = true;
RequestForm.FileListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
//RequestForm.SelectedFile = FileTransfer.FileListView.SelectedItems[0].Text;
//FileName_textBox.Text = RequestForm.SelectedFile;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FileName_textBox.Text = FileListBox.SelectedItem.ToString();
RequestForm.SelectedFile = FileName_textBox.Text;
}
private void ImageRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (ImageRB.Checked == true && LoginAuthenticator.registered)
{
AudioRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = true;
EnableCompr_checkBox.Visible = true;
FileListBox.Hide();
FileTransfer.FileType = "Image";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
DisplayRadioButtons();
ViewFiles();
//ViewFiles_button.Visible = true;
}
else if (ImageRB.Checked == true && !LoginAuthenticator.registered)
{
AudioRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = false;
EnableCompr_checkBox.Visible = true;
FileListBox.Hide();
FileTransfer.FileType = "Image";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
DisplayRadioButtons();
ViewFiles();
}
else if (ImageRB.Checked == false)
{
//ViewFiles_button.Visible = false;
RequestForm.FileListBox.Hide();
}
}
private void TextRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (TextRB.Checked == true && LoginAuthenticator.registered)
{
ImageRB.Checked = false;
AudioRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = true;
EnableCompr_checkBox.Visible = true;
RequestForm.FileListBox.Hide();
DisplayRadioButtons();
FileTransfer.FileType = "Text";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
ViewFiles();
//ViewFiles_button.Visible = true;
}
else if (TextRB.Checked == true && !LoginAuthenticator.registered)
{
ImageRB.Checked = false;
AudioRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = false;
EnableCompr_checkBox.Visible = true;
RequestForm.FileListBox.Hide();
DisplayRadioButtons();
FileTransfer.FileType = "Text";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
ViewFiles();
}
else if (TextRB.Checked == false)
{
//ViewFiles_button.Visible = false;
RequestForm.FileListBox.Hide();
}
}
private void Cache_checkBox_CheckedChanged(object sender, EventArgs e)
{
if (Cache_checkBox.Checked)
{
CacheMsg_label.Text = "Caching Enabled";
CacheSelected = true;
}
else
{
CacheMsg_label.Text = "Caching Disabled";
CacheSelected = false;
}
}
private void EnableCompr_checkBox_CheckedChanged(object sender, EventArgs e)
{
DisplayRadioButtons();
}
internal void DisplayRadioButtons()
{
if (EnableCompr_checkBox.CheckState == CheckState.Unchecked)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
DefaultRB.Enabled = false;
DefaultRB.Visible = false;
NoneRB.Enabled = false;
NoneRB.Visible = false;
}
if (EnableCompr_checkBox.Checked && FileTransfer.FileType == null)
{
MessageBox.Show("Please select a FileType.'nAssociated Compression techniques available will be shown.");
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
}
if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Text" && LoginAuthenticator.registered)
{
DefaultRB.Enabled = true;
DefaultRB.Visible = true;
NoneRB.Visible = true;
NoneRB.Enabled = true;
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Text" && !LoginAuthenticator.registered)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Visible = false;
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Image" && LoginAuthenticator.registered)
{
DefaultRB.Enabled = true;
DefaultRB.Visible = true;
NoneRB.Visible = true;
NoneRB.Enabled = true;
HighRB.Enabled = true;
MediumRB.Enabled = true;
LowRB.Enabled = true;
HighRB.Visible = true;
MediumRB.Visible = true;
LowRB.Visible = true;
ChosenTech = "Default";
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Image" && !LoginAuthenticator.registered)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
DefaultRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Enabled = false;
NoneRB.Enabled = false;
ChosenTech = "VLOW";
ComprMsgLabel.Text = "Chosen Compression technique : " + ChosenTech;
}
}
private void NoneRB_CheckedChanged(object sender, EventArgs e)
{
if (FileTransfer.FileType == "Text" && NoneRB.Checked == true)
{
ChosenTech = "None";
DefaultRB.Checked = false;
}
else if (FileTransfer.FileType == "Image" && NoneRB.Checked == true)
{
ChosenTech = "None";
DefaultRB.Checked = false;
HighRB.Checked = false;
LowRB.Checked = false;
MediumRB.Checked = false;
}
else if (NoneRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void Logout_Label_Click(object sender, EventArgs e)
{
Connections.EndConnectionWithServer();
Form1 newForm = new Form1();
LoginAuthenticator.myCurrentUser = string.Empty;
ChosenTech = string.Empty;
FileTransfer.FileType = null;
this.Hide();
newForm.Show();
MessageBox.Show("LogOut successful");
}
private void MediumRB_CheckedChanged(object sender, EventArgs e)
{
if (MediumRB.Checked == true)
{
ChosenTech = "MEDIUM";
HighRB.Checked = false;
LowRB.Checked = false;
NoneRB.Checked = false;
}
else if (MediumRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void LowRB_CheckedChanged(object sender, EventArgs e)
{
if (LowRB.Checked == true)
{
ChosenTech = "LOW";
HighRB.Checked = false;
MediumRB.Checked = false;
NoneRB.Checked = false;
}
else if (LowRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void HighRB_CheckedChanged(object sender, EventArgs e)
{
if (HighRB.Checked == true)
{
ChosenTech = "HIGH";
LowRB.Checked = false;
MediumRB.Checked = false;
NoneRB.Checked = false;
}
else if (HighRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void statistics_button_Click(object sender, EventArgs e)
{
Form2 statForm = new Form2();
statForm.fileType_label.Text = statForm.fileType_label.Text + FileTransfer.FileType;
statForm.fnstat_label.Text = statForm.fnstat_label.Text + SelectedFile;
statForm.ASize_label.Text = statForm.ASize_label.Text + " " + FileTransfer.actualSize + " bytes";
statForm.recSize_label.Text = statForm.recSize_label.Text + " " + FileTransfer.recSize + " bytes";
statForm.rtt_label.Text = statForm.rtt_label.Text + " " + watch.ElapsedMilliseconds + " ms";
long percentage = ((FileTransfer.actualSize - FileTransfer.recSize) * 100) / FileTransfer.actualSize;
statForm.BWsave.Text = statForm.BWsave.Text + " " + percentage + " %";
statForm.Show();
if (watch.ElapsedMilliseconds > 0)
{
MessageBox.Show("The time taken for the transaction is : " + watch.ElapsedMilliseconds);
}
else
{
MessageBox.Show("The time taken for the transaction is : 1000 ");
}
}
private void Folder_Select_Button_Click(object sender, EventArgs e)
{
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = @"D:'FT'";
sd.Filter = "Text files (*.txt)|*.txt|ImageFiles (*.jpg)|*.jpg | Audio Files (*.mp3)|*.mp3";
sd.ShowDialog();
if (!File.Exists(sd.FileName))
{
FileStream fs = File.Create(sd.FileName);
fs.Close();
}
else
{
FileStream fd = File.Open(sd.FileName, FileMode.OpenOrCreate);
fd.Close();
}
StoragePath = sd.FileName;
}
private void RegisterlinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Connections.EndConnectionWithServer();
//MessageBox.Show("Please Enter the Registration Details. ");
Register regForm = new Register();
regForm.Show();
this.Hide();
}
private void refresh_Click(object sender, EventArgs e)
{
if (FileTransfer.FileType == null)
{
MessageBox.Show("Select a File Type");
}
else
{
ViewFiles();
}
}
private void Setting_Label_Click(object sender, EventArgs e)
{
Process myprocess = new Process();
try
{
myprocess.StartInfo.UseShellExecute = true;
myprocess.StartInfo.FileName = "http://" + HelperInfo.Default_Gateway + "//WANem";
myprocess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void FileName_textBox_TextChanged(object sender, EventArgs e)
{
}
}
}
尝试在表单构造函数中使用下面的代码。
this.DoubleBuffered = true;
它应该能提高一些性能。Windows窗体不能显示大量相互重叠的图像。