Commit 90c1bd42 张东亮

1

1 个父辈 b2a03fd1
......@@ -7,6 +7,9 @@ using System.Runtime.InteropServices;
using System.IO;
using BLL;
using log4net;
using System.Text;
using System.Collections.Generic;
using log4net.Util;
namespace SmartScan
{
......@@ -31,9 +34,9 @@ namespace SmartScan
_ = new Mutex(true, Application.ProductName, out bool ret);
if (!ret)
{
IntPtr formhwnd = FindWindow(null, "BarCode Rule Setting");
IntPtr formhwnd = GetWindowHandle("BarCode Rule Setting");
if (formhwnd == IntPtr.Zero)
formhwnd = FindWindow(null, "条码规则设置");
formhwnd = GetWindowHandle("条码规则设置");
show($"查找条码规则窗口句柄:{formhwnd}");
if(formhwnd != IntPtr.Zero)
......@@ -193,7 +196,11 @@ namespace SmartScan
/// <param name="fAltTab">True代表窗口正在通过Alt/Ctrl +Tab被切换</param>
[DllImport("user32.dll ", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll")]
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
/// <summary>
/// 设置窗口的显示状态
/// </summary>
......@@ -203,7 +210,53 @@ namespace SmartScan
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
public const int SW_RESTORE = 9;
[DllImport("user32.dll")]
private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
#endregion
//寻找系统的全部窗口
static WindowInfo[] GetAllDesktopWindows()
{
List<WindowInfo> wndList = new List<WindowInfo>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(256);
//get hwnd
wnd.hWnd = hWnd;
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);
//add it into list
wndList.Add(wnd);
return true;
}, 0);
return wndList.ToArray();
}
static IntPtr GetWindowHandle(string titleName)
{
WindowInfo[] a = GetAllDesktopWindows();
int i = 0;
for (i = 0; i < a.Length; i++)
{
// MessageBox.Show(a[i].szWindowName.ToString());
if (a[i].szWindowName.ToString().Contains(titleName))
{
return a[i].hWnd;
}
}
return IntPtr.Zero;
}
}
public struct WindowInfo
{
public IntPtr hWnd;
public string szWindowName;
public string szClassName;
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!