/// <summary> /// 把指定日期K线的高开低收数据设置到画面的数值控件上 /// </summary> /// <param name="date">日期</param> private void SetSpinValue(double date) { ChartA chart = m_chart.Chart; CTable dataSource = chart.DataSource; int rowsCount = dataSource.RowsCount; if (rowsCount > 0) { int index = dataSource.GetRowIndex(date); if (index < 1) { index = rowsCount - 1; } SpinA spinOpen = GetSpin("txtOpen"); SpinA spinClose = GetSpin("txtClose"); SpinA spinHigh = GetSpin("txtHigh"); SpinA spinLow = GetSpin("txtLow"); SpinA spinVolume = GetSpin("txtVolume"); SpinA spinAmount = GetSpin("txtAmount"); LabelA lblDate = GetLabel("lblDate"); spinOpen.Value = dataSource.Get2(index, KeyFields.OPEN_INDEX); spinClose.Value = dataSource.Get2(index, KeyFields.CLOSE_INDEX); spinHigh.Value = dataSource.Get2(index, KeyFields.HIGH_INDEX); spinLow.Value = dataSource.Get2(index, KeyFields.LOW_INDEX); spinVolume.Value = dataSource.Get2(index, KeyFields.VOL_INDEX); spinAmount.Value = dataSource.Get2(index, KeyFields.AMOUNT_INDEX); DateTime dateTime = CStrA.ConvertNumToDate(date); int m_cycle = m_chart.Cycle; if (m_cycle <= 60) { lblDate.Text = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); } else { lblDate.Text = dateTime.ToString("yyyy-MM-dd"); } m_window.Invalidate(); } }
/// <summary> /// 重绘背景方法 /// </summary> /// <param name="paint">绘图对象</param> /// <param name="clipRect">裁剪区域</param> public override void OnPaintBackground(CPaint paint, RECT clipRect) { int width = Width; int height = Height; if (width > 0 && height > 0) { ChartA chart = m_chart.Chart; //十字线出现时进行绘制 if (chart.ShowCrossLine) { CTable dataSource = chart.DataSource; //获取鼠标停留索引 int crossStopIndex = chart.CrossStopIndex; if (dataSource.RowsCount > 0) { if (crossStopIndex < 0) { crossStopIndex = chart.FirstVisibleIndex; } if (crossStopIndex > chart.LastVisibleIndex) { crossStopIndex = chart.LastVisibleIndex; } } else { crossStopIndex = -1; } //获取K线和成交量 RECT rectangle = new RECT(0, 0, width, height); long win32Color = COLOR.EMPTY; paint.FillRect(GetPaintingBackColor(), rectangle); paint.DrawRect(GetPaintingBorderColor(), 1, 0, rectangle); //画关闭按钮 long lineColor = CDraw.PCOLORS_LINECOLOR; paint.DrawLine(lineColor, 2, 0, width - 6, 4, width - 14, 12); paint.DrawLine(lineColor, 2, 0, width - 6, 12, width - 14, 4); //创建字体 FONT font = new FONT("SimSun", 14, false, false, false); FONT lfont = new FONT("Arial", 12, false, false, false); FONT nfont = new FONT("Arial", 14, true, false, false); //画日期 CDraw.DrawText(paint, "时 间", CDraw.PCOLORS_FORECOLOR, font, rectangle.left + 25, rectangle.top + 2); DateTime date = DateTime.Now; if (crossStopIndex >= 0) { double dateNum = dataSource.GetXValue(crossStopIndex); if (dateNum != 0) { date = CStrA.ConvertNumToDate(dateNum); } String dateStr = ""; int cycle = m_chart.Cycle; if (cycle <= 1) { dateStr = date.ToString("hh:mm"); } else if (cycle >= 5 && cycle <= 60) { dateStr = date.ToString("MM-dd hh:mm"); } else { dateStr = date.ToString("yyyy-MM-dd"); } SIZE dtSize = paint.TextSize(dateStr, lfont); CDraw.DrawText(paint, dateStr, CDraw.PCOLORS_FORECOLOR3, lfont, rectangle.left + width / 2 - dtSize.cx / 2, rectangle.top + 20); //获取值 double close = 0, high = 0, low = 0, open = 0, amount = 0; if (crossStopIndex >= 0) { close = dataSource.Get2(crossStopIndex, KeyFields.CLOSE_INDEX); high = dataSource.Get2(crossStopIndex, KeyFields.HIGH_INDEX); low = dataSource.Get2(crossStopIndex, KeyFields.LOW_INDEX); open = dataSource.Get2(crossStopIndex, KeyFields.OPEN_INDEX); amount = dataSource.Get2(crossStopIndex, KeyFields.AMOUNT_INDEX); } if (double.IsNaN(close)) { close = 0; } if (double.IsNaN(high)) { high = 0; } if (double.IsNaN(low)) { low = 0; } if (double.IsNaN(open)) { open = 0; } if (double.IsNaN(amount)) { amount = 0; } double rate = 1; double lastClose = 0; if (crossStopIndex > 1) { lastClose = dataSource.Get2(crossStopIndex - 1, KeyFields.CLOSE_INDEX); if (cycle == 0) { lastClose = m_chart.LatestData.m_lastClose; } if (!double.IsNaN(lastClose)) { if (lastClose != 0) { rate = (close - lastClose) / lastClose; } } } //开盘价 String openStr = double.IsNaN(open) ? "" : CStr.GetValueByDigit(open, m_digit).ToString(); SIZE tSize = paint.TextSize(openStr, nfont); CDraw.DrawText(paint, openStr, CDraw.GetPriceColor(open, lastClose), nfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 60); //最高价 String highStr = double.IsNaN(high) ? "" : CStr.GetValueByDigit(high, m_digit).ToString(); tSize = paint.TextSize(highStr, nfont); CDraw.DrawText(paint, highStr, CDraw.GetPriceColor(high, lastClose), nfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 100); //最低价 String lowStr = double.IsNaN(low) ? "" : CStr.GetValueByDigit(low, m_digit).ToString(); tSize = paint.TextSize(lowStr, nfont); CDraw.DrawText(paint, lowStr, CDraw.GetPriceColor(low, lastClose), nfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 140); //最低价 String closeStr = double.IsNaN(close) ? "" : CStr.GetValueByDigit(close, m_digit).ToString(); tSize = paint.TextSize(closeStr, nfont); CDraw.DrawText(paint, closeStr, CDraw.GetPriceColor(close, lastClose), nfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 180); //成交量 String unit = ""; if (amount > 100000000) { amount /= 100000000; unit = "亿"; } else if (amount > 10000) { amount /= 10000; unit = "万"; } String amountStr = CStr.GetValueByDigit(amount, 2) + unit; tSize = paint.TextSize(amountStr, lfont); CDraw.DrawText(paint, amountStr, CDraw.PCOLORS_FORECOLOR3, lfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 220); //涨幅 String rangeStr = double.IsNaN(rate) ? "0.00%" : rate.ToString("0.00%"); tSize = paint.TextSize(rangeStr, nfont); CDraw.DrawText(paint, rangeStr, CDraw.GetPriceColor(close, lastClose), lfont, rectangle.left + width / 2 - tSize.cx / 2, rectangle.top + 260); } long whiteColor = CDraw.PCOLORS_FORECOLOR; CDraw.DrawText(paint, "开 盘", whiteColor, font, rectangle.left + 25, rectangle.top + 40); CDraw.DrawText(paint, "最 高", whiteColor, font, rectangle.left + 25, rectangle.top + 80); CDraw.DrawText(paint, "最 低", whiteColor, font, rectangle.left + 25, rectangle.top + 120); CDraw.DrawText(paint, "收 盘", whiteColor, font, rectangle.left + 25, rectangle.top + 160); CDraw.DrawText(paint, "金 额", whiteColor, font, rectangle.left + 25, rectangle.top + 200); CDraw.DrawText(paint, "涨 幅", whiteColor, font, rectangle.left + 25, rectangle.top + 240); } } }
/// <summary> /// 调用控件线程方法 /// </summary> /// <param name="args">参数</param> public override void OnInvoke(object args) { base.OnInvoke(args); if (args != null) { CMessage message = (CMessage)args; if (message.m_requestID == m_requestID) { //分时数据 if (message.m_functionID == QuoteService.FUNCTIONID_QUOTE_PUSHLATESTDATA) { LatestDataInfo dataInfo = new LatestDataInfo(); List <SecurityLatestData> datas = new List <SecurityLatestData>(); QuoteService.GetLatestDatas(ref dataInfo, datas, message.m_body, message.m_bodyLength); SecurityLatestData latestData = datas[0]; if (latestData != null && latestData.m_securityCode == m_securityCode && !latestData.Equal(m_latestData)) { m_latestData = latestData; //设置保留小数的位数 int digit = 2; if (m_latestData.m_securityCode.StartsWith("1") || m_latestData.m_securityCode.StartsWith("5")) { digit = 3; } m_chart.Digit = digit; m_chart.RefreshData(); } } //LV2分时数据 else if (message.m_functionID == QuoteService.FUNCTIONID_QUOTE_PUSHLATESTDATALV2) { LatestDataInfoLV2 dataInfo = new LatestDataInfoLV2(); List <SecurityLatestDataLV2> datas = new List <SecurityLatestDataLV2>(); QuoteService.GetLatestDatasLV2(ref dataInfo, datas, message.m_body, message.m_bodyLength); SecurityLatestDataLV2 latestDataLV2 = datas[0]; if (latestDataLV2 != null && latestDataLV2.m_securityCode == m_securityCode && !latestDataLV2.Equal(m_latestDataLV2)) { m_latestDataLV2 = latestDataLV2; } } //成交数据 else if (message.m_functionID == QuoteService.FUNCTIONID_QUOTE_PUSHTRANSACTIONDATA) { String securityCode = ""; List <TransactionData> transactionDatas = new List <TransactionData>(); QuoteService.GetTransactionDatas(ref securityCode, transactionDatas, message.m_body, message.m_bodyLength); int transactionDatasSize = transactionDatas.Count; for (int i = 0; i < transactionDatasSize; i++) { TransactionData transactionData = transactionDatas[i]; DateTime date = CStrA.ConvertNumToDate(transactionData.m_date); GridRow row = new GridRow(); m_gridTransaction.InsertRow(0, row); TransactionDateCell dateCell = new TransactionDateCell(); dateCell.Text = date.ToString("HH:mm:ss"); row.AddCell(0, dateCell); GridCellStyle dateCellStyle = new GridCellStyle(); dateCellStyle.BackColor = COLOR.EMPTY; dateCellStyle.Font = new FONT("SimSun", 14, true, false, false); dateCellStyle.ForeColor = CDraw.PCOLORS_FORECOLOR2; dateCell.Style = dateCellStyle; TransactionDataDoubleCell priceCell = new TransactionDataDoubleCell(); priceCell.Digit = 2; priceCell.SetDouble(transactionData.m_price); row.AddCell(1, priceCell); GridCellStyle priceCellStyle = new GridCellStyle(); priceCellStyle.BackColor = COLOR.EMPTY; priceCellStyle.Font = new FONT("SimSun", 14, true, false, false); priceCellStyle.ForeColor = CDraw.GetPriceColor(transactionData.m_price, m_latestData.m_lastClose); priceCell.Style = priceCellStyle; TransactionDataDoubleCell volumeCell = new TransactionDataDoubleCell(); volumeCell.SetDouble(transactionData.m_volume); row.AddCell(2, volumeCell); GridCellStyle volumeCellStyle = new GridCellStyle(); volumeCellStyle.BackColor = COLOR.EMPTY; volumeCellStyle.Font = new FONT("SimSun", 14, true, false, false); if (transactionData.m_type == 0) { volumeCellStyle.ForeColor = CDraw.PCOLORS_FORECOLOR; } else if (transactionData.m_type == 1) { volumeCellStyle.ForeColor = CDraw.PCOLORS_UPCOLOR; } else { volumeCellStyle.ForeColor = CDraw.PCOLORS_DOWNCOLOR; } volumeCell.Style = volumeCellStyle; } m_gridTransaction.Update(); } } Invalidate(); } }