#include "stdafx.h"
#include "KMAssist.h"

// 定义鼠标移动的时间间隔和距离系数
const int MouseMoveInterval = 10;
const double MouseMoveDistanceFactor = 1.5;

// 定义辅助函数，计算两点之间的距离
double Distance(POINT startPt, POINT endPt)
{
	return sqrt(pow(startPt.x - endPt.x, 2) + pow(startPt.y - endPt.y, 2));
}

double Distance(double x1, double y1, double x2, double y2)
{
	return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}

void KMAssist::MouseMove(POINT endPt, bool bSetCurPos)
{
	// 当前坐标;
	POINT startPt;
	::GetCursorPos(&startPt);

	// 计算两点距离;
	double distance = Distance(startPt.x, startPt.y, endPt.x, endPt.y);

	// 计算鼠标移动的偏移量
	int dx = (int)((endPt.x - startPt.x) * MouseMoveDistanceFactor / distance);
	int dy = (int)((endPt.y - startPt.y) * MouseMoveDistanceFactor / distance);

	// 计算鼠标相对屏幕坐标的绝对坐标值;
	static long fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;
	static long fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;
	// 使用浮点数，计算出来的值转Long会更精确;
	double fx = (double)endPt.x * (65535.0f / fScreenWidth);
	double fy = (double)endPt.y * (65535.0f / fScreenHeight);

	INPUT Input = { 0 };
	Input.type = INPUT_MOUSE;
	Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = static_cast<LONG>(fx);
	Input.mi.dy = static_cast<LONG>(fy);
	::SendInput(1, &Input, sizeof(INPUT));

	// Move之后设置当前鼠标位置;
	if (bSetCurPos)::SetCursorPos(endPt.x, endPt.y);
}

void KMAssist::MouseMove(int x, int y, bool bSetCurPos)
{
}

void KMAssist::MouseClick()
{
}

void KMAssist::MouseClick(POINT pt)
{
}

void KMAssist::MouseDbClick()
{
}

void KMAssist::MouseDbClick(POINT pt)
{
}

void KMAssist::MouseRClick()
{
}

void KMAssist::MouseRClick(POINT pt)
{
}

void KMAssist::MouseDrag(POINT pt)
{
}

void KMAssist::SendKey(DWORD key, BOOL bCtrl, BOOL bAtl, BOOL bShift)
{
}
