/// <summary> /// 秒表事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { SecurityLatestData newData = new SecurityLatestData(); SecurityService.GetLatestData(securityCode, ref newData); if (!newData.equal(lastData)) { lastData = newData; Invalidate(); } }
/// <summary> /// 单元格点击事件 /// </summary> /// <param name="cell"></param> /// <param name="e"></param> public override void OnCellClick(GridCell cell, System.Windows.Forms.MouseEventArgs e) { base.OnCellClick(cell, e); if (e.Clicks == 1) { String code = cell.Row.GetCell("colP1").GetString(); Security security = new Security(); SecurityLatestData latestData = new SecurityLatestData(); if (SecurityService.GetSecurityByCode(code, ref security) > 0 && SecurityService.GetLatestData(code, ref latestData) > 0) { MainForm.instance.latestDiv.SecurityCode = code; MainForm.instance.latestDiv.Invalidate(); MainForm.instance.chartExtend.ChangeSecurity(code); } Console.WriteLine("1"); } }
/// <summary> /// 键盘按下事件 /// </summary> /// <param name="e"></param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.KeyCode == Keys.Enter) { if (SelectedRows.Count > 0) { String code = SelectedRows[0].GetCell("colP1").GetString(); Security security = new Security(); SecurityLatestData latestData = new SecurityLatestData(); if (SecurityService.GetSecurityByCode(code, ref security) > 0 && SecurityService.GetLatestData(code, ref latestData) > 0) { MainForm.instance.latestDiv.SecurityCode = code; MainForm.instance.latestDiv.Invalidate(); MainForm.instance.chartExtend.ChangeSecurity(code); } } } }
/// <summary> /// 重绘方法 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = null; BufferedGraphics myBuffer = null; BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; myBuffer = currentContext.Allocate(pe.Graphics, DisplayRectangle); g = myBuffer.Graphics; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; if (!blackOrWhite) { g.Clear(Color.White); } SecurityLatestData latestData = new SecurityLatestData(); Security security = new Security(); SecurityService.GetSecurityByCode(SecurityCode, ref security); SecurityService.GetLatestData(SecurityCode, ref latestData); String securityName = security.m_name; int width = Width - 1; int height = Height - 1; if (width > 0 && height > 0) { Font font = new Font("微软雅黑", 12); Font lfont = new Font("微软雅黑", 12); Color wordColor = Color.FromArgb(100, 100, 100); int top = 32, step = 22; //画买卖盘 DrawText(g, "卖", wordColor, font, 1, 47); DrawText(g, "盘", wordColor, font, 1, 100); DrawText(g, "买", wordColor, font, 1, 157); DrawText(g, "盘", wordColor, font, 1, 210); String buySellStr = "5,4,3,2,1,1,2,3,4,5"; String[] buySellStrs = buySellStr.Split(','); int strsSize = buySellStrs.Length; for (int i = 0; i < strsSize; i++) { DrawText(g, buySellStrs[i], wordColor, font, 25, top); top += step; } font = new Font("微软雅黑", 12); top = 260; DrawText(g, "最新", wordColor, font, 1, top); DrawText(g, "升跌", wordColor, font, 1, top + 20); DrawText(g, "幅度", wordColor, font, 1, top + 40); DrawText(g, "总手", wordColor, font, 1, top + 60); DrawText(g, "涨停", wordColor, font, 1, top + 80); DrawText(g, "外盘", wordColor, font, 1, top + 100); DrawText(g, "开盘", wordColor, font, 110, top); DrawText(g, "最高", wordColor, font, 110, top + 20); DrawText(g, "最低", wordColor, font, 110, top + 40); DrawText(g, "换手", wordColor, font, 110, top + 60); DrawText(g, "跌停", wordColor, font, 110, top + 80); DrawText(g, "内盘", wordColor, font, 110, top + 100); font = new Font("微软雅黑", 14, FontStyle.Bold); //画股票代码 Color yellowColor = Color.FromArgb(255, 255, 80); if (!blackOrWhite) { yellowColor = Color.Black; } if (latestData.m_code != null && latestData.m_code.Length > 0) { double close = latestData.m_close, open = latestData.m_open, high = latestData.m_high, low = latestData.m_low, lastClose = latestData.m_lastClose; if (close == 0) { if (latestData.m_buyPrice1 > 0) { close = latestData.m_buyPrice1; open = latestData.m_buyPrice1; high = latestData.m_buyPrice1; low = latestData.m_buyPrice1; } else if (latestData.m_sellPrice1 > 0) { close = latestData.m_sellPrice1; open = latestData.m_sellPrice1; high = latestData.m_sellPrice1; low = latestData.m_sellPrice1; } } if (lastClose == 0) { lastClose = close; } List <double> plist = new List <double>(); List <double> vlist = new List <double>(); plist.Add(latestData.m_sellPrice5); plist.Add(latestData.m_sellPrice4); plist.Add(latestData.m_sellPrice3); plist.Add(latestData.m_sellPrice2); plist.Add(latestData.m_sellPrice1); vlist.Add(latestData.m_sellVolume5); vlist.Add(latestData.m_sellVolume4); vlist.Add(latestData.m_sellVolume3); vlist.Add(latestData.m_sellVolume2); vlist.Add(latestData.m_sellVolume1); plist.Add(latestData.m_buyPrice1); plist.Add(latestData.m_buyPrice2); plist.Add(latestData.m_buyPrice3); plist.Add(latestData.m_buyPrice4); plist.Add(latestData.m_buyPrice5); vlist.Add(latestData.m_buyVolume1); vlist.Add(latestData.m_buyVolume2); vlist.Add(latestData.m_buyVolume3); vlist.Add(latestData.m_buyVolume4); vlist.Add(latestData.m_buyVolume5); Color color = Color.Empty; double mx = Max(vlist); font = new Font("微软雅黑", 12); if (mx > 0) { //绘制买卖盘 int pLength = plist.Count; top = 32; for (int i = 0; i < pLength; i++) { color = GetPriceColor(plist[i], lastClose); DrawUnderLineNum(g, plist[i], digit, font, color, true, 60, top); DrawUnderLineNum(g, vlist[i], 0, font, yellowColor, false, 110, top); Brush sBrush = new SolidBrush(color); int dWidth = (int)(vlist[i] / mx * 60); if (dWidth < 2) { dWidth = 2; } g.FillRectangle(sBrush, new Rectangle(width - dWidth, top + step / 2 - 2, dWidth, 4)); sBrush.Dispose(); top += step; } } vlist.Clear(); plist.Clear(); top = 260; //成交 color = GetPriceColor(close, lastClose); DrawUnderLineNum(g, close, digit, font, color, true, 45, top); //升跌 double sub = 0; if (close == 0) { sub = latestData.m_buyPrice1 - lastClose; double rate = 100 * (latestData.m_buyPrice1 - lastClose) / lastClose; int pleft = DrawUnderLineNum(g, rate, 2, font, color, false, 45, top + 40); DrawText(g, "%", color, font, pleft + 47, top + 40); } else { sub = close - latestData.m_lastClose; double rate = 100 * (close - lastClose) / lastClose; int pleft = DrawUnderLineNum(g, rate, 2, font, color, false, 45, top + 40); DrawText(g, "%", color, font, pleft + 47, top + 40); } DrawUnderLineNum(g, sub, digit, font, color, false, 45, top + 20); double volume = latestData.m_volume / 100; String unit = ""; if (volume > 100000000) { volume /= 100000000; unit = "亿"; } else if (volume > 10000) { volume /= 10000; unit = "万"; } //总手 int cleft = DrawUnderLineNum(g, volume, unit.Length > 0 ? digit : 0, font, yellowColor, true, 45, top + 60); if (unit.Length > 0) { DrawText(g, unit, yellowColor, font, cleft + 47, top + 60); } //换手 double turnoverRate = latestData.m_turnoverRate; cleft = DrawUnderLineNum(g, turnoverRate, 2, font, yellowColor, true, 155, top + 60); if (turnoverRate > 0) { DrawText(g, "%", yellowColor, font, cleft + 157, top + 60); } //开盘 color = GetPriceColor(open, lastClose); DrawUnderLineNum(g, open, digit, font, color, true, 155, top); //最高 color = GetPriceColor(high, lastClose); DrawUnderLineNum(g, high, digit, font, color, true, 155, top + 20); //最低 color = GetPriceColor(low, lastClose); DrawUnderLineNum(g, low, digit, font, color, true, 155, top + 40); //涨停 double upPrice = lastClose * 1.1; if (securityName != null && securityName.Length > 0) { if (securityName.StartsWith("ST") || securityName.StartsWith("*ST")) { upPrice = lastClose * 1.05; } } if (blackOrWhite) { DrawUnderLineNum(g, upPrice, digit, font, Color.FromArgb(255, 80, 80), true, 45, top + 80); } else { DrawUnderLineNum(g, upPrice, digit, font, Color.FromArgb(0, 0, 0), true, 45, top + 80); } //跌停 double downPrice = lastClose * 0.9; if (securityName != null && securityName.Length > 0) { if (securityName.StartsWith("ST") || securityName.StartsWith("*ST")) { downPrice = lastClose * 0.95; } } if (blackOrWhite) { DrawUnderLineNum(g, downPrice, digit, font, Color.FromArgb(80, 255, 80), true, 155, top + 80); } else { DrawUnderLineNum(g, downPrice, digit, font, Color.FromArgb(0, 0, 0), true, 155, top + 80); } //外盘 double outerVol = latestData.m_outerVol; unit = ""; if (outerVol > 100000000) { outerVol /= 100000000; unit = "亿"; } else if (outerVol > 10000) { outerVol /= 10000; unit = "万"; } if (blackOrWhite) { cleft = DrawUnderLineNum(g, outerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(255, 80, 80), false, 45, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(255, 80, 80), font, cleft + 47, top + 100); } } else { cleft = DrawUnderLineNum(g, outerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(0, 0, 0), false, 45, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(0, 0, 0), font, cleft + 47, top + 100); } } unit = ""; double innerVol = latestData.m_innerVol; if (innerVol > 100000000) { innerVol /= 100000000; unit = "亿"; } else if (innerVol > 10000) { innerVol /= 10000; unit = "万"; } if (blackOrWhite) { //内盘 cleft = DrawUnderLineNum(g, innerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(80, 255, 80), true, 155, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(80, 255, 80), font, cleft + 157, top + 100); } } else { //内盘 cleft = DrawUnderLineNum(g, innerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(0, 0, 0), true, 155, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(0, 0, 0), font, cleft + 157, top + 100); } } } font = new Font("微软雅黑", 14); if (blackOrWhite) { //股票代码 if (securityCode != null && securityCode.Length > 0) { DrawText(g, securityCode, Color.FromArgb(255, 255, 255), font, 2, 4); } //股票名称 if (securityName != null && securityName.Length > 0) { DrawText(g, securityName, Color.FromArgb(80, 255, 255), font, 110, 3); } } else { //股票代码 if (securityCode != null && securityCode.Length > 0) { DrawText(g, securityCode, Color.FromArgb(0, 0, 0), font, 2, 4); } //股票名称 if (securityName != null && securityName.Length > 0) { DrawText(g, securityName, Color.FromArgb(0, 0, 0), font, 110, 3); } } //画边框 Color frameColor = Color.FromArgb(150, 0, 0); if (!blackOrWhite) { frameColor = Color.Black; } Pen framePen = new Pen(frameColor); g.DrawLine(framePen, 0, 0, 0, height); g.DrawLine(framePen, 0, 30, width, 30); g.DrawLine(framePen, 24, 30, 24, top - 2); g.DrawLine(framePen, 0, 140, width, 140); g.DrawLine(framePen, 0, top - 2, width, top - 2); g.DrawLine(framePen, width, 0, width, height); g.DrawLine(framePen, 0, top + 130, width, top + 130); framePen.Dispose(); } myBuffer.Render(); myBuffer.Dispose(); g.Dispose(); }
/// <summary> /// 秒表事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void thisTimer_Tick(object sender, EventArgs e) { if (minuteMode) { if (minuteDatasPos < minuteDatas.Count) { minuteDatasPos++; UpdateDataToGraphMinute(minuteDatas, false); Invalidate(); } } else { SecurityLatestData newData = new SecurityLatestData(); SecurityService.GetLatestData(currentCode, ref newData); if (!newData.equal(lastData) && newData.m_volume > 0) { double close = newData.m_close; double dVolume = 0; if (lastData.m_code.Length > 0) { dVolume = newData.m_volume - lastData.m_volume; } SecurityData securityData = new SecurityData(); securityData.date = (double)((long)newData.m_date / (cycle * 60) * (cycle * 60)); if (cycle != 1440) { securityData.date += (cycle * 60); } securityData.close = close; if (DataSource.RowsCount > 0) { if (DataSource.GetXValue(DataSource.RowsCount - 1) == securityData.date) { if (securityData.close > DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH)) { securityData.high = close; } else { securityData.high = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH); } if (securityData.close < DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW)) { securityData.low = close; } else { securityData.low = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW); } securityData.open = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_OPEN); double oldVolume = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_VOLUME); oldVolume += dVolume; securityData.volume = oldVolume; } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } List <SecurityData> datas = new List <SecurityData>(); datas.Add(securityData); UpdateDataToGraph(datas, false); datas.Clear(); lastData = newData; } } }
/// <summary> /// 重绘方法 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = null; BufferedGraphics myBuffer = null; BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; myBuffer = currentContext.Allocate(pe.Graphics, DisplayRectangle); g = myBuffer.Graphics; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; int width = Width; int height = Height; if (width > 0 && height > 0) { SecurityLatestData ssLatestData = new SecurityLatestData(); SecurityLatestData szLatestData = new SecurityLatestData(); SecurityLatestData cyLatestData = new SecurityLatestData(); if (SecurityService.GetLatestData("000001.SH", ref ssLatestData) > 0 && SecurityService.GetLatestData("399001.SZ", ref szLatestData) > 0 && SecurityService.GetLatestData("399006.SZ", ref cyLatestData) > 0) { } Color titleColor = Color.FromArgb(255, 255, 80); Font font = new Font("微软雅黑", 12); Font indexFont = new Font("微软雅黑", 12); Color grayColor = Color.FromArgb(200, 200, 200); //上证指数 Color indexColor = GetPriceColor(ssLatestData.m_close, ssLatestData.m_lastClose); int left = 1; DrawText(g, "上证", titleColor, font, left, 3); left += 40; Pen grayPen = new Pen(grayColor); g.DrawLine(grayPen, left, 0, left, height); String amount = (ssLatestData.m_amount / 100000000).ToString("0.0") + "亿"; Size amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width / 3 - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; int length = DrawUnderLineNum(g, ssLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += length + (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, ssLatestData.m_close - ssLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); //深证指数 left = width / 3; g.DrawLine(grayPen, left, 0, left, height); indexColor = GetPriceColor(szLatestData.m_close, szLatestData.m_lastClose); DrawText(g, "深证", titleColor, font, left, 3); left += 40; g.DrawLine(grayPen, left, 0, left, height); amount = (szLatestData.m_amount / 100000000).ToString("0.0") + "亿"; amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width * 2 / 3 - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, szLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += length + (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, szLatestData.m_close - szLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); //创业指数 left = width * 2 / 3; g.DrawLine(grayPen, left, 0, left, height); indexColor = GetPriceColor(cyLatestData.m_close, cyLatestData.m_lastClose); DrawText(g, "创业", titleColor, font, left, 3); left += 40; g.DrawLine(grayPen, left, 0, left, height); amount = (cyLatestData.m_amount / 100000000).ToString("0.0") + "亿"; amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, cyLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += (width / 3 - 40 - amountSize.Width) / 4 + length; length = DrawUnderLineNum(g, cyLatestData.m_close - cyLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); g.DrawRectangle(grayPen, new Rectangle(0, 0, width - 1, height - 1)); grayPen.Dispose(); } myBuffer.Render(); myBuffer.Dispose(); g.Dispose(); }
/// <summary> /// 秒表方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { List <String> refreshCodes = new List <string>(); lock (newDataCodes) { if (newDataCodes.Count > 0) { foreach (String key in newDataCodes.Keys) { refreshCodes.Add(key); } newDataCodes.Clear(); } } int refreshCodesSize = refreshCodes.Count; if (refreshCodes.Count > 0) { if (updateCells.Count > 0) { if (updateCells.Count > 10) { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } else { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } updateCells.Clear(); } for (int i = 0; i < refreshCodesSize; i++) { GridRow row = null; String code = refreshCodes[i]; if (rowsMap.ContainsKey(code)) { row = rowsMap[code]; SecurityLatestData lastestData = new SecurityLatestData(); SecurityService.GetLatestData(code, ref lastestData); double lastClose = lastestData.m_lastClose; double diff = 0, diffRange = 0; diff = lastestData.m_close - lastestData.m_lastClose; if (lastestData.m_lastClose != 0) { diffRange = diff / lastestData.m_lastClose; } GridCell cell3 = row.GetCell("colP3"); if (lastestData.m_close != cell3.GetDouble()) { cell3.SetDouble(lastestData.m_close); cell3.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell3); } GridCell cell4 = row.GetCell("colP4"); if (diff != cell4.GetDouble()) { cell4.SetDouble(Convert.ToDouble(LbCommon.GetValueByDigit(diff, 2, true))); cell4.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell4); } GridCell cell5 = row.GetCell("colP5"); if (diffRange != cell5.GetDouble()) { cell5.SetDouble(diffRange); cell5.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell5); } GridCell cell6 = row.GetCell("colP6"); if (lastestData.m_high != cell6.GetDouble()) { cell6.SetDouble(lastestData.m_high); cell6.Style.TextColor = GetPriceColor(lastestData.m_high, lastClose); updateCells.Add(cell6); } GridCell cell7 = row.GetCell("colP7"); if (lastestData.m_low != cell7.GetDouble()) { cell7.SetDouble(lastestData.m_low); cell7.Style.TextColor = GetPriceColor(lastestData.m_low, lastClose); updateCells.Add(cell7); } GridCell cell8 = row.GetCell("colP8"); if (lastestData.m_open != cell8.GetDouble()) { cell8.SetDouble(lastestData.m_open); cell8.Style.TextColor = GetPriceColor(lastestData.m_open, lastClose); updateCells.Add(cell8); } GridCell cell9 = row.GetCell("colP9"); if (lastestData.m_volume != cell9.GetDouble()) { cell9.SetDouble(lastestData.m_volume); updateCells.Add(cell9); } GridCell cell10 = row.GetCell("colP10"); if (lastestData.m_amount != cell10.GetDouble()) { cell10.SetDouble(lastestData.m_amount); updateCells.Add(cell10); } GridCell cell11 = row.GetCell("colP11"); if (lastestData.m_buyPrice1 != cell11.GetDouble()) { cell11.SetDouble(lastestData.m_buyPrice1); cell11.Style.TextColor = GetPriceColor(lastestData.m_buyPrice1, lastClose); updateCells.Add(cell11); } GridCell cell12 = row.GetCell("colP12"); if (lastestData.m_buyVolume1 != cell12.GetDouble()) { cell12.SetDouble(lastestData.m_buyVolume1); updateCells.Add(cell12); } GridCell cell13 = row.GetCell("colP13"); if (lastestData.m_sellPrice1 != cell13.GetDouble()) { cell13.SetDouble(lastestData.m_sellPrice1); cell13.Style.TextColor = GetPriceColor(lastestData.m_sellPrice1, lastClose); updateCells.Add(cell13); } GridCell cell14 = row.GetCell("colP14"); if (lastestData.m_sellVolume1 != cell14.GetDouble()) { cell14.SetDouble(lastestData.m_sellVolume1); updateCells.Add(cell14); } } } if (updateCells.Count > 0) { for (int i = 0; i < updateCells.Count; i++) { updateCells[i].Style.BackColor = Color.FromArgb(50, 255, 255, 255); } UpdateGrid(); Invalidate(); } } }