在愉快地使用开源软件多年后,我认为是时候回馈了。由于文档通常是许多项目的弱点,加上我的C#技能并不是我在FLOSS的角落里的高要求,我想我会从教程等开始。
在第二个刻录工具教程之后,我已经厌倦了
的例行程序并认为我可以自动化。
我想我正在寻找的是一个程序,它会截取当前打开的窗口的截图,例如聚焦控件周围的黄色条(可能是一个按钮),然后弹出一个小文本框,让我输入图片描述,最后将其全部添加到网站/数据库/列表/等。
现在我的实际问题是:除非有人知道已经做到这一点的工具,否则我需要一些关于如何在“外部”窗口上访问控件的大小和位置的启动器,以便我可以计算在哪里绘制重要的突出显示条控制。我记得那些用于Windows的密码取消屏蔽工具可以揭示任何******
受保护文本框的内容,但是我找不到任何开放的例子。我想,WinAPI的东西,WindowFromPoint + GetDlgItem或类似的东西。不知道在Linux中是否更容易,但任何一个都可以。也不喜欢编程语言。
答案 0 :(得分:2)
据我所知,你想做的事情需要一些P / Invoke,因为.NET没有任何用于访问其他应用程序窗口的API。
您可以先使用GetForegroundWindow来获取当前窗口(您需要使用全局热键或计时器触发该代码,因为如果您切换窗口以截取屏幕截图,您将获得自己的从GetForegroundWindow返回的窗口。)
我受到你的问题的启发,做了一个星期天下午的编码。我发现,GetForegroundWindow将为您提供前景窗口,但不是控件级别。但是还有另一个有用的功能,GetGUIThreadInfo,它将为您提供当前关注的控件和其他一些信息。我们可以使用GetWindowInfo来获取有关Window的信息(可能是顶层窗口中包含的控件)。
把这些东西放在一起,我们可以创建一个Window类来抽象出所有粗糙的P / Invoke调用:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace dr.Stackoverflow.ScreenshotTest
{
public class Window
{
private WINDOWINFO info;
private readonly IntPtr handle;
internal Window(IntPtr handle)
{
this.handle = handle;
}
public int Handle
{
get { return handle.ToInt32(); }
}
// Note - will not work on controls in other processes.
public string Text
{
get
{
int length = GetWindowTextLength(handle);
if ( length > 0 )
{
StringBuilder buffer = new StringBuilder(length);
if (0 < GetWindowText(handle, buffer, length))
{
return buffer.ToString();
}
}
return "<unknown>";
}
}
public Rectangle WindowArea
{
get
{
EnsureWindowInfo();
return info.rcWindow;
}
}
public override string ToString()
{
return String.Format("{0} 0x{1}", Text, handle.ToString("x8"));
}
private unsafe void EnsureWindowInfo()
{
if (info.cbSize == 0)
{
info.cbSize = sizeof (WINDOWINFO);
if ( !GetWindowInfo(handle, out info) )
throw new ApplicationException("Unable to get Window Info");
}
}
public static Window GetForeground()
{
IntPtr handle = GetForegroundWindow();
if (handle == IntPtr.Zero)
return null;
return new Window(handle);
}
public unsafe static Window GetFocus()
{
IntPtr foreground = GetForegroundWindow();
int procId;
int tId = GetWindowThreadProcessId(foreground, out procId);
if (0 != tId)
{
GUITHREADINFO threadInfo = new GUITHREADINFO() {cbSize = sizeof (GUITHREADINFO)};
if ( GetGUIThreadInfo(tId, out threadInfo) )
{
return new Window(threadInfo.hwndFocus);
}
}
return null;
}
[StructLayout(LayoutKind.Sequential)]
private struct WINDOWINFO
{
public int cbSize;
public RECT rcWindow;
public RECT rcClient;
public int dwStyle;
public int dwExStyle;
public int dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public int atomWindowType;
public int wCreatorVersion;
}
[StructLayout(LayoutKind.Sequential)]
private 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 rcCaret;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public static implicit operator Rectangle(RECT rhs)
{
return new Rectangle(rhs.left, rhs.top, rhs.right - rhs.left, rhs.bottom - rhs.top);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool GetWindowInfo(IntPtr hwnd, out WINDOWINFO pwi);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern bool GetGUIThreadInfo(int threadId, out GUITHREADINFO threadInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
}
}
然后我们可以使用它制作一个示例程序:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
namespace dr.Stackoverflow.ScreenshotTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sleeping for 3 seconds (switch to a window of interest)");
Thread.Sleep(3000);
Window currentWindow = Window.GetForeground();
Window focusedWindow = Window.GetFocus();
if ( currentWindow != null )
{
Console.WriteLine("Foreground window");
Console.WriteLine(currentWindow.Text);
Console.WriteLine(currentWindow.WindowArea);
}
if (focusedWindow != null)
{
Console.WriteLine("\tFocused window");
Console.WriteLine("\t{0}", focusedWindow.WindowArea);
}
if (focusedWindow !=null && currentWindow != null && focusedWindow.Handle != currentWindow.Handle)
{
Console.WriteLine("\nTaking a screenshot");
Rectangle screenshotArea = currentWindow.WindowArea;
Bitmap bm = new Bitmap(currentWindow.WindowArea.Width,currentWindow.WindowArea.Height);
using(Graphics g = Graphics.FromImage(bm))
{
g.CopyFromScreen(screenshotArea.Left,screenshotArea.Top, 0,0, new Size(screenshotArea.Width,screenshotArea.Height));
Rectangle focusBox = focusedWindow.WindowArea;
focusBox.Offset(screenshotArea.Left * -1, screenshotArea.Top * -1);
focusBox.Inflate(5,5);
g.DrawRectangle(Pens.Red,focusBox);
}
bm.Save("D:\\screenshot.png", ImageFormat.Png);
}
}
}
}
这将显示当前前景窗口的屏幕截图,其中一个红色框突出显示当前聚焦的控件。请注意,这是示例代码,并且具有最少的错误检查:-)当您运行它时,请将Alt-Tab移至感兴趣的窗口并保持该状态直到程序完成。
但是有一些限制。我发现最重要的一个是这种方法在WPF应用程序中不起作用 - 仅仅因为单个控件不是Windows,就像在其他Windows程序中一样。