Updated native component source code

This commit is contained in:
Namhyeon Go 2023-12-16 23:42:17 +09:00
parent e74afcf112
commit 9bc4621962
6 changed files with 186 additions and 126 deletions

View File

@ -1,5 +1,23 @@
// NamedSharedMemory.cs /*
// https://github.com/gnh1201/welsonjs * WelsonJS.Toolkit: WelsonJS dotNET native component
*
* filename:
* NamedSharedMemory.cs
*
* description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
*/
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;

View File

@ -1,5 +1,23 @@
// Prompt.cs /*
// https://github.com/gnh1201/welsonjs * WelsonJS.Toolkit: WelsonJS dotNET native component
*
* filename:
* Prompt.cs
*
* description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
*/
using System.Windows.Forms; using System.Windows.Forms;
namespace WelsonJS namespace WelsonJS

View File

@ -6,11 +6,11 @@ using System.Runtime.InteropServices;
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 // 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요. // 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("WelsonJS.Toolkit")] [assembly: AssemblyTitle("WelsonJS.Toolkit")]
[assembly: AssemblyDescription("WelsonJS CSharp.NET native component")] [assembly: AssemblyDescription("WelsonJS dotNET native component")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("gnh1201/welsonjs")] [assembly: AssemblyCompany("Catswords Research")]
[assembly: AssemblyProduct("WelsonJS.Toolkit")] [assembly: AssemblyProduct("WelsonJS.Toolkit")]
[assembly: AssemblyCopyright("gnh1201/welsonjs is licensed under the Microsoft Public License (Ms-PL)")] [assembly: AssemblyCopyright("Catswords Research (Licensed under GPLv3 or MS-RL)")]
[assembly: AssemblyTrademark("WelsonJS.Toolkit")] [assembly: AssemblyTrademark("WelsonJS.Toolkit")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]

View File

@ -1,5 +1,8 @@
/* /*
* WelsonJS.Toolkit: WelsonJS dotnet native component * WelsonJS.Toolkit: WelsonJS dotNET native component
*
* filename:
* Toolkit.cs
* *
* description: * description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine * WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
@ -27,7 +30,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
@ -37,113 +39,49 @@ namespace WelsonJS
public class Toolkit public class Toolkit
{ {
private Dictionary<string, NamedSharedMemory> sharedMemoryDict; private Dictionary<string, NamedSharedMemory> sharedMemoryDict;
public static string ApplicationName = "WelsonJS";
[DllImport("user32.dll")] public static readonly string ApplicationName = "WelsonJS";
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] public Toolkit()
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
public enum WMessages : int
{ {
WM_MOUSEMOVE = 0x200, sharedMemoryDict = new Dictionary<string, NamedSharedMemory>();
WM_LBUTTONDOWN = 0x201, //Left mousebutton down
WM_LBUTTONUP = 0x202, //Left mousebutton up
WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
WM_RBUTTONDOWN = 0x204, //Right mousebutton down
WM_RBUTTONUP = 0x205, //Right mousebutton up
WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
WM_KEYDOWN = 0x100, //Key down
WM_KEYUP = 0x101, //Key up
WM_SYSKEYDOWN = 0x104,
WM_SYSKEYUP = 0x105,
WM_CHAR = 0x102, //char
WM_COMMAND = 0x111
}
public enum WVirtualKeys : int
{
VK_RETURN = 0x0D,
VK_F1 = 0x70,
VK_F2 = 0x71,
VK_F3 = 0x72,
VK_F4 = 0x73,
VK_F5 = 0x74,
VK_F6 = 0x75,
VK_F7 = 0x76,
VK_F8 = 0x77,
VK_F9 = 0x78,
VK_F10 = 0x79,
VK_F11 = 0x7A,
VK_F12 = 0x7B
}
public IntPtr QueryHandleWindow(string wName)
{
IntPtr hWnd = IntPtr.Zero;
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
hWnd = pList.MainWindowHandle;
break;
}
}
return hWnd;
} }
[ComVisible(true)] [ComVisible(true)]
public bool SendClick(string wName, int X, int Y) public bool SendClick(string title, int x, int y)
{ {
bool result = false; IntPtr hWnd = Window.GetWindowByTitleContains(title);
IntPtr hWnd = QueryHandleWindow(wName);
if (hWnd != IntPtr.Zero) { if (hWnd != IntPtr.Zero) {
PostMessage(hWnd, (int)WMessages.WM_LBUTTONDOWN, 1, new IntPtr(Y * 0x10000 + X)); Window.PostMessage(hWnd, (int)Window.Message.WM_LBUTTONDOWN, 1, new IntPtr(y * 0x10000 + x));
PostMessage(hWnd, (int)WMessages.WM_LBUTTONUP, 0, new IntPtr(Y * 0x10000 + X)); Window.PostMessage(hWnd, (int)Window.Message.WM_LBUTTONUP, 0, new IntPtr(y * 0x10000 + x));
result = true;
} }
return result; return hWnd != IntPtr.Zero;
} }
[ComVisible(true)] [ComVisible(true)]
public bool SendKey(string wName, char key) public bool SendKey(string title, char key)
{ {
IntPtr hWnd = QueryHandleWindow(wName); IntPtr hWnd = Window.GetWindowByTitleContains(title);
return SendKey(hWnd, key); return SendKey(hWnd, key);
} }
public bool SendKey(IntPtr hWnd, char key) public bool SendKey(IntPtr hWnd, char key)
{ {
return PostMessage(hWnd, (int)WMessages.WM_CHAR, key, IntPtr.Zero); return Window.PostMessage(hWnd, (int)Window.Message.WM_CHAR, key, IntPtr.Zero);
} }
[ComVisible(true)] [ComVisible(true)]
public bool SendKeys(string wName, string str) public bool SendKeys(string title, string str)
{ {
bool result = false; IntPtr hWnd = Window.GetWindowByTitleContains(title);
IntPtr hWnd = QueryHandleWindow(wName);
if (hWnd != IntPtr.Zero) if (hWnd != IntPtr.Zero)
{ {
foreach (char i in str) foreach (char i in str) SendKey(hWnd, i);
{ return true;
SendKey(hWnd, i);
}
result = true;
} }
return result; return false;
} }
[ComVisible(true)] [ComVisible(true)]
@ -167,56 +105,49 @@ namespace WelsonJS
} }
[ComVisible(true)] [ComVisible(true)]
public bool SendEnterKey(string wName) public bool SendEnterKey(string title)
{ {
IntPtr hWnd = QueryHandleWindow(wName); IntPtr hWnd = Window.GetWindowByTitleContains(title);
if (hWnd != IntPtr.Zero) if (hWnd != IntPtr.Zero)
{ {
PostMessage(hWnd, (int)WMessages.WM_KEYDOWN, (char)WVirtualKeys.VK_RETURN, IntPtr.Zero); Window.PostMessage(hWnd, (int)Window.Message.WM_KEYDOWN, (char)Window.VirtualKey.VK_RETURN, IntPtr.Zero);
PostMessage(hWnd, (int)WMessages.WM_KEYUP, (char)WVirtualKeys.VK_RETURN, IntPtr.Zero); Window.PostMessage(hWnd, (int)Window.Message.WM_KEYUP, (char)Window.VirtualKey.VK_RETURN, IntPtr.Zero);
return true; return true;
} }
else
{
return false; return false;
} }
}
[ComVisible(true)] [ComVisible(true)]
public bool SendFnKey(string wName, int num) { public bool SendFnKey(string title, int num) {
IntPtr hWnd = QueryHandleWindow(wName); char[] fnKeys = new char[]
char cKey = (char)0x00;
if (hWnd != IntPtr.Zero)
{ {
switch (num) { (char)0x00,
case 1: cKey = (char)WVirtualKeys.VK_F1; break; (char)Window.VirtualKey.VK_F1,
case 2: cKey = (char)WVirtualKeys.VK_F2; break; (char)Window.VirtualKey.VK_F2,
case 3: cKey = (char)WVirtualKeys.VK_F3; break; (char)Window.VirtualKey.VK_F3,
case 4: cKey = (char)WVirtualKeys.VK_F4; break; (char)Window.VirtualKey.VK_F4,
case 5: cKey = (char)WVirtualKeys.VK_F5; break; (char)Window.VirtualKey.VK_F5,
case 6: cKey = (char)WVirtualKeys.VK_F6; break; (char)Window.VirtualKey.VK_F6,
case 7: cKey = (char)WVirtualKeys.VK_F7; break; (char)Window.VirtualKey.VK_F7,
case 8: cKey = (char)WVirtualKeys.VK_F8; break; (char)Window.VirtualKey.VK_F8,
case 9: cKey = (char)WVirtualKeys.VK_F9; break; (char)Window.VirtualKey.VK_F9,
case 10: cKey = (char)WVirtualKeys.VK_F10; break; (char)Window.VirtualKey.VK_F10,
case 11: cKey = (char)WVirtualKeys.VK_F11; break; (char)Window.VirtualKey.VK_F11,
case 12: cKey = (char)WVirtualKeys.VK_F12; break; (char)Window.VirtualKey.VK_F12
} };
IntPtr hWnd = Window.GetWindowByTitleContains(title);
if (cKey != 0x00) {
PostMessage(hWnd, (int)WMessages.WM_KEYDOWN, cKey, IntPtr.Zero);
PostMessage(hWnd, (int)WMessages.WM_KEYUP, cKey, IntPtr.Zero);
}
if (hWnd != IntPtr.Zero && (fnKeys.Length + 1 < num))
{
Window.PostMessage(hWnd, (int)Window.Message.WM_KEYDOWN, fnKeys[num], IntPtr.Zero);
Window.PostMessage(hWnd, (int)Window.Message.WM_KEYUP, fnKeys[num], IntPtr.Zero);
return true; return true;
} }
else
{
return false; return false;
} }
}
// [Toolkit] Access to a shared memory #96 // [Toolkit] Access to a shared memory #96

View File

@ -47,9 +47,11 @@
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="NamedSharedMemory.cs" />
<Compile Include="Prompt.cs" /> <Compile Include="Prompt.cs" />
<Compile Include="Toolkit.cs" /> <Compile Include="Toolkit.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Window.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="welsonjs.snk" /> <None Include="welsonjs.snk" />

View File

@ -0,0 +1,91 @@
/*
* WelsonJS.Toolkit: WelsonJS dotNET native component
*
* filename:
* Window.cs
*
* description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
*/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WelsonJS
{
public class Window
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
public enum Message : int
{
WM_MOUSEMOVE = 0x200,
WM_LBUTTONDOWN = 0x201, //Left mousebutton down
WM_LBUTTONUP = 0x202, //Left mousebutton up
WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
WM_RBUTTONDOWN = 0x204, //Right mousebutton down
WM_RBUTTONUP = 0x205, //Right mousebutton up
WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
WM_KEYDOWN = 0x100, //Key down
WM_KEYUP = 0x101, //Key up
WM_SYSKEYDOWN = 0x104,
WM_SYSKEYUP = 0x105,
WM_CHAR = 0x102, //char
WM_COMMAND = 0x111
}
public enum VirtualKey : int
{
VK_RETURN = 0x0D,
VK_F1 = 0x70,
VK_F2 = 0x71,
VK_F3 = 0x72,
VK_F4 = 0x73,
VK_F5 = 0x74,
VK_F6 = 0x75,
VK_F7 = 0x76,
VK_F8 = 0x77,
VK_F9 = 0x78,
VK_F10 = 0x79,
VK_F11 = 0x7A,
VK_F12 = 0x7B
}
public static IntPtr GetWindowByTitleContains(string title)
{
IntPtr hWnd = IntPtr.Zero;
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains(title))
{
hWnd = proc.MainWindowHandle;
break;
}
}
return hWnd;
}
}
}