1 Star 0 Fork 0

Joke_Study/电子天平应用程序

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SendMsg.cs 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
LuckyJoke 提交于 2024-08-28 16:22 . first commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ElecScale
{
#region 向当前活动文本框输入文字
/// <summary>
/// 向当前活动文本框输入文字
/// new SendMsg().SendText("你要输入的字符串");
/// </summary>
public class SendMsg
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rectCaret;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
int left;
int top;
int right;
int bottom;
}
public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
{
if (hwnd != IntPtr.Zero)
{
uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
return null; return guiThreadInfo;
}
return null;
}
public void SendText(string text)
{
IntPtr hwnd = GetForegroundWindow();
if (String.IsNullOrEmpty(text))
return;
GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
if (guiInfo != null)
{
for (int i = 0; i < text.Length; i++)
{
SendMessage(guiInfo.Value.hwndFocus, 0x0102, (IntPtr)(int)text[i], IntPtr.Zero);
}
}
}
private bool isTrigger()
{
IntPtr hwnd = GetForegroundWindow();
GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
if (guiInfo != null)
{
return true;
}
return false;
}
}
#endregion
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/joke_study/BalanceAdaper.git
[email protected]:joke_study/BalanceAdaper.git
joke_study
BalanceAdaper
电子天平应用程序
master

搜索帮助