public class SinaEntity { /// <summary> /// /// </summary> public string Num { get; set; } /// <summary> /// 0:”大秦铁路”,股票名字; /// </summary> public string Name { get; set; } /// <summary> /// 1:”27.55″,今日开盘价; /// </summary> public string Begin { get; set; } /// <summary> /// 2:”27.25″,昨日收盘价; /// </summary> public string Last { get; set; } /// <summary> /// 3:”26.91″,当前价格; /// </summary> public string New { get; set; } /// <summary> /// 4:”27.55″,今日最高价; /// </summary> public string Highest { get; set; } /// <summary> /// 5:”26.20″,今日最低价; /// </summary> public string Lowest { get; set; } /// <summary> /// 6:”26.91″,竞买价,即“买一”报价; /// </summary> public string Buy { get; set; } /// <summary> /// 7:”26.92″,竞卖价,即“卖一”报价; /// </summary> public string Sale { get; set; } /// <summary> /// 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百; /// </summary> public string Count { get; set; } /// <summary> /// 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万; /// </summary> public string TotalCommited { get; set; } /// <summary> /// 10:”4695″,“买一”申请4695股,即47手; /// </summary> public string b1Count { get; set; } /// <summary> /// 11:”26.91″,“买一”报价; /// </summary> public string b1 { get; set; } /// <summary> /// 13:”26.90″,“买二” /// </summary> public string b2 { get; set; } /// <summary> /// 12:”57590″,“买二” /// </summary> public string b2Count { get; set; } /// <summary> /// 15:”26.89″,“买三” /// </summary> public string b3 { get; set; } /// <summary> /// 14:”14700″,“买三” /// </summary> public string b3Count { get; set; } /// <summary> /// 17:”26.88″,“买四” /// </summary> public string b4 { get; set; } /// <summary> /// 16:”14300″,“买四” /// </summary> public string b4Count { get; set; } /// <summary> /// 19:”26.87″,“买五” /// </summary> public string b5 { get; set; } /// <summary> /// 18:”15100″,“买五” /// </summary> public string b5Count { get; set; } /// <summary> /// 21 /// </summary> public string s1 { get; set; } /// <summary> /// 20 /// </summary> public string s1Count { get; set; } /// <summary> /// 23 /// </summary> public string s2 { get; set; } /// <summary> /// 22 /// </summary> public string s2Count { get; set; } /// <summary> /// 25 /// </summary> public string s3 { get; set; } /// <summary> /// 24 /// </summary> public string s3Count { get; set; } /// <summary> /// 27 /// </summary> public string s4 { get; set; } /// <summary> /// 26 /// </summary> public string s4Count { get; set; } /// <summary> /// 29 /// </summary> public string s5 { get; set; } /// <summary> /// 28 /// </summary> public string s5Count { get; set; } /// <summary> /// 30:”2008-01-11″,日期; /// </summary> public string Date { get; set; } /// <summary> /// 31:”15:05:32″,时间; /// </summary> public string Time { get; set; } /// <summary> /// 涨跌额(元) /// </summary> public string Different { get; set; } /// <summary> /// 涨跌幅(%) /// </summary> public string PricePercentage { get; set; } /// <summary> /// 委比(%) /// </summary> public double DelegatePercentage { get; set; } public double DelegatePercentage1 { get; set; } public double DelegatePercentage2 { get; set; } public double DelegatePercentage3 { get; set; } public double DelegatePercentage4 { get; set; } public double DelegatePercentage5 { get; set; } } partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (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.textBox1 = new System.Windows.Forms.TextBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 0; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // timer1 // this.timer1.Interval = 5000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(139, 21); this.Controls.Add(this.textBox1); this.MaximizeBox = false; this.Name = "Form1"; this.ShowIcon = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Timer timer1; } public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Next //cn.com.webxml.webservice.ChinaStockWebService cws; WebClient client; string chars = @"|/*/"; string Next(string s) { for (int i = 0; i < chars.Length; i++) { if (chars[i].ToString() == s && i + 1 < chars.Length) return chars[i + 1].ToString(); else if (chars[i].ToString() == s && i + 1 == chars.Length) return chars[0].ToString(); } return "!"; } #endregion private void Form1_Load(object sender, EventArgs e) { this.ShowIcon = true; client = new WebClient(); client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; //cws = new WindowsFormsApplication6.cn.com.webxml.webservice.ChinaStockWebService(); //cws.Proxy = System.Net.WebProxy.GetDefaultProxy(); //cws.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; } public Entity ConvertToEntity(string[] s) { Entity rtnValue = new Entity(); rtnValue.Num = s[0]; rtnValue.Name = s[1]; rtnValue.Time = s[2]; rtnValue.New = s[3]; rtnValue.Last = s[4]; rtnValue.Begin = s[5]; rtnValue.Different = s[6]; rtnValue.Lowest = s[7]; rtnValue.Highest = s[8]; rtnValue.PricePercentage = s[9]; rtnValue.Count = s[10]; rtnValue.TotalCommited = s[11]; rtnValue.Buy = s[12]; rtnValue.Sale = s[13]; rtnValue.DelegatePercentage = s[14]; rtnValue.Buy1 = s[15]; rtnValue.Buy2 = s[16]; rtnValue.Buy3 = s[17]; rtnValue.Buy4 = s[18]; rtnValue.Buy5 = s[19]; rtnValue.Sale1 = s[20]; rtnValue.Sale2 = s[21]; rtnValue.Sale3 = s[22]; rtnValue.Sale4 = s[23]; rtnValue.Sale5 = s[24]; return rtnValue; } public SinaEntity ConvertToEntity(string str) { SinaEntity rtnValue = new SinaEntity(); if (!string.IsNullOrEmpty(str)) { string[] s = str.Split(','); //rtnValue.Num = s[0]; rtnValue.Name = s[0]; rtnValue.Begin = s[1]; rtnValue.Last = s[2]; rtnValue.New = s[3]; rtnValue.Highest = s[4]; rtnValue.Lowest = s[5]; rtnValue.Buy = s[6]; rtnValue.Sale = s[7]; rtnValue.Count = s[8]; rtnValue.TotalCommited = s[9]; rtnValue.b1Count = s[10]; double db1c; double.TryParse(s[10], out db1c); rtnValue.b1 = s[11]; rtnValue.b2Count = s[12]; double db2c; double.TryParse(s[12], out db2c); rtnValue.b2 = s[13]; rtnValue.b3Count = s[14]; double db3c; double.TryParse(s[14], out db3c); rtnValue.b3 = s[15]; rtnValue.b4Count = s[16]; double db4c; double.TryParse(s[16], out db4c); rtnValue.b4 = s[17]; rtnValue.b5Count = s[18]; double db5c; double.TryParse(s[18], out db5c); rtnValue.b5 = s[19]; rtnValue.s1Count = s[20]; double ds1c; double.TryParse(s[20], out ds1c); rtnValue.s1 = s[21]; rtnValue.s2Count = s[22]; double ds2c; double.TryParse(s[22], out ds2c); rtnValue.s2 = s[23]; rtnValue.s3Count = s[24]; double ds3c; double.TryParse(s[24], out ds3c); rtnValue.s3 = s[25]; rtnValue.s4Count = s[26]; double ds4c; double.TryParse(s[26], out ds4c); rtnValue.s4 = s[27]; rtnValue.s5Count = s[28]; double ds5c; double.TryParse(s[28], out ds5c); rtnValue.s5 = s[29]; rtnValue.Date = s[30]; rtnValue.Time = s[31]; decimal last = decimal.Parse(rtnValue.Last); decimal nEw = decimal.Parse(rtnValue.New); rtnValue.Different = Convert.ToString(nEw - last); rtnValue.PricePercentage = string.Format("{0:n2}%", (nEw - last) / last * 100); double scount = ds1c + ds2c + ds3c + ds4c + ds5c; double bcount = db1c + db2c + db3c + db4c + db5c; rtnValue.DelegatePercentage = (bcount - scount) / (scount + bcount) * 100; double s1count = ds1c; double b1count = db1c; rtnValue.DelegatePercentage1 = (b1count - s1count) / (s1count + b1count) * 100; //double s2count = ds2c; //double b2count = db2c; //rtnValue.DelegatePercentage2 = (b2count - s2count) / (s2count + b2count) * 100; //double s3count = ds3c; //double b3count = db3c; //rtnValue.DelegatePercentage3 = (b3count - s3count) / (s3count + b3count) * 100; //double s4count = ds4c; //double b4count = db4c; //rtnValue.DelegatePercentage4 = (b4count - s4count) / (s4count + b4count) * 100; //double s5count = ds5c; //double b5count = db5c; //rtnValue.DelegatePercentage5 = (b5count - s5count) / (s5count + b5count) * 100; } return rtnValue; } DRun drun; private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Contains("s")) { this.timer1.Stop(); this.Text = "Stop " + this.Text; } else if (textBox1.Text.Trim().Length == 6) { this.WindowState = FormWindowState.Minimized; error = 3; timer1.Interval = 1000; timer1.Start(); //drun = new DRun(Run); //drun.BeginInvoke(n, null, null); //Run(n); } } string lastNew = string.Empty; int error; string c = "|"; delegate void DRun(int n); private void Run(int n) { if (this.InvokeRequired) { this.Invoke(new DRun(Run), n); } else { string num = string.Empty; if (textBox1.Text.Trim().StartsWith("60")) { num = "sh" + textBox1.Text.Trim(); } else if (textBox1.Text.StartsWith("00")) { num = "sz" + textBox1.Text.Trim(); } else return; //while (!stop) { try { //string[] s = cws.getStockInfoByCode(num); //Entity entity = ConvertToEntity(s); string result; Uri address = new Uri("http://hq.sinajs.cn/list=" + num); using (StringReader reader = new StringReader(client.DownloadString(address))) { result = reader.ReadToEnd(); } SinaEntity entity = ConvertToEntity(result); string UorD = " "; if (entity.New == entity.s1) UorD = "T"; else if (entity.New == entity.b1) UorD = "_"; if (entity.Different.StartsWith("0.")) entity.Different = entity.Different.Replace("0.", "."); else if (entity.Different.StartsWith("-0.")) entity.Different = entity.Different.Replace("-0.", "-."); if (entity.PricePercentage.StartsWith("0.")) entity.PricePercentage = entity.PricePercentage.Replace("0.", "."); else if (entity.PricePercentage.StartsWith("-0.")) entity.PricePercentage = entity.PricePercentage.Replace("-0.", "-."); this.Text = entity.New + UorD + entity.Different + UorD + entity.PricePercentage + "/n" + // c+ timer1.Interval.ToString() + "/n" + "+" + entity.s5.Substring(entity.s5.Length - 1) + " " + LongToString(entity.s5Count) + " " + entity.s4.Substring(entity.s4.Length - 1) + " " + LongToString(entity.s4Count) + " " + entity.s3.Substring(entity.s3.Length - 1) + " " + LongToString(entity.s3Count) + "/n" + "+" + entity.s1.Substring(entity.s1.Length - 1) + " " + LongToString(entity.s1Count) + " " + entity.s2.Substring(entity.s2.Length - 1) + " " + LongToString(entity.s2Count) + "/n" + "-" + entity.b1.Substring(entity.b1.Length - 1) + " " + LongToString(entity.b1Count) + " " + entity.b2.Substring(entity.b2.Length - 1) + " " + LongToString(entity.b2Count) + " " + entity.b3.Substring(entity.b3.Length - 1) + " " + LongToString(entity.b3Count) + "/n" + "-" + entity.b5.Substring(entity.b5.Length - 1) + " " + LongToString(entity.b5Count) + " " + entity.b4.Substring(entity.b4.Length - 1) + " " + LongToString(entity.b4Count) + "/n"; c = Next(c); Bitmap iconBm = new Bitmap(16, 16); Graphics g = Graphics.FromImage(iconBm); #region wb if (entity.DelegatePercentage > 0) { g.DrawLine(Pens.Red, 14, 15, 14, 15f - (float)(15f / 100 * entity.DelegatePercentage)); g.DrawLine(Pens.Red, 15, 15, 15, 15f - (float)(15f / 100 * entity.DelegatePercentage)); } else { g.DrawLine(Pens.Green, 14, 0, 14, (float)(15f / 100 * entity.DelegatePercentage * -1)); g.DrawLine(Pens.Green, 15, 0, 15, (float)(15f / 100 * entity.DelegatePercentage * -1)); } //11,12,13 double ds1c; if (double.TryParse(entity.s1Count, out ds1c)) { int l1 = 11, l2 = 12, l3 = 13; DrawSaleLine(g, ds1c, l1, l2, l3); } double db1c; if (double.TryParse(entity.b1Count, out db1c)) { int l1 = 11, l2 = 12, l3 = 13; DrawBuyLine(g, db1c, l1, l2, l3); } //8,9,10 double ds2c; if (double.TryParse(entity.s2Count, out ds2c)) { int l1 = 8, l2 = 9, l3 = 10; DrawSaleLine(g, ds2c, l1, l2, l3); } double db2c; if (double.TryParse(entity.b2Count, out db2c)) { int l1 = 8, l2 = 9, l3 = 10; DrawBuyLine(g, db2c, l1, l2, l3); } //5,6,7 double ds3c; if (double.TryParse(entity.s3Count, out ds3c)) { int l1 = 5, l2 = 6, l3 = 7; DrawSaleLine(g, ds3c, l1, l2, l3); } double db3c; if (double.TryParse(entity.b3Count, out db3c)) { int l1 = 5, l2 = 6, l3 = 7; DrawBuyLine(g, db3c, l1, l2, l3); } //2,3,4 double ds4c; if (double.TryParse(entity.s4Count, out ds4c)) { int l1 = 2, l2 = 3, l3 = 4; DrawSaleLine(g, ds4c, l1, l2, l3); } double db4c; if (double.TryParse(entity.b4Count, out db4c)) { int l1 = 2, l2 = 3, l3 = 4; DrawBuyLine(g, db4c, l1, l2, l3); } //0,1,1 double ds5c; if (double.TryParse(entity.s5Count, out ds5c)) { int l1 = 0, l2 = 1, l3 = 1; DrawSaleLine(g, ds5c, l1, l2, l3); } double db5c; if (double.TryParse(entity.b5Count, out db5c)) { int l1 = 0, l2 = 1, l3 = 1; DrawBuyLine(g, db5c, l1, l2, l3); } 21 //if (entity.DelegatePercentage1 > 0) //{ // g.DrawLine(Pens.Red, 21, 31, 21, 31f - (float)(31f / 100 * entity.DelegatePercentage1)); //} //else //{ // g.DrawLine(Pens.Green, 21, 0, 21, (float)(31f / 100 * entity.DelegatePercentage1 * -1)); //} //if (entity.DelegatePercentage2 > 0) //{ // g.DrawLine(Pens.Black, 17, 31, 17, 31f - (float)(31f / 100 * entity.DelegatePercentage2)); //} //else //{ // g.DrawLine(Pens.Black, 17, 0, 17, (float)(31f / 100 * entity.DelegatePercentage2 * -1)); //} //if (entity.DelegatePercentage3 > 0) //{ // g.DrawLine(Pens.Black, 11, 31, 11, 31f - (float)(31f / 100 * entity.DelegatePercentage3)); //} //else //{ // g.DrawLine(Pens.Black, 11, 0, 11, (float)(31f / 100 * entity.DelegatePercentage3 * -1)); //} //if (entity.DelegatePercentage4 > 0) //{ // g.DrawLine(Pens.Black, 5, 31, 5, 31f - (float)(31f / 100 * entity.DelegatePercentage4)); //} //else //{ // g.DrawLine(Pens.Black, 5, 0, 5, (float)(31f / 100 * entity.DelegatePercentage4 * -1)); //} //if (entity.DelegatePercentage5 > 0) //{ // g.DrawLine(Pens.Black, 1, 31, 1, 31f - (float)(31f / 100 * entity.DelegatePercentage5)); //} //else //{ // g.DrawLine(Pens.Black, 1, 0, 1, (float)(31f / 100 * entity.DelegatePercentage5 * -1)); //} #endregion Icon icon = Icon.FromHandle(iconBm.GetHicon()); this.Icon = icon; if (string.IsNullOrEmpty(lastNew)) { lastNew = entity.New; timer1.Interval = 5000; } else if (lastNew == entity.New) { TimerIntervalAdd(true); } else { lastNew = entity.New; TimerIntervalAdd(false); } //Thread.Sleep(5000); //Application.DoEvents(); } catch { error--; if (error < 0) { this.Text = "Error!"; return; } } } } } private string LongToString(string num) { decimal d; if (decimal.TryParse(num, out d)) { if (d < 1000) return Convert.ToString((int)d); else if (d < 10000) return num.Substring(0, 1) + "k";//k else if (d < 100000) return num.Substring(0, 1) + "w";//w else if (d < 1000000) return num.Substring(0, 2) + "w";//10w else if (d < 10000000) return num.Substring(0, 3) + "w";//100w } return "0"; } private static void DrawSaleLine(Graphics g, double saleCount, int l1, int l2, int l3) { Pen p1 = Pens.LightGreen, p2 = Pens.GreenYellow, p3 = Pens.SpringGreen, p4 = Pens.LimeGreen, p5 = Pens.MediumSeaGreen, p6 = Pens.Green, p7 = Pens.DarkGreen; if (saleCount < 10) { //g.DrawLine(p1, l1, 0, l1, (float)(8f / 10 * saleCount)); g.DrawLine(p1, l2, 0, l2, (float)(8f / 10 * saleCount)); //g.DrawLine(p1, l3, 0, l3, (float)(8f / 10 * saleCount)); } else if (saleCount < 100) { //g.DrawLine(p1, l1, 0, l1, (float)(8f / 100 * saleCount)); g.DrawLine(p2, l2, 0, l2, (float)(8f / 100 * saleCount)); //g.DrawLine(p1, l3, 0, l3, (float)(8f / 100 * saleCount)); } else if (saleCount < 1000)//<k { //g.DrawLine(p1, l2, 0, l2, (float)(8f / 1000 * saleCount)); g.DrawLine(p3, l3, 0, l3, (float)(8f / 1000 * saleCount)); } else if (saleCount < 10000)//<w { //g.DrawLine(p2, l2, 0, l2, (float)(8f / 10000 * saleCount)); g.DrawLine(p4, l3, 0, l3, (float)(8f / 10000 * saleCount)); } else if (saleCount < 100000)//<10w { //g.DrawLine(p1, l1, 0, l1, (float)(8f / 100000 * saleCount)); g.DrawLine(p5, l2, 0, l2, (float)(8f / 100000 * saleCount)); //g.DrawLine(p1, l3, 0, l3, (float)(8f / 100000 * saleCount)); } else if (saleCount < 1000000)//<100w { //g.DrawLine(p2, l1, 0, l1, (float)(8f / 1000000 * saleCount)); g.DrawLine(p6, l2, 0, l2, (float)(8f / 1000000 * saleCount)); //g.DrawLine(p2, l3, 0, l3, (float)(8f / 1000000 * saleCount)); } else if (saleCount < 10000000)//<1000w { g.DrawLine(p7, l1, 0, l1, (float)(8f / 10000000 * saleCount)); g.DrawLine(p7, l2, 0, l2, (float)(8f / 10000000 * saleCount)); g.DrawLine(p7, l3, 0, l3, (float)(8f / 10000000 * saleCount)); } else g.DrawLine(Pens.Black, l2, 0, l2, (float)(8f / 100000000 * saleCount)); } private static void DrawBuyLine(Graphics g, double saleCount, int l1, int l2, int l3) { Pen p1 = Pens.LightGreen, p2 = Pens.GreenYellow, p3 = Pens.SpringGreen, p4 = Pens.LimeGreen, p5 = Pens.MediumSeaGreen, p6 = Pens.Green, p7 = Pens.DarkGreen; if (saleCount < 10) { g.DrawLine(p1, l2, 15, l2, 15f - (float)(8f / 10 * saleCount)); } else if (saleCount < 100) { g.DrawLine(p2, l2, 15, l2, 15f - (float)(8f / 100 * saleCount)); } else if (saleCount < 1000)//<k { g.DrawLine(p3, l3, 15, l3, 15f - (float)(8f / 1000 * saleCount)); } else if (saleCount < 10000)//<w { g.DrawLine(p4, l3, 15, l3, 15f - (float)(8f / 10000 * saleCount)); } else if (saleCount < 100000)//<10w { g.DrawLine(p5, l2, 15, l2, 15f - (float)(8f / 100000 * saleCount)); } else if (saleCount < 1000000)//<100w { g.DrawLine(p6, l2, 15, l2, 15f - (float)(8f / 1000000 * saleCount)); } else if (saleCount < 10000000)//<1000w { g.DrawLine(p7, l1, 15, l1, 15f - (float)(8f / 10000000 * saleCount)); g.DrawLine(p7, l2, 15, l2, 15f - (float)(8f / 10000000 * saleCount)); g.DrawLine(p7, l3, 15, l3, 15f - (float)(8f / 10000000 * saleCount)); } else g.DrawLine(Pens.Black, l2, 15, l2, 15f - (float)(8f / 100000000 * saleCount)); } private void TimerIntervalAdd(bool add) { if (add) { if (timer1.Interval == 30 * 1000) return; else if (timer1.Interval >= 20 * 1000) timer1.Interval = 30 * 1000; else if (timer1.Interval >= 15 * 1000) timer1.Interval = 20 * 1000; else if (timer1.Interval >= 10 * 1000) timer1.Interval = 15 * 1000; else if (timer1.Interval >= 8 * 1000) timer1.Interval = 10 * 1000; else if (timer1.Interval >= 6 * 1000) timer1.Interval = 8 * 1000; else if (timer1.Interval >= 4 * 1000) timer1.Interval = 6 * 1000; else if (timer1.Interval >= 2 * 1000) timer1.Interval = 4 * 1000; else if (timer1.Interval >= 1 * 1000) timer1.Interval = 2 * 1000; else timer1.Interval = 1000; } else { timer1.Interval = 1000; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (client != null) client.Dispose(); } bool timerLock = false; private void timer1_Tick(object sender, EventArgs e) { if (timerLock) return; timerLock = true; int n = 0; if (int.TryParse(textBox1.Text, out n)) { drun = new DRun(Run); drun.BeginInvoke(n, null, null); } timerLock = false; } }