这不应该是一个棘手的问题,但是很难对这个问题进行谷歌搜索,并且难以理解。
问题很简单:我有一个窗口表单,用户按下按钮,然后,它将等待用户单击另一个窗口。它存储选定的窗口信息以便稍后进行操作(特别是尺寸)。
如何在按下按钮后获取下一次用户点击的活动窗口?
由于
答案 0 :(得分:1)
您需要获取前景窗口。
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
Rect rect = new Rect ();
GetWindowRect(GetForegroundWindow(), out rect);
//calculate width and height from rect
using (Bitmap bitmap = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
Size size = new System.Drawing.Size(width, height);
g.CopyFromScreen(new Point(rect.Left, rect.Top), Point.Empty, size);
}
bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
}
[StructLayout(LayoutKind.Sequential)]
public struct Rect {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
我在SO上找到了这两个答案中的大部分代码。修改它以适合您的问题
答案 1 :(得分:0)
对您的问题感兴趣,我创建了这个小屏幕捕获应用程序。
它有一些奇怪的解决方法:
提供的应用程序可能非常不专业且不安全,可能会炸毁您的计算机,所以要小心; P
使用的窗体是无边框的,不透明度设置为80%。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace WindowInfo { public partial class CurrentWindow : Form { Rectangle GDIrect = new Rectangle(0, 0, 100, 100); [DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(Point lpPoint); [DllImport("user32.dll")] public static extern bool GetCursorPos(out Point lpPoint); [DllImport("user32.dll")] private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [StructLayout(LayoutKind.Sequential)] public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } public CurrentWindow() { InitializeComponent(); } protected override CreateParams CreateParams { get { CreateParams baseParams = base.CreateParams; baseParams.ExStyle |= (int)( 0x00080000 | 0x08000000 | 0x00000080 | 0x00000020 ); return baseParams; } } public static IntPtr GetWindowUnderCursor() { Point ptCursor = new Point(); GetCursorPos(out ptCursor); return WindowFromPoint(ptCursor); } public Bitmap CaptureScreen() { var result = new Bitmap(this.DisplayRectangle.Width, this.DisplayRectangle.Height); using (var g = Graphics.FromImage(result)) { g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.DisplayRectangle.Size); } return result; } private void timer1_Tick(object sender, EventArgs e) { IntPtr windowHandle = GetWindowUnderCursor(); Rect rect = new Rect(); GetWindowRect(windowHandle, ref rect); GDIrect = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
this.Location = new Point(GDIrect.Left, GDIrect.Top); this.Size = GDIrect.Size; this.Activate(); } private void CurrentWindow_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 'c') { this.Visible = false; Bitmap bmp = CaptureScreen(); bmp.Save(Application.StartupPath + "\\example.png"); this.Visible = true; } else if (e.KeyChar == 'x') { this.Close(); } } } }
U可能会将它添加到您的应用程序并在按钮单击后运行,它应该可以工作,但我只是单独测试它。祝你好运:)。