Update the screen time feature

This commit is contained in:
Namhyeon Go 2024-08-24 22:24:26 +09:00
parent b345d655f1
commit 02681a135e
2 changed files with 27 additions and 7 deletions

View File

@ -9,14 +9,16 @@ namespace WelsonJS.Service
public int ScreenNumber { get; set; } public int ScreenNumber { get; set; }
public IntPtr WindowHandle { get; set; } public IntPtr WindowHandle { get; set; }
public string WindowTitle { get; set; } public string WindowTitle { get; set; }
public Point Location { get; set; } public string ProcessName { get; set; }
public Point WindowPosition { get; set; }
public Point Position { get; set; }
public double MaxCorrelation { get; set; } public double MaxCorrelation { get; set; }
public override string ToString() public override string ToString()
{ {
return $"Template: {FileName}, Screen Number: {ScreenNumber}, Window Title: {WindowTitle}, " + return $"Template: {FileName}, Screen Number: {ScreenNumber}, Window Title: {WindowTitle}, " +
$"Location: (x: {Location.X}, y: {Location.Y}), " + $"Process Name: {ProcessName}, Window Position: (x: {WindowPosition.X}, y: {WindowPosition.Y}), " +
$"Max Correlation: {MaxCorrelation}"; $"Location: (x: {Position.X}, y: {Position.Y}), Max Correlation: {MaxCorrelation}";
} }
} }
} }

View File

@ -2,6 +2,7 @@
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -37,6 +38,9 @@ public class ScreenMatch
[DllImport("gdi32.dll")] [DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop); private static extern bool BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private const int SRCCOPY = 0x00CC0020; private const int SRCCOPY = 0x00CC0020;
// 델리게이트 선언 // 델리게이트 선언
@ -169,12 +173,12 @@ public class ScreenMatch
Bitmap image = templateImages[templateCurrentIndex]; Bitmap image = templateImages[templateCurrentIndex];
parent.Log($"Trying match the template {image.Tag as string} on the screen {i}..."); parent.Log($"Trying match the template {image.Tag as string} on the screen {i}...");
Point matchLocation = FindTemplate(mainImage, (Bitmap)image.Clone(), out double maxCorrelation); Point matchPosition = FindTemplate(mainImage, (Bitmap)image.Clone(), out double maxCorrelation);
results.Add(new ScreenMatchResult results.Add(new ScreenMatchResult
{ {
FileName = image.Tag.ToString(), FileName = image.Tag.ToString(),
ScreenNumber = i, ScreenNumber = i,
Location = matchLocation, Position = matchPosition,
MaxCorrelation = maxCorrelation MaxCorrelation = maxCorrelation
}); });
} }
@ -210,11 +214,15 @@ public class ScreenMatch
try try
{ {
string windowTitle = GetWindowTitle(hWnd); string windowTitle = GetWindowTitle(hWnd);
string processName = GetProcessName(hWnd);
GetWindowRect(hWnd, out RECT windowRect);
Point windowPosition = new Point(windowRect.Left, windowRect.Top); // 창 위치 계산
Bitmap windowImage = CaptureWindow(hWnd); Bitmap windowImage = CaptureWindow(hWnd);
if (windowImage != null) if (windowImage != null)
{ {
Bitmap image = templateImages[templateCurrentIndex]; Bitmap image = templateImages[templateCurrentIndex];
Point matchLocation = FindTemplate(windowImage, image, out double maxCorrelation); Point matchPosition = FindTemplate(windowImage, image, out double maxCorrelation);
string templateFileName = image.Tag as string; string templateFileName = image.Tag as string;
var result = new ScreenMatchResult var result = new ScreenMatchResult
@ -222,7 +230,9 @@ public class ScreenMatch
FileName = templateFileName, FileName = templateFileName,
WindowHandle = hWnd, WindowHandle = hWnd,
WindowTitle = windowTitle, WindowTitle = windowTitle,
Location = matchLocation, ProcessName = processName,
WindowPosition = windowPosition,
Position = matchPosition,
MaxCorrelation = maxCorrelation MaxCorrelation = maxCorrelation
}; };
results.Add(result); results.Add(result);
@ -246,6 +256,14 @@ public class ScreenMatch
return sb.ToString(); return sb.ToString();
} }
public string GetProcessName(IntPtr hWnd)
{
uint processId;
GetWindowThreadProcessId(hWnd, out processId);
Process process = Process.GetProcessById((int)processId);
return process.ProcessName;
}
public Bitmap CaptureWindow(IntPtr hWnd) public Bitmap CaptureWindow(IntPtr hWnd)
{ {
GetWindowRect(hWnd, out RECT rect); GetWindowRect(hWnd, out RECT rect);