仅PrintScreen活动窗口 [英] PrintScreen only active window

查看:64
本文介绍了仅PrintScreen活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的Windows Forms实用程序以截屏整个屏幕或活动窗口并保存文件.Winform有一个名为捕获"的按钮,单击该按钮即可捕获屏幕截图.

I'm writing a simple Windows Forms utility to screenshot either the entire screen or an active window and save the file. The winform has a button called 'Capture' that, when clicked, takes the screenshot.

我的代码完全可以在整个屏幕上使用,但是无法在活动窗口中找到.

I have the code entirely working for the entire screen but can't figure it out for the active window.

根据我当前的代码,如果我选择活动窗口",它将结束打印筛选"应用程序本身,这不是我想要的.

By my current code, if I select 'Active Window', it ends up Print Screening the application itself which isn't what I want.

这是我的代码(在Capture_Click方法下):

Here's my code (under the Capture_Click method):

try
        {
            this.Hide();

            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            //bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            if (AltPrint.Checked)
            {
                gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);
            }
            else
            {
                gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            }

            bmpScreenshot.Save(SaveLocation, ImageFormat.Png);

            number++;
        }

推荐答案

据我所知,您的代码使用其自身形式的边界来捕获活动"窗口.那可能不是您想要的.

As far as I can see your code uses the bounds of its own form to capture the "active" window. That is probably not what you intend.

如果要使用鼠标打印"窗口(而不是可能更易于实现的热键),则需要能够以自己的形式让用户指向另一个窗口进行捕获.这是描述如何执行此操作的想法:

If you want to use the mouse to "print" a window (and not hotkey which probably is easier to implement) you need to be able to from your own form let the user point to another window to capture. Here is an idea describing how to do that:

  1. 当用户按下捕获"按钮时,捕获鼠标并启动拖动操作.
  2. 让用户将鼠标(也许现在具有靶心"或相机"光标)拖到窗口中进行捕获.
  3. 当用户释放鼠标时,确定窗口位于顶部,并使用该窗口的位置和大小捕获正确的像素.

您可能需要使用P/Invoke来获取有关桌面上所有顶级窗口的信息.

You will probably need to use P/Invoke to get information about all top-level windows on the desktop.

请注意,鼠标捕获"与屏幕捕获"非常不同.当应用程序捕获鼠标"时,它将获得鼠标光标的所有权,即使将鼠标移到应用程序窗口的边界之外也无法跟踪鼠标.

Note that "mouse capture" is very different from "screen capture". When an application "captures the mouse" it takes ownership of the mouse cursor and doesn't the ability to track the mouse even when it is moved outside the bounds of the window of the application.

这篇关于仅PrintScreen活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆