捕获 Alt+PrintScreen 热键和剪贴板内容

发布于 2024-08-06 18:31:47 字数 5178 浏览 3 评论 0原文

我在 alt+printscreen 上设置了捕获热键。它捕获完美,但缓冲区中没有任何内容 - 没有图像。捕获热键后如何从 Clipboard.GetImage() 获取图像?

这是代码。

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Magic_Screenshot
{
    public enum ModifierKey : uint
    {
        MOD_NULL = 0x0000,
        MOD_ALT = 0x0001,
        MOD_CONTROL = 0x0002,
        MOD_SHIFT = 0x0004,
        MOD_WIN = 0x0008,
    }

    public enum HotKey
    {
        PrintScreen,
        ALT_PrintScreen,
        CONTROL_PrintScreen
    }

    public class HotKeyHelper : IMessageFilter
    {
        const string MSG_REGISTERED = "Горячие клавиши уже зарегистрированы, вызовите UnRegister для отмены регистрации.";
        const string MSG_UNREGISTERED = "Горячие клавиши не зарегистрированы, вызовите Register для регистрации.";
        //Делаем из нашего класса singleton
        public HotKeyHelper()
        {
        }
        //public static readonly HotKeyHelper Instance = new HotKeyHelper();
        public bool isRegistered;
        ushort atom;
        //ushort atom1;
        ModifierKey modifiers;
        Keys keyCode;
        public void Register(ModifierKey modifiers, Keys keyCode)
        {
            //Эти значения нам будут нужны в PreFilterMessage
            this.modifiers = modifiers;
            this.keyCode = keyCode;
            //Не выполнена ли уже регистрация?
            //if (isRegistered)
            //    throw new InvalidOperationException(MSG_REGISTERED);
            //Сохраняем atom, для последующей отмены регистрации
            atom = GlobalAddAtom(Guid.NewGuid().ToString());
            //atom1 = GlobalAddAtom(Guid.NewGuid().ToString());
            if (atom == 0)
                ThrowWin32Exception();
            if (!RegisterHotKey(IntPtr.Zero, atom, modifiers, keyCode))
                ThrowWin32Exception();

            //if (!RegisterHotKey(IntPtr.Zero, atom1, ModifierKey.MOD_CONTROL, Keys.PrintScreen))
            //    ThrowWin32Exception();
            //Добавляем себя в цепочку фильтров сообщений
            Application.AddMessageFilter(this);
            isRegistered = true;
        }
        public void UnRegister()
        {
            //Не отменена ли уже регистрация?
            if (!isRegistered)
                throw new InvalidOperationException(MSG_UNREGISTERED);
            if (!UnregisterHotKey(IntPtr.Zero, atom))
                ThrowWin32Exception();
            GlobalDeleteAtom(atom);
            //Удаляем себя из цепочки фильтров сообщений
            Application.RemoveMessageFilter(this);
            isRegistered = false;
        }
        //Генерирует Win32Exception в ответ на неудачный вызов импортируемой Win32 функции
        void ThrowWin32Exception()
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        //Событие, инициируемое при обнаружении нажатия HotKeys
        public event HotKeyHelperDelegate HotKeyPressed;

        public bool PreFilterMessage(ref Message m)
        {
            //Проверка на сообщение WM_HOTKEY
            if (m.Msg == WM_HOTKEY &&
                //Проверка на окно
              m.HWnd == IntPtr.Zero &&
                //Проверка virtual key code
                m.LParam.ToInt32() >> 16 == (int)keyCode &&
                //Проверка кнопок модификаторов
                (m.LParam.ToInt32() & 0x0000FFFF) == (int)modifiers &&
                //Проверка на наличие подписчиков сообщения
              HotKeyPressed != null)
            {
                if ((m.LParam.ToInt32() & 0x0000FFFF) == (int)ModifierKey.MOD_CONTROL && (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen))
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.CONTROL_PrintScreen);
                }
                else if ((m.LParam.ToInt32() & 0x0000FFFF) == (int)ModifierKey.MOD_ALT && (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen))
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.ALT_PrintScreen);
                }
                else if (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen)
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.PrintScreen);
                }
            }
            return false;
        }

        //Необходимые Win32 константы и функции
        const string USER32_DLL = "User32.dll";
        const string KERNEL32_DLL = "Kernel32.dll";
        const int WM_HOTKEY = 0x0312;
        [DllImport(USER32_DLL, SetLastError = true)]
        static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKey fsModifiers, Keys vk);
        [DllImport(USER32_DLL, SetLastError = true)]
        static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        [DllImport(KERNEL32_DLL, SetLastError = true)]
        static extern ushort GlobalAddAtom(string lpString);
        [DllImport(KERNEL32_DLL)]
        static extern ushort GlobalDeleteAtom(ushort nAtom);
    }
}

错误在哪里?

I setup catching hotkey on alt+printscreen. It catches perfectly but there is nothing in the buffer - no image. How can I get the image from Clipboard.GetImage() after catching hotkey?

Here is the the code.

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Magic_Screenshot
{
    public enum ModifierKey : uint
    {
        MOD_NULL = 0x0000,
        MOD_ALT = 0x0001,
        MOD_CONTROL = 0x0002,
        MOD_SHIFT = 0x0004,
        MOD_WIN = 0x0008,
    }

    public enum HotKey
    {
        PrintScreen,
        ALT_PrintScreen,
        CONTROL_PrintScreen
    }

    public class HotKeyHelper : IMessageFilter
    {
        const string MSG_REGISTERED = "Горячие клавиши уже зарегистрированы, вызовите UnRegister для отмены регистрации.";
        const string MSG_UNREGISTERED = "Горячие клавиши не зарегистрированы, вызовите Register для регистрации.";
        //Делаем из нашего класса singleton
        public HotKeyHelper()
        {
        }
        //public static readonly HotKeyHelper Instance = new HotKeyHelper();
        public bool isRegistered;
        ushort atom;
        //ushort atom1;
        ModifierKey modifiers;
        Keys keyCode;
        public void Register(ModifierKey modifiers, Keys keyCode)
        {
            //Эти значения нам будут нужны в PreFilterMessage
            this.modifiers = modifiers;
            this.keyCode = keyCode;
            //Не выполнена ли уже регистрация?
            //if (isRegistered)
            //    throw new InvalidOperationException(MSG_REGISTERED);
            //Сохраняем atom, для последующей отмены регистрации
            atom = GlobalAddAtom(Guid.NewGuid().ToString());
            //atom1 = GlobalAddAtom(Guid.NewGuid().ToString());
            if (atom == 0)
                ThrowWin32Exception();
            if (!RegisterHotKey(IntPtr.Zero, atom, modifiers, keyCode))
                ThrowWin32Exception();

            //if (!RegisterHotKey(IntPtr.Zero, atom1, ModifierKey.MOD_CONTROL, Keys.PrintScreen))
            //    ThrowWin32Exception();
            //Добавляем себя в цепочку фильтров сообщений
            Application.AddMessageFilter(this);
            isRegistered = true;
        }
        public void UnRegister()
        {
            //Не отменена ли уже регистрация?
            if (!isRegistered)
                throw new InvalidOperationException(MSG_UNREGISTERED);
            if (!UnregisterHotKey(IntPtr.Zero, atom))
                ThrowWin32Exception();
            GlobalDeleteAtom(atom);
            //Удаляем себя из цепочки фильтров сообщений
            Application.RemoveMessageFilter(this);
            isRegistered = false;
        }
        //Генерирует Win32Exception в ответ на неудачный вызов импортируемой Win32 функции
        void ThrowWin32Exception()
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        //Событие, инициируемое при обнаружении нажатия HotKeys
        public event HotKeyHelperDelegate HotKeyPressed;

        public bool PreFilterMessage(ref Message m)
        {
            //Проверка на сообщение WM_HOTKEY
            if (m.Msg == WM_HOTKEY &&
                //Проверка на окно
              m.HWnd == IntPtr.Zero &&
                //Проверка virtual key code
                m.LParam.ToInt32() >> 16 == (int)keyCode &&
                //Проверка кнопок модификаторов
                (m.LParam.ToInt32() & 0x0000FFFF) == (int)modifiers &&
                //Проверка на наличие подписчиков сообщения
              HotKeyPressed != null)
            {
                if ((m.LParam.ToInt32() & 0x0000FFFF) == (int)ModifierKey.MOD_CONTROL && (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen))
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.CONTROL_PrintScreen);
                }
                else if ((m.LParam.ToInt32() & 0x0000FFFF) == (int)ModifierKey.MOD_ALT && (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen))
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.ALT_PrintScreen);
                }
                else if (m.LParam.ToInt32() >> 16 == (int)Keys.PrintScreen)
                {
                    HotKeyPressed(this, EventArgs.Empty, HotKey.PrintScreen);
                }
            }
            return false;
        }

        //Необходимые Win32 константы и функции
        const string USER32_DLL = "User32.dll";
        const string KERNEL32_DLL = "Kernel32.dll";
        const int WM_HOTKEY = 0x0312;
        [DllImport(USER32_DLL, SetLastError = true)]
        static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKey fsModifiers, Keys vk);
        [DllImport(USER32_DLL, SetLastError = true)]
        static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        [DllImport(KERNEL32_DLL, SetLastError = true)]
        static extern ushort GlobalAddAtom(string lpString);
        [DllImport(KERNEL32_DLL)]
        static extern ushort GlobalDeleteAtom(ushort nAtom);
    }
}

Where is the bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-08-13 18:31:47

在您的事件处理程序中添加以下代码:

captureRect = Screen.PrimaryScreen.Bounds;

// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(captureRect.Width, captureRect.Height, PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);

// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(captureRect.X, captureRect.Y, 0, 0, captureRect.Size, CopyPixelOperation.SourceCopy);

//NOTE: gfxScreenshot is your captured bitmap image, use some simple functions to save or display it

In your event handler add the following code:

captureRect = Screen.PrimaryScreen.Bounds;

// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(captureRect.Width, captureRect.Height, PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);

// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(captureRect.X, captureRect.Y, 0, 0, captureRect.Size, CopyPixelOperation.SourceCopy);

//NOTE: gfxScreenshot is your captured bitmap image, use some simple functions to save or display it
橘香 2024-08-13 18:31:47

该键在处理之前被捕获,您必须调度热键并让它完成其工作,然后在通过诸如 SetClipboardViewer Windows API 调用之类的方法更新剪贴板后捕获结果。

The key is caught before being processed, you would have to dispatch the hotkey and let it do its job, then capture the result after the clipboard has been updated via something like the SetClipboardViewer Windows API Call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文