registered hotkeys only works once after... (C# Forms)

Started by
1 comment, last by ajm113 15 years, 11 months ago
I created a application when it minimizes the program's taskbar is set too false then it has a notification icon appear on the side where the clock is and when I double click on the notification icon the app appears normal mode and the show taskbar is set to true and the notification icon hides. My problem is that I registered a print screen button on my app and it works fine when the app is in normal mode, but when the user minimizes the app and brings the form back too normal mode the register hotkey doesn't work any more and I am trying too create a screen capture feature too my app! So whats wrong with it? Do I have to reregister the hot key when the app's task bar hides? Code:

         private const int NullKey = 0;
         private const int WM_HOTKEY = 786;
         protected internal short hotkeyID = 0;

         [DllImport("user32.dll")]
         public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
         [DllImport("user32.dll")]
         public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

//Form Load Event

RegisterHotKey(this.Handle, hotkeyID, NullKey,(int) Keys.PrintScreen);


//Form Closed event
UnregisterHotKey(this.Handle, hotkeyID);


//Check for key presses
 protected override void WndProc(ref System.Windows.Forms.Message m)
 {
    
     if (m.Msg == WM_HOTKEY) {

         CaptureScreen();
        
     }
    
     base.WndProc(ref m);
    
 }



//Other stuff when the app minimizes or goes into normal mode events

        private void FocusForm(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            notifyIcon1.Visible = false;
        }

        private void FocusForm1(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                notifyIcon1.Visible = true;
            }
        }


Check out my open source code projects/libraries! My Homepage You may learn something.
Advertisement
I'm not sure how C# implements what you're doing but it sounds like when C# removes the taskbar button it does so by destroying the window. This invalidates the HWND passed into RegisterHotKey(). I'm not familiar with C# but you'll need to find a way for C# to only hide your window rather than destroy it.
Yep I was right I had to re register the hotkey the hole time for when ever I minimized it. Well thanks any ways for trying too help me out. You can delete this thread or lock it if you want. I don't care about it any more or it mite not come into handy with anyone else.
Check out my open source code projects/libraries! My Homepage You may learn something.

This topic is closed to new replies.

Advertisement