/// <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(); } } }
/// <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; //画线交易 for (int i = 0; i < tradeLines.Count; i++) { TradeLine trendLine = tradeLines[i]; if (trendLine.Bs == "多头") { if (close <= trendLine.Value) { trendLine.Bs += "已成交"; } } else if (trendLine.Bs == "空头") { if (close >= trendLine.Value) { trendLine.Bs += "已成交"; } } } 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> public GridExtend() { ShowHScrollBar = true; ShowVScrollBar = true; RowStyle.HoveredBackColor = Color.Empty; AddColumn(new GridColumn("colP1", "股票代码", 100)); AddColumn(new GridColumn("colP2", "股票名称", 100)); AddColumn(new GridColumn("colP3", "最新价", 90)); AddColumn(new GridColumn("colP4", "涨跌额", 90)); AddColumn(new GridColumn("colP5", "涨跌幅", 90)); AddColumn(new GridColumn("colP6", "最高价", 90)); AddColumn(new GridColumn("colP7", "最低价", 90)); AddColumn(new GridColumn("colP8", "开盘价", 90)); AddColumn(new GridColumn("colP9", "成交量", 120)); AddColumn(new GridColumn("colP10", "成交额", 110)); AddColumn(new GridColumn("colP11", "买一价", 90)); AddColumn(new GridColumn("colP12", "买一量", 110)); AddColumn(new GridColumn("colP13", "卖一价", 90)); AddColumn(new GridColumn("colP14", "卖一量", 110)); GetColumn("colP9").CellAlign = HorizontalAlign.Left; GetColumn("colP10").CellAlign = HorizontalAlign.Left; GetColumn("colP12").CellAlign = HorizontalAlign.Left; GetColumn("colP14").CellAlign = HorizontalAlign.Left; BeginUpdate(); //绑定数据 foreach (Security security in SecurityService.codedMaps.Values) { GridRow row = new GridRow(); AddRow(row); row.Height = 30; row.AddCell("colP1", new GridStringCell(security.m_code)); row.AddCell("colP2", new GridStringCell(security.m_name)); row.AddCell("colP3", new GridDoubleCell()); row.AddCell("colP4", new GridDoubleCell()); row.AddCell("colP5", new GridPercentCell()); row.AddCell("colP6", new GridDoubleCell()); row.AddCell("colP7", new GridDoubleCell()); row.AddCell("colP8", new GridDoubleCell()); row.AddCell("colP9", new GridLongCell()); row.AddCell("colP10", new GridLongCell()); row.AddCell("colP11", new GridDoubleCell()); row.AddCell("colP12", new GridLongCell()); row.AddCell("colP13", new GridDoubleCell()); row.AddCell("colP14", new GridLongCell()); rowsMap[security.m_code] = row; for (int i = 0; i < row.Cells.Count; i++) { GridCellStyle cellStyle = new GridCellStyle(); if (i > 1) { cellStyle.Align = HorizontalAlign.Right; } if (Program.BlackOrWhite) { cellStyle.TextColor = Color.FromArgb(255, 255, 255); } else { cellStyle.TextColor = Color.Black; } cellStyle.Font = new Font("微软雅黑", 12); row.Cells[i].Style = cellStyle; } } EndUpdate(); SecurityService.m_listener = this; SecurityService.Start(); System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = 300; timer.Enabled = true; if (!Program.BlackOrWhite) { for (int i = 0; i < Columns.Count; i++) { GridColumn gridColumn = Columns[i]; gridColumn.BackColor = Color.White; gridColumn.TextColor = Color.Black; } BackColor = Color.White; GridRowStyle gridRowStyle = RowStyle; gridRowStyle.BackColor = Color.White; gridRowStyle.TextColor = Color.Black; gridRowStyle.SelectedBackColor = Color.FromArgb(200, 200, 200); gridRowStyle.HoveredBackColor = Color.Empty; } }
/// <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 (!Program.BlackOrWhite) { g.Clear(Color.White); } 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); if (!Program.BlackOrWhite) { titleColor = Color.Black; } //上证指数 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(); }