0

I've been trying to figure this out for a very long time now; I've searched this several times and read more articles and questions on this than I can recall and I can't seem to figure out exactly what's going wrong. This is a small program that I've been trying to compile to test generating a hotkey to be used by an application.

The source for the test I've been trying to figure out is as follows:

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

namespace Prg
{
    class MainClass
    {
        [DllImport("User32.dll")]
        private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
        fsModifiers, int vk);
        [DllImport("User32.dll")]
        private static extern int UnregisterHotKey(IntPtr hWnd, int id);

        public static Form f1 = new Form();

        public static int Register(Form f)
        {
            IntPtr ip = f.Handle;
            return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
        }

        public static void b1_click(object sender, EventArgs e)
        {
            //Blah Blah stuff
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                MessageBox.Show("wow");
            }
            base.WndProc(ref m);
        }
        public static void Main()
        {
            Button b1 = new Button();
            b1.Location = new Point(10, 10);
            b1.Text = "wow";
            b1.Click += new EventHandler(b1_click);
            f1.Width = 200;
            f1.Height = 200;
            f1.Controls.Add(b1);
            f1.ShowDialog();
            Register(f1);
        }
    }
}

I have been compiling with csc.exe using C# 4.0. Every time I try and compile this, or similar code, I keep getting this error:

Main.csx(37,27): error CS0115: 'Prg.MainClass.WndProc(System.Windows.Forms.Message)': no suitable method found to override

Every example of using User32.dll to register a hotkey has had the "protected override WndProc" method inside it and everyone has been saying it worked just fine for them, but I can't figure out why it won't work for the life of me. If someone could help me solve this problem it would be greatly appreciated. I'm using Windows 7 Professional 64-bit, and the path to csc.exe is C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

Thanks :)


Edit


I've gotten it to compile now, However the issue now is that it doesn't seem to be registering and kind of hotkey or at least not picking up the KeyPress at all. Is there an error in my code?

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

namespace Prg
{
    class MainClass : Form
    {
        [DllImport("User32.dll")]
        private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
        fsModifiers, int vk);
        [DllImport("User32.dll")]
        private static extern int UnregisterHotKey(IntPtr hWnd, int id);

        public static Form f1 = new Form();

        public static int Register(Form f)
        {
            IntPtr ip = f.Handle;
            return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
        }

        public static void b1_click(object sender, EventArgs e)
        {
            //Blah Blah stuff
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                MessageBox.Show("wow");
            }
            base.WndProc(ref m);
        }
        public static void Main()
        {
            Button b1 = new Button();
            b1.Location = new Point(10, 10);
            b1.Text = "wow";
            b1.Click += new EventHandler(b1_click);
            f1.Width = 200;
            f1.Height = 200;
            f1.Controls.Add(b1);
            f1.ShowDialog();
            Register(f1);
        }
    }
}

I've tried several different solutions but none of them have gotten the Hot Key to work at all. I've even tried re-writing the source to use Application.Run(new MainClass()); but it still didn't detect the Keypress even when the form was focused.


Edit


The Problem is solved thanks to zzxyz for helping get it to compile, and Antoine for helping me fix my mistakes in the code. Thanks guys. This is the code that compiles and works, for anyone who may have had the same problems or just prefers to learn by example. Thanks again.

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

namespace Prg
{
    class MainClass : Form
    {
        [DllImport("User32.dll")]
        private static extern int RegisterHotKey(IntPtr hWnd, int id, int 
        fsModifiers, int vk);
        [DllImport("User32.dll")]
        private static extern int UnregisterHotKey(IntPtr hWnd, int id);

        public static MainClass f1 = new MainClass();

        public static int Register(Form f)
        {
            IntPtr ip = f.Handle;
            return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
        }

        public static void b1_click(object sender, EventArgs e)
        {
            //Blah Blah stuff
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                MessageBox.Show("wow");
            }
            base.WndProc(ref m);
        }
        public static void Main()
        {
            Button b1 = new Button();
            b1.Location = new Point(10, 10);
            b1.Text = "wow";
            b1.Click += new EventHandler(b1_click);
            f1.Width = 200;
            f1.Height = 200;
            f1.Controls.Add(b1);
            Register(f1);
            f1.ShowDialog();
        }
    }
}
Devon Mathews
  • 45
  • 1
  • 8
  • your class doesn't derive from anything other than `Object`. there's no `WndProc` for that. – Daniel A. White Nov 22 '17 at 02:40
  • 1
    WndProc is declared in `Form`, so to override it you need to be in a class that derives from it. The Form-derived class is also where you would typically add buttons and the like. – zzxyz Nov 22 '17 at 02:40

1 Answers1

3

2 mistakes:

  • you must register the hotkeys before showing your f1. Swap the last 2 lines

  • currently, you override WndProc for MainClass, not for every form. So your form f1 inherits the base Form.WndProc, not your overriden one. So just declare f1 as MainClass, and it will work.

Antoine
  • 148
  • 8
  • Hey, thanks for summing it up. Not only did you solve the problem, but you cleared up quite a bit of confusion with two simple sentences. Thanks :) – Devon Mathews Nov 22 '17 at 17:54