Files
2025-09-16 16:06:41 +08:00

76 lines
2.4 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace Szmedi.AIScriptRunner.Assists;
public class WinIntPtr
{
[DllImport("user32.dll")]
public static extern bool EnumWindows(CallBack lpEnumFunc, int lParam);
public delegate bool CallBack(IntPtr hwnd, int lParam);//回调函数声明,后续还要实现回调函数实体。
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam);
private IntPtr theHandle;
public bool EnumWindowsProc(IntPtr hwnd, int lParam)
{
var len = GetWindowTextLength(hwnd);
if (len > 10)
{
theHandle = hwnd;
return false;//返回FALSE停止枚举
}
return true;
}
public void test()
{
CallBack cbEnumWindows = new(EnumWindowsProc);
EnumWindows(cbEnumWindows, 0);
}
[DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//public void InputStr(IntPtr myIntPtr, string Input)
//{
// byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
// for (int i = 0; i < ch.Length; i++)
// {
// SendMessage(PW, 0X102, ch, 0);
// }
//}
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
public static void ShowAndActive(string winname)
{
var parenthWndldsl = FindWindow(null, winname);
if (!parenthWndldsl.Equals(IntPtr.Zero))
{
ShowWindow(parenthWndldsl, 1);
SetActiveWindow(parenthWndldsl);
}
}
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static bool IsVisible(string winName)
{
var parenthWndldsl = FindWindow(null, winName);
return !parenthWndldsl.Equals(IntPtr.Zero);
}
}