C#使用Winfrom编程时的注意事项
C#让窗体不能被修改大小
FormBorderStyle = FormBorderStyle.FixedSingle;
让窗体在屏幕中心显示:
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
关闭最大化功能:
this.MaximizeBox = false;
关闭最小化功能:
this.MinimizeBox = false;
让组件可以被多线程修改:
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
发送httpd请求:
·HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(@"url地址"));
req.Method = "协议";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "application/json";
req.Referer = "http:///";
string result = "";
StringBuilder builder = new StringBuilder();
builder.AppendFormat("{0}={1}", "phone", textBox1.Text);
builder.Append("&");
builder.AppendFormat("{0}={1}", "password", textBox2.Text);
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{ reqStream.Write(data, 0, data.Length); reqStream.Close(); }
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{ result = reader.ReadToEnd(); }
开启多线程执行函数:
Thread thread1 = new Thread(new ThreadStart(StartSomeWorkFromUIThread));
Thread thread2 = new Thread(new ThreadStart(GetMessage));
thread1.IsBackground = true;
thread1.Start();
定义动态string数组:
private List<string> Have_Gotton_User_Name = new List<string>();