加入了十字光标,选中的日期k线数据在右侧表现 自定义光标监听类 并发坐标消息
#define MESSAGE_UPDATEPOS WM_USER+002
struct pointlf
{
double x;
double y;
};
class CCustomCursorListener : public CChartCursorListener
{
public:
void OnCursorMoved(CChartCursor *pCursor, double xValue, double yValue)
{
pointlf pt;
pt.x = xValue;
pt.y = yValue;
SendMessage(m_hwnd,MESSAGE_UPDATEPOS,DWORD(&pt),0);
// Do something with the string...
}
double x,y;
void GetHwnd(HWND hwnd)
{
m_hwnd = hwnd;
}
HWND m_hwnd;
};
在控件面板初始化代码中加入
CChartCrossHairCursor* pCrossHair = m_ChartCtrl.CreateCrossHairCursor();
HWND hWnd = this->GetSafeHwnd();
m_pCursorListener = new CCustomCursorListener;
m_pCursorListener->GetHwnd(hWnd);
pCrossHair->RegisterListener(m_pCursorListener);
注册监听事件
控件头文件中加入
afx_msg LRESULT OnUpdateData(WPARAM wp,LPARAM lp);
头文件中用到的变量
CChartCtrl m_ChartCtrl;
CCustomCursorListener* m_pCursorListener;
CChartCandlestickSerie *pStickSerie;
cpp中加入
BEGIN_MESSAGE_MAP(CStocknoteDlg, CDialog)
//{{AFX_MSG_MAP(CStocknoteDlg)
ON_MESSAGE(MESSAGE_UPDATEPOS,OnUpdateData)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
LRESULT CStocknoteDlg::OnUpdateData(WPARAM wp,LPARAM lp)
{
CString s;
pointlf* pa =(pointlf*)wp;
CStatic* pStatic;pStatic = (CStatic*)GetDlgItem(IDC_MSG);
// CChartCandlestickSerie *pStickSerie =(CChartCandlestickSerie *) m_ChartCtrl.GetSerie(0);
if(pStickSerie != NULL)
{
int cnt = pStickSerie->GetPointsCount();
for(int i = 0; i < cnt ; i++)
{
SChartCandlestickPoint stickPt = pStickSerie->GetPoint(i);
if(stickPt.Low > pa->y || stickPt.High < pa->x)continue;
if(stickPt.XVal > pa->x+0.2|| stickPt.XVal < pa->x-0.2)continue;
s.Format("最低 = %.2f,最高 = %.2f,开盘 = %.2f,收盘 = %.2f",stickPt.Low,stickPt.High,stickPt.Open,stickPt.Close);
break;
}
pStatic->SetWindowText(s);
}else
{
s.Format("没有数据!");
pStatic->SetWindowText(s);
}
return 1;
}