Files
DNF_MACRO/2400MW/WindowsFormsApp1/MainForm.cs
2020-03-09 11:55:29 +09:00

378 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using OpenCvSharp;
using System.Diagnostics;
using DNFMacro.Data;
using Tesseract;
namespace DNFMacro
{
/// <summary>
/// 던파 매크로 폼
/// 외부 라이브러리는 ImportLib에 partial로 분할함
/// </summary>
public partial class MainForm : Form
{
//이미지 처리 클래스
//ImageMatch.Matching imageMatch = new ImageMatch.Matching();
//던파 프로세스
Process _process;
//던파 화면 저장 클래스
Graphics _graphicsdata;
//던파 화면 저장할 클래스
Bitmap copybmp;
/// <summary>
/// 이미지를 저장하면서 화면에 띄움
/// </summary>
Bitmap _threadCopybmp
{
get
{
return copybmp;
}
set
{
copybmp = value;
pictureBoxIpl화면.Image = value;
}
}
/// <summary>
/// 이미지 지속적으로 저장할 쓰레드
/// </summary>
Thread _imgSave;
/// <summary>
/// 던파 설정이 완료되면 true
/// </summary>
public bool settingChecker = false;
/// <summary>
/// CheckBox뷰어와 연동되며 화면을 띄울때 true
/// </summary>
public bool imgviewerChecker = false;
/// <summary>
/// 던파의 화면 위치를 저장하는 함수
/// </summary>
Point st = new Point();
private void imgSave()
{
while (true)
{
if(settingChecker)
{
Thread.Sleep(100);
_graphicsdata = Graphics.FromHwnd(_process.MainWindowHandle);
st = new Point();
WinFunction.ScreenToClient(_process.MainWindowHandle, ref st);
//찾은 플레이어 창 크기 및 위치를 가져옵니다.
Rectangle rect = Rectangle.Round(_graphicsdata.VisibleClipBounds);
if (copybmp == null)
copybmp = new Bitmap(rect.Width, rect.Height);
//비트맵을 바탕으로 그래픽스 함수로 선언해줍니다.
Graphics.FromImage(copybmp).CopyFromScreen(Math.Abs(st.X), Math.Abs(st.Y), 0, 0, copybmp.Size);
if (imgviewerChecker)
{
//CommonProperty.LatestMainImageMat = ImageMatch.Matching.ConvertToIplImage(copybmp);
//CommonProperty.LatestMainImageBMP = (Bitmap) copybmp.Clone();
CommonProperty.LatestMainImage = IplImage.FromImageData(ImageMatch.Matching.ImageToByte(copybmp), LoadMode.Color); ;
pictureBoxIpl화면.Image = copybmp;
}
}
else
{
Thread.Sleep(3000);
}
}
}
public MainForm()
{
InitializeComponent();
_imgSave = new Thread(new ThreadStart(imgSave));
//키보드 기본 정보 세팅
KeyInit();
//뷰어 설정
imgviewerChecker = checkBox뷰어.Checked;
}
/// <summary>
/// 던파 초기설정 함수를 설정한다.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton던파설정_Click(object sender, EventArgs e)
{
//던파 정보 세팅
DNFMacroInit();
}
/// <summary>
/// 키보드 세팅하는 함수 호출
/// </summary>
private void KeyInit()
{
try
{
WinFunction.init();
dnfLog("키 세팅 완료");
}
catch(Exception ex)
{
dnfLog("키 세팅 실패");
dnfLog(ex);
}
}
/// <summary>
/// 던파 정보 가져와 세팅하는 함수
/// </summary>
public void DNFMacroInit()
{
var processall = Process.GetProcesses().ToList();
var process = Process.GetProcessesByName("DNF");
if (process.Count() <= 0)
{
dnfLog("던파 프로세스를 찾을 수 없음.");
return;
}
if (process.Count() > 1)
{
dnfLog("던파 프로세스가 1개보다 많음");
return;
}
_process = process[0];
var dnfMainHandle = _process.MainWindowHandle;
if (dnfMainHandle != IntPtr.Zero)
{
//찾은 프로그램을 바탕으로 Graphics 정보를 가져옵니다.
_graphicsdata = Graphics.FromHwnd(dnfMainHandle);
st = new Point();
WinFunction.ScreenToClient(dnfMainHandle, ref st);
//찾은 플레이어 창 크기 및 위치를 가져옵니다.
Rectangle rect = Rectangle.Round(_graphicsdata.VisibleClipBounds);
if (copybmp == null)
copybmp = new Bitmap(rect.Width, rect.Height);
//비트맵을 바탕으로 그래픽스 함수로 선언해줍니다.
Graphics.FromImage(copybmp).CopyFromScreen(Math.Abs(st.X), Math.Abs(st.Y), 0, 0, copybmp.Size);
//pictureBoxIpl화면.Image = copybmp;
settingChecker = true;
_imgSave.Start();
dnfLog("던파 연결 성공");
}
else
{
dnfLog("던파 프로세스 메인 핸들을 찾을 수 없음");
}
}
private void button로그삭제_Click(object sender, EventArgs e)
{
textBox로그.Text = "";
}
public void dnfLog(object data)
{
textBox로그.Text += data.ToString() + "\r\n";
}
private void CheckBox뷰어_CheckedChanged(object sender, EventArgs e)
{
imgviewerChecker = checkBox뷰어.Checked;
}
Scenario.Eclipse.TestStage testStage = null;
private void simpleButton_UP_Click(object sender, EventArgs e)
{
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
Thread.Sleep(1000);
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
WinFunction.pressKey(0, KeyData.UP, 1000);
}
private void simpleButton_DOWN_Click(object sender, EventArgs e)
{
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
WinFunction.pressKey(0, KeyData.DOWN, 1000);
}
private void simpleButton_RIGHT_Click(object sender, EventArgs e)
{
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
WinFunction.pressKey(0, KeyData.RIGHT, 1000);
}
private void simpleButton_LEFT_Click(object sender, EventArgs e)
{
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
WinFunction.pressKey(0, KeyData.LEFT, 1000);
}
private void simpleButton_START_Click(object sender, EventArgs e)
{
testStage = new Scenario.Eclipse.TestStage();
}
private void simpleButton_FOLLOW_Click(object sender, EventArgs e)
{
testStage.FollowTarget();
}
private void simpleButton_STOP_Click(object sender, EventArgs e)
{
testStage.StopFollowTarget();
}
private void getCharacterWorkingPoint(string path)
{
//이미지를 가져와서 하도록
Bitmap img = new Bitmap(path);
getCharacterWorkingPoint(img);
}
private void getCharacterWorkingPoint(Bitmap img)
{
Bitmap copyimg = new Bitmap(img.Width, img.Height);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
var pixelColor = img.GetPixel(i, j);
if (!(pixelColor.R >= 225 &&
pixelColor.G >= 225 &&
pixelColor.B >= 225))
{
copyimg.SetPixel(i, j, Color.White);
}
else
{
copyimg.SetPixel(i, j, Color.Black);
}
}
}
img = copyimg;
pictureBoxIpl화면.Image = img;
var ocr = new TesseractEngine("./tessdata", "eng", EngineMode.CubeOnly);
var texts = ocr.Process(img).GetText().Replace(" ", "");
dnfLog(texts);
}
private void mouseMoveClick(Point desPoint)
{
/*
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
Thread.Sleep(1000);
WinFunction.pressKey(0, KeyData.ESC, 300);
var sourcePoint = Cursor.Position;
dnfLog("시작점 : " + sourcePoint);
var desPoint = new Point(st.X + 515, st.Y + 450);
dnfLog("도착점 : " + desPoint);
mouseMoveClick(desPoint);
*/
var sourcePoint = Cursor.Position;
var xSize = desPoint.X - sourcePoint.X;
var ySize = desPoint.Y - sourcePoint.Y;
//X좌표
while(Cursor.Position.X != desPoint.X)
{
var m = desPoint.X - Cursor.Position.X;
var xm = desPoint.X - Cursor.Position.X > 0 ? 10 : -10;
if(Math.Abs(m)<10)
{
xm = m;
}
WinFunction.MouseMove(xm, 0);
Thread.Sleep(10);
}
//Y좌표
while (Cursor.Position.Y != desPoint.Y)
{
var m = desPoint.Y - Cursor.Position.Y;
var xm = desPoint.Y - Cursor.Position.Y > 0 ? 10 : -10;
if (Math.Abs(m) < 10)
{
xm = m;
}
WinFunction.MouseMove(0, xm);
Thread.Sleep(10);
}
WinFunction.MouseClick();
}
private void button1_Click(object sender, EventArgs e)
{
WinFunction.SetForegroundWindow(_process.MainWindowHandle);
Thread.Sleep(1000);
var desPoint = new Point(Math.Abs(st.X) + 475, Math.Abs(st.Y) + 555);
dnfLog("st : " +st.ToString());
dnfLog(desPoint);
mouseMoveClick(desPoint);
var copybmp1 = new Bitmap(40, 20);
Rectangle rect = new Rectangle(new Point(Math.Abs(st.X)+542, Math.Abs(st.Y)+529),new Size(40,20));
//비트맵을 바탕으로 그래픽스 함수로 선언해줍니다.
Graphics.FromImage(copybmp1).CopyFromScreen(Math.Abs(st.X) + 542, Math.Abs(st.Y) + 529, 0, 0, copybmp1.Size);
getCharacterWorkingPoint(copybmp1);
//copybmp1.Save(@"C:\test\aaa.bmp");
//getCharacterWorkingPoint(@"C:\test\aaa.bmp");
}
private void simpleButton_ViewMatchingImage_Click(object sender, EventArgs e)
{
foreach(var match in CommonProperty.MatchList)
{
match.matchingViewerForm = new ImageMatch.MatchingViewerForm();
match.matchingViewerForm.Show();
}
}
}
}