📜  windows 快照快捷方式 (1)

📅  最后修改于: 2023-12-03 14:48:29.778000             🧑  作者: Mango

Windows 快照快捷方式

在 Windows 操作系统中,快照是一种常见的功能,它能够让用户快速捕捉屏幕的截图并保存在本地。快照可以用于创建教程、演示、文档等多种用途。本文将介绍 Windows 快照的快捷方式及相关的代码片段。

手动截图

在 Windows 操作系统中,可以使用以下快捷方式进行截图:

  • Windows + PrintScreen:按下该键组合将屏幕截图保存至本地,截图所在路径为 C:/Users/username/Pictures/Screenshots。
  • Alt + PrintScreen:按下该键组合将仅截图当前活动的窗口。
  • Windows + Shift + S:按下该键组合将启动 Windows 截图工具,可以自由选择截取所需部分。
自动化截图

若需要通过程序自动进行截图并保存至本地,则可以使用 Windows API 中的相关方法。以下是使用 C# 语言编写的示例代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class ScreenCapture
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool BitBlt(IntPtr hdcDst, int xDst, int yDst, int w, int h, IntPtr hdcSrc, int xSrc, int ySrc, int rop);
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern int DeleteDC(IntPtr hdc);
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern int DeleteObject(IntPtr hObject);

    public static void CaptureScreenshot(string fileName)
    {
        IntPtr desktopWindow = GetDesktopWindow();
        IntPtr desktopDC = GetWindowDC(desktopWindow);
        IntPtr memoryDC = CreateCompatibleDC(desktopDC);
        int screenWidth = Screen.PrimaryScreen.Bounds.Width;
        int screenHeight = Screen.PrimaryScreen.Bounds.Height;
        IntPtr bitmapHandle = CreateCompatibleBitmap(desktopDC, screenWidth, screenHeight);
        if (bitmapHandle != IntPtr.Zero)
        {
            IntPtr oldBitmap = SelectObject(memoryDC, bitmapHandle);
            bool success = BitBlt(memoryDC, 0, 0, screenWidth, screenHeight, desktopDC, 0, 0, 0xCC0020);
            SelectObject(memoryDC, oldBitmap);
            DeleteDC(memoryDC);
            if (success)
            {
                using (Bitmap bitmap = Image.FromHbitmap(bitmapHandle))
                {
                    bitmap.Save(fileName, ImageFormat.Png);
                }
            }
            DeleteObject(bitmapHandle);
        }
        ReleaseDC(desktopWindow, desktopDC);
    }

    static void Main(string[] args)
    {
        CaptureScreenshot(@"C:\screenshot.png");
    }
}

以上代码将截取桌面的快照并将其保存至本地,文件名为 screenshot.png。

结语

使用 Windows 快照快捷方式,可以方便地进行截图并用于多种用途。通过 API,还可以自动化进行快照的截取和保存,方便程序员进行快照相关的开发工作。