Hi there,
I have weired problem with the registration of a HotKey and the FormStartPosition / Form WindowState:
Everything works fine, until I set the FormStartPosition to CenterScreen and/or the FormWindowState to Minimized.
Can somebody tell me why
To illustrade my problem, I have written a smoll programm that shows my problem. It consitst out of two classes:
First: MainForm:
It instanciates an object of Class HotKey and sets the startup behavior:
MainForm:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace HotKey
{
public class MainForm : System.Windows.Forms.Form{ public MainForm()
{
HotKey HK = new HotKey();
InitializeComponent();
}//Constructor [STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}//Main private void InitializeComponent() {
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "MainForm";
this.Text = "MainForm";
//this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
//this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
}
//Class}
//NamespaceSecond: HotKey:
It registeres the HotKey and works fine until I uncomment the two concering line in the MainForm-Class:
HotKey:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace HotKey
{
/// http://www.codekeep.net/snippets/ea68475b-c33e-4a71-8008-90f63d7e918d.aspx
/// http://www.csharphelp.com/board2/read.html f=1&i=33914&t=33899
/// </summary>
public
class HotKey : System.Windows.Forms.Form{ //////////////////////////////////////////////////////////////
#region API Imports
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[
DllImport("user32.dll", SetLastError = true)]public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
#endregion
//////////////////////////////////////////////////////////////
const int HOTKEY_ID = 31197; //any number to be used as an id within this app
const int WM_HOTKEY = 0x0312; //The number WndProc() waits for public HotKey()
{
//set hotkey on [ALT + F11]
RegisterHotKey(Handle, HOTKEY_ID, KeyModifiers.Alt, Keys.F11);
}//Constructor
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}//KeyModifiers
//having registered a hotkey the thread that registered it
//receives a WM_HOTKEY message upon the keypress which has to
//be caught by overwriting the WndProc method
{
// Listen for operating system messages.
if(m.Msg == WM_HOTKEY)
MessageBox.Show("OK");
base.WndProc(ref m);
}//WndProc public void ReleaseHotKey()
{
//reset hotkey if exit
UnregisterHotKey(Handle, HOTKEY_ID);
}//ReleaseHotKey }
}

HotKeyRegistration and FormStartPosition/FormWindowState
Darren Braun
Ricardo Pinto
Some things looks strange:
I think you need to work with HotKey in MainForm (no need for separate form) and register it each time HandleCreating event raised (this event may be called many times, so if you already registered - unregister and register again).
NWC
Thanks for your help,
I tried to keep everything in one class before: Your garbage-collegtion theory sounds good, but: Same issue! :-(
What I don't understand is the last part of your post:
register it each time HandleCreating event raised (this event may be called many times, so if you already registered - unregister and register again).
Thanks you,
Finch82
PS: Here are the two classes: http://home.arcor.de/webspace82/HotKey.zip
EDIT: I added some more code to my project and it works now. I think it just "seems to work" but the problem did not disappear... So I'm still thankful for any help on this.