Form1是一种类型,但用作变量

时间:2014-01-21 20:17:54

标签: c# winforms

在我最终将我的应用程序启动到alpha之前,我处于最后一段代码中,但是我一直试图获得一个全局注册的组合键。我发现一个类似乎会做我想要的(从here找到):

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.InteropServices;

namespace Hotkey
{
    public class Hotkey : IMessageFilter
    {
                                                    #region Interop

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk);

    [DllImport("user32.dll", SetLastError=true)]
    private static extern int UnregisterHotKey(IntPtr hWnd, int id);

    private const uint WM_HOTKEY = 0x312;

    private const uint MOD_ALT = 0x1;
    private const uint MOD_CONTROL = 0x2;
    private const uint MOD_SHIFT = 0x4;
    private const uint MOD_WIN = 0x8;

    private const uint ERROR_HOTKEY_ALREADY_REGISTERED = 1409;

    #endregion

        private static int currentID;
        private const int maximumID = 0xBFFF;

        private Keys keyCode;
        private bool shift;
        private bool control;
        private bool alt;
        private bool windows;

        [XmlIgnore]
        private int id;
        [XmlIgnore]
        private bool registered;
        [XmlIgnore]
        private Control windowControl;

        public event HandledEventHandler Pressed;

        public Hotkey() : this(Keys.None, false, false, false, false)
        {
            // No work done here!
        }

        public Hotkey(Keys keyCode, bool shift, bool control, bool alt, bool windows)
        {
            // Assign properties
            this.KeyCode = keyCode;
            this.Shift = shift;
            this.Control = control;
            this.Alt = alt;
            this.Windows = windows;

            // Register us as a message filter
            Application.AddMessageFilter(this);
        }

        ~Hotkey()
        {
            // Unregister the hotkey if necessary
            if (this.Registered)
            { this.Unregister(); }
        }

        public Hotkey Clone()
        {
            // Clone the whole object
            return new Hotkey(this.keyCode, this.shift, this.control, this.alt, this.windows);
        }

        public bool GetCanRegister(Control windowControl)
        {
            // Handle any exceptions: they mean "no, you can't register" :)
            try
            {
                // Attempt to register
                if (!this.Register(windowControl))
                { return false; }

                // Unregister and say we managed it
                this.Unregister();
                return true;
            }
            catch (Win32Exception)
            { return false; }
            catch (NotSupportedException)
            { return false; }
        }

        public bool Register(Control windowControl)
        {
            // Check that we have not registered
            if (this.registered)
            { throw new NotSupportedException("You cannot register a hotkey that is already registered"); }

            // We can't register an empty hotkey
            if (this.Empty)
            { throw new NotSupportedException("You cannot register an empty hotkey"); }

            // Get an ID for the hotkey and increase current ID
            this.id = Hotkey.currentID;
            Hotkey.currentID = Hotkey.currentID + 1 % Hotkey.maximumID;

            // Translate modifier keys into unmanaged version
            uint modifiers = (this.Alt ? Hotkey.MOD_ALT : 0) | (this.Control ? Hotkey.MOD_CONTROL : 0) |
                            (this.Shift ? Hotkey.MOD_SHIFT : 0) | (this.Windows ? Hotkey.MOD_WIN : 0);

            // Register the hotkey
            if (Hotkey.RegisterHotKey(windowControl.Handle, this.id, modifiers, keyCode) == 0)
            { 
                // Is the error that the hotkey is registered?
                if (Marshal.GetLastWin32Error() == ERROR_HOTKEY_ALREADY_REGISTERED)
                { return false; }
                else
                { throw new Win32Exception(); } 
            }

            // Save the control reference and register state
            this.registered = true;
            this.windowControl = windowControl;

            // We successfully registered
            return true;
        }

        public void Unregister()
        {
            // Check that we have registered
            if (!this.registered)
            { throw new NotSupportedException("You cannot unregister a hotkey that is not registered"); }

            // It's possible that the control itself has died: in that case, no need to unregister!
            if (!this.windowControl.IsDisposed)
            {
                // Clean up after ourselves
                if (Hotkey.UnregisterHotKey(this.windowControl.Handle, this.id) == 0)
                { throw new Win32Exception(); }
            }

            // Clear the control reference and register state
            this.registered = false;
            this.windowControl = null;
        }

        private void Reregister()
        {
            // Only do something if the key is already registered
            if (!this.registered)
            { return; }

            // Save control reference
            Control windowControl = this.windowControl;

            // Unregister and then reregister again
            this.Unregister();
            this.Register(windowControl);
        }

        public bool PreFilterMessage(ref Message message)
        {
            // Only process WM_HOTKEY messages
            if (message.Msg != Hotkey.WM_HOTKEY)
            { return false; }

            // Check that the ID is our key and we are registerd
            if (this.registered && (message.WParam.ToInt32() == this.id))
            {
                // Fire the event and pass on the event if our handlers didn't handle it
                return this.OnPressed();
            }
            else
            { return false; }
        }

        private bool OnPressed()
        {
            // Fire the event if we can
            HandledEventArgs handledEventArgs = new HandledEventArgs(false);
            if (this.Pressed != null)
            { this.Pressed(this, handledEventArgs); }

            // Return whether we handled the event or not
            return handledEventArgs.Handled;
        }

        public override string ToString()
        {
            // We can be empty
            if (this.Empty)
            { return "(none)"; }

            // Build key name
            string keyName = Enum.GetName(typeof(Keys), this.keyCode);;
            switch (this.keyCode)
            {
                case Keys.D0:
                case Keys.D1:
                case Keys.D2:
                case Keys.D3:
                case Keys.D4:
                case Keys.D5:
                case Keys.D6:
                case Keys.D7:
                case Keys.D8:
                case Keys.D9:
                    // Strip the first character
                    keyName = keyName.Substring(1);
                    break;
                default:
                    // Leave everything alone
                    break;
            }

            // Build modifiers
            string modifiers = "";
            if (this.shift)
            { modifiers += "Shift+"; }
            if (this.control)
            { modifiers += "Control+"; }
            if (this.alt)
            { modifiers += "Alt+"; }
            if (this.windows)
            { modifiers += "Windows+"; }

            // Return result
            return modifiers + keyName;
        }

        public bool Empty
        {
            get { return this.keyCode == Keys.None; }
        }

        public bool Registered
        {
            get { return this.registered; }
        }

        public Keys KeyCode
        {
            get { return this.keyCode; }
            set
            {
                // Save and reregister
                this.keyCode = value;
                this.Reregister();
            }
        }

        public bool Shift
        {
            get { return this.shift; }
            set 
            {
                // Save and reregister
                this.shift = value;
                this.Reregister();
            }
        }

        public bool Control
        {
            get { return this.control; }
            set
            { 
                // Save and reregister
                this.control = value;
                this.Reregister();
            }
        }

        public bool Alt
        {
            get { return this.alt; }
            set
            { 
                // Save and reregister
                this.alt = value;
                this.Reregister();
            }
        }

        public bool Windows
        {
            get { return this.windows; }
            set 
            {
                // Save and reregister
                this.windows = value;
                this.Reregister();
            }
        }
    }
}

我对它应该如何运作的理解是这样的:

Hotkey hk = new Hotkey();

hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };

hk.Register(myForm); 

和我在表单中的实现:

Hotkey hk = new Hotkey();

private void Form1_Load(object sender, EventArgs e)
    {
        hk.KeyCode = Keys.Alt;
        hk.Windows = true;
        hk.Pressed += hk_Pressed;

        hk.Register(Form1);
    }

    private void hk_Pressed(object sender, EventArgs e)
    {
        MessageBox.Show("pressed");
    }

但我收到一个错误:Form1 is a type but is used as a variable现在坦率地说,我完全迷失了我的错误。我假设表单的名称是Register中需要传递的内容。我是C#的新手,所以它可能是我从根本上做错的事情,但我在我的智慧结束这里任何帮助都会很棒。

2 个答案:

答案 0 :(得分:3)

hk.Register(this);

只需这样做!它正在寻找Form1实例

答案 1 :(得分:0)

hk.Register(myForm);更改为hk.Register(this);

与mainForm

相同

另外,我看到你发布了迈克尔的答案,说它仍然没有发射。

您需要删除hk.Windows = true;