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?