// LessonA49.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

#define WM_MOUSEWHEEL 0x020A

extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();

CChart chart;
HWND hWnd;

// 绘图函数
void	MyDraw()
{
	HDC hDC = GetDC(hWnd);
	
	RECT rt;
	GetClientRect(hWnd, &rt);
	rt.top = (rt.top+rt.bottom)/2;
	chart.OnDraw(hDC, rt);
	
	ReleaseDC(hWnd, hDC);
}

int main(int argc, char* argv[])
{
	hWnd = GetConsoleWindow();
	
	double pi = 4.0*atan(1.0);
	int perioid = 360;
	for(int i=0; i<4*perioid; ++i)
	{
		chart.AddPoint2D(i, 1.4*sin(i*2.0*pi/perioid));
	}
	chart.SetTitle(_T("在控制台窗口中绘图"));
	chart.SetBkgndColor(RGB(0, 0, 0));
	chart.SetTitleColor(RGB(245, 245, 245));
	
	MyDraw();
	
	printf("控制台窗口绘图!\n");

	//消息处理方案1
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
	HANDLE Hout = GetStdHandle(STD_OUTPUT_HANDLE);
	INPUT_RECORD inp;
	DWORD recnum;
	
 	POINT point;
 	UINT message;
	bool loop = true;
	while(loop)
	{
		ReadConsoleInput(hIn, &inp, 1, &recnum);
		switch(inp.EventType)
		{
		case FOCUS_EVENT:
			MyDraw();
			break;
 		case KEY_EVENT:
 			if(inp.Event.KeyEvent.uChar.AsciiChar=='c' || inp.Event.KeyEvent.uChar.AsciiChar=='C')loop = false;
 			break;
		case MOUSE_EVENT:
			if(inp.Event.MouseEvent.dwEventFlags == 0)//单击（包括按下或释放）
			{
				if(inp.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)//左键按下
				{
					message = WM_LBUTTONDOWN;
				}
				else if(inp.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED)//右键按下
				{
					message = WM_CONTEXTMENU;
				}
				else if(inp.Event.MouseEvent.dwButtonState == FROM_LEFT_2ND_BUTTON_PRESSED)//滚轮
				{
					message = WM_MOUSEWHEEL;
				}
				//释放的时候：
				else if(inp.Event.MouseEvent.dwButtonState == 0)
				{
					message = WM_LBUTTONUP;
				}
			}
			else if(inp.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)//双击
			{
				message = WM_LBUTTONDBLCLK;
			}
			else if(inp.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)//移动
			{
				message = WM_MOUSEMOVE;
			}
			else
			{
				message = WM_MOUSEMOVE;
			}
			GetCursorPos(&point);
			ScreenToClient(hWnd, &point);
			if(chart.OnEvent(hWnd, message, inp.Event.MouseEvent.dwControlKeyState, point.x + (point.y<<16)))MyDraw();
			break;
		}
	}
	
	CloseHandle(hIn);
	CloseHandle(Hout);

	return 0;
}
