#include <windows.h>
//包含炫彩界面库文件
#include "xcgui.h"
#pragma comment(lib, "XCGUI.lib")

//包含CChart库文件
#include "Chart.h"
#if defined(_UNICODE) || defined(UNICODE)
#	pragma comment(lib,"CChartu.lib")
#else
#	pragma comment(lib,"CChart.lib")
#endif
using namespace NsCChart;

#include <math.h>

class CMyWindow
{
public:
    HWINDOW m_hWindow;
	CChart m_Chart;
    CMyWindow()
	{
        Init();
    }
    void Init()
    {
        m_hWindow = XWnd_Create(0, 0, 800, 600, L"炫彩界面库窗口",NULL, xc_window_style_default );
        XBtn_SetType(XBtn_Create(10, 5, 60, 20,L"close",m_hWindow),button_type_close);

		if(m_hWindow)
		{
			HWND hWnd = XWnd_GetHWND(m_hWindow);
			
			m_Chart.CreateChart(kTypeXY, hWnd);
			
			const int nLen = 501;
			double pX[nLen], pY[nLen];
			int i, dataID;
			for(i=0; i<nLen; i++)
			{
				pX[i]=2.0/(nLen-1.0)*i;
				pY[i]=2*sin(pX[i]*8.0*3.1415926536) + (2.0*rand()/RAND_MAX-1.0)*0.3;
			}
			dataID = m_Chart.AddCurve(pX, pY, nLen);
			m_Chart.SetDataTitle(_T("Test data 1"), 0, 0);
			
			for(i=0; i<nLen; i++)
			{
				pX[i]=2.0/(nLen-1.0)*i;
				pY[i]=(i-nLen/2.0)*(i-nLen/2.0)/nLen/nLen*20.0 + (2.0*rand()/RAND_MAX-1.0)*0.1;
			}
			dataID = m_Chart.AddCurve(pX, pY, nLen);
			m_Chart.SetDataTitle(_T("Test data 2"), 1, 0);
			m_Chart.SetTitle(_T("在炫彩界面库中绘图"));
			
			XWnd_RegEventCPP(m_hWindow, WM_PAINT, &CMyWindow::OnWndDrawWindow);
			XWnd_RegEventCPP(m_hWindow,WM_LBUTTONDOWN,&CMyWindow::OnWndLButtonDown);
			XWnd_RegEventCPP(m_hWindow,WM_LBUTTONUP,&CMyWindow::OnWndLButtonUp);//注册事件
	        XWnd_RegEventCPP1(m_hWindow,WM_LBUTTONDBLCLK,&CMyWindow::OnWndLButtonDblClk);//注册事件
			XWnd_RegEventCPP(m_hWindow,WM_RBUTTONDOWN,&CMyWindow::OnWndRButtonDown);
			XWnd_RegEventCPP(m_hWindow,WM_MOUSEMOVE,&CMyWindow::OnWndMouseMove);
        }
 
        XWnd_ShowWindow(m_hWindow,SW_SHOW);
    }

	
	int OnWndDrawWindow(HDRAW hDraw,BOOL *pbHandled)
	{
		RECT rect;
		XWnd_GetBodyRect(m_hWindow, &rect);
		HDC hDC = XDraw_GetHDC(hDraw);

		XWnd_DrawWindow(m_hWindow, hDraw);
		m_Chart.OnDraw(hDC, rect);
		*pbHandled=TRUE;
		return 0;
	}
	int OnWndLButtonDown(UINT nFlags,POINT *pPt,BOOL *pbHandled)
	{
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		
		if(m_Chart.OnLButtonDown(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
	}
    int OnWndLButtonUp(UINT flags,POINT *pPt,BOOL *pbHandled)
    {
        HWND hWnd = XWnd_GetHWND(m_hWindow);
		
		if(m_Chart.OnLButtonUp(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
    }
    int OnWndLButtonDblClk(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
    {
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		
		if(m_Chart.OnLButtonDblClk(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
    }
	int OnWndRButtonDown(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
    {
		HWND hWnd = XWnd_GetHWND(m_hWindow);

		ClientToScreen(hWnd, pPt);
		
		if(m_Chart.OnContextMenu(NULL, hWnd, *pPt))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
    }
	int OnWndMouseMove(HWINDOW hWindow,UINT nFlags,POINT *pPt, BOOL *pbHandled)
    {
		HWND hWnd = XWnd_GetHWND(m_hWindow);
		
		if(m_Chart.OnMouseMove(hWnd, *pPt, 0))
		{
			XWnd_RedrawWnd(m_hWindow);
		}
		*pbHandled = FALSE;
		return 0;
    }
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    XInitXCGUI();
    CMyWindow  MyWindow;
    XRunXCGUI();
    XExitXCGUI();
    return 0;
}

/*

  //CXEventMsg  : C++消息事件接口类
  //CMyWnd      : 我的窗口类
  class CMyWnd
  {
  public:
  HWINDOW m_hWindow; //窗口句柄
  CChart m_Chart;
  
	BOOL Create() //创建窗口和按钮
    {
	m_hWindow=XWnd_Create(0,0,800,600,L"炫彩界面库-窗口"); //创建窗口
	if(m_hWindow)
	{
	HWND hWnd = XWnd_GetHWND(m_hWindow);
	
	  m_Chart.SetType(kTypeXY, hWnd);
	  
		const int nLen = 501;
		double pX[nLen], pY[nLen];
		int i, dataID;
		for(i=0; i<nLen; i++)
		{
		pX[i]=2.0/(nLen-1.0)*i;
		pY[i]=2*sin(pX[i]*8.0*3.1415926536) + (2.0*rand()/RAND_MAX-1.0)*0.3;
		}
		dataID = m_Chart.AddCurve(pX, pY, nLen);
		m_Chart.SetDataTitle("Test data 1", 0, 0);
		
		  for(i=0; i<nLen; i++)
		  {
		  pX[i]=2.0/(nLen-1.0)*i;
		  pY[i]=(i-nLen/2.0)*(i-nLen/2.0)/nLen/nLen*20.0 + (2.0*rand()/RAND_MAX-1.0)*0.1;
		  }
		  dataID = m_Chart.AddCurve(pX, pY, nLen);
		  m_Chart.SetDataTitle("Test data 2", 1, 0);
		  m_Chart.SetTitle("在炫彩界面库中绘图");
		  
			XWnd_RegEventCPP(m_hWindow, WM_PAINT, &CMyWnd::OnWndPaint);
			XWnd_RegEventC(m_hWindow, WM_LBUTTONDOWN, &CMyWnd::OnWndLButtonDown);
			XWnd_RegEventC(m_hWindow, WM_LBUTTONUP, &CMyWnd::OnWndLButtonUp);
			XWnd_RegEventC(m_hWindow, XWM_MOUSEDBCLICK, &CMyWnd::OnWndLButtonDblClk);
			XWnd_RegEventC(m_hWindow, WM_MOUSEMOVE, &CMyWnd::OnWndMouseMove);
			XWnd_RegEventC(m_hWindow, WM_KEYDOWN, &CMyWnd::OnWndKeyDown);
			XWnd_RegEventC(m_hWindow, WM_RBUTTONDOWN, &CMyWnd::OnWndRButtonDown);
			
            XWnd_ShowWindow(m_hWindow,SW_SHOW); //显示窗口
            return true;
        }
        return false;
    }
private:
	RECT rect;
	int dx, dy;
	
	int CALLBACK OnWndPaint(HDRAW hDraw,BOOL *pbHandled)
	{
		XWnd_GetDrawRect(m_hWindow, &rect);
		HDC hDC = XDraw_GetHDC(hDraw);
		
		XDraw_GetOffset(hDraw, &dx, &dy);
		rect.left += dx;
		rect.top += dy;
		rect.right -= dx;
		rect.bottom -= dx;
		m_Chart.OnDraw(hDC, rect);
		pbHandled=TRUE;
		return 0;
	}
	
	BOOL OnWndLButtonDown(HWINDOW hWindow,UINT flags,POINT *pPt)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		POINT point = *pPt;
		point.x += dx;
		point.y += dy;
		
		if(m_Chart.OnLButtonDown(hWnd, point))
		{
			XWnd_RedrawWnd(hWindow);
		}
		return FALSE;
	}
	BOOL OnWndLButtonUp(HWINDOW hWindow,UINT flags,POINT *pPt)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		POINT point = *pPt;
		point.x += dx;
		point.y += dy;
		
		if(m_Chart.OnLButtonUp(hWnd, point))
		{
			XWnd_RedrawWnd(hWindow);
		}

		return FALSE;
	}
	BOOL OnWndLButtonDblClk(HWINDOW hWindow,POINT *pPt)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		POINT point = *pPt;
		point.x += dx;
		point.y += dy;
		
		if(m_Chart.OnLButtonDblClk(hWnd, point))
		{
			XWnd_RedrawWnd(hWindow);
		}
		if(GetCapture())
		{
			ReleaseCapture();
		}
	
		return FALSE;
	}
	BOOL OnWndMouseMove(HWINDOW hWindow,UINT flags,POINT *pPt)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		POINT point = *pPt;
		point.x += dx;
		point.y += dy;
		
		if(m_Chart.OnMouseMove(hWnd, point))
		{
			XWnd_RedrawWnd(hWindow);
		}
		return FALSE;
	}
	BOOL OnWndKeyDown(HWINDOW hWindow,WPARAM wParam,LPARAM lParam)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		UINT	key = wParam;
		
		if(m_Chart.OnKeyDown(hWnd, key))
		{
			XWnd_RedrawWnd(hWindow);
		}
		return FALSE;
	}
	BOOL OnWndRButtonDown(HWINDOW hWindow,UINT flags,POINT *pPt)
	{
		HWND hWnd = XWnd_GetHWnd(hWindow);
		POINT point = *pPt;
		point.x += dx;
		point.y += dy;
		ClientToScreen(hWnd, &point);
		
		if(m_Chart.OnContextMenu(NULL, hWnd, point))
		{
			XWnd_RedrawWnd(hWindow);
		}
		return FALSE;
	}
};

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow)
{
    XInitXCGUI(); //初始化
	
    CMyWnd  MyWnd;
    if(MyWnd.Create())
    {
        XRunXCGUI(); //运行
    }
	XExitXCGUI();
    return 0;
}
*/