简体   繁体   中英

C# Global hotkeys won't work in first minimize to tray but after showing and minimize again it works

Sorry If I miss anything like a fool, but I have to send in this project and I got a question

I have registered global hotkeys on handle created (EDIT!!)

    protected override void OnHandleCreated(EventArgs e) 
    {
    base.OnHandleCreated(e);
        RegisterHotKey(this.Handle, 1, 0x0000,0x70);
        RegisterHotKey(this.Handle, 2, 0x0000,0x71);
        RegisterHotKey(this.Handle, 3, 0x0000,0x72);
        RegisterHotKey(this.Handle, 4, 0x0000,0x73);
        RegisterHotKey(this.Handle, 5, 0x0000,0x74);
        RegisterHotKey(this.Handle, 6, 0x0000,0x75);
        RegisterHotKey(this.Handle, 7, 0x0000,0x76);
        RegisterHotKey(this.Handle, 8, 0x0000,0x77);
        RegisterHotKey(this.Handle, 9, 0x0000,0x78);
        RegisterHotKey(this.Handle, 10, 0x0000,0x79);
        RegisterHotKey(this.Handle, 11, 0x0000,0x7A);
        RegisterHotKey(this.Handle, 12, 0x0000,0x7B);
        RegisterHotKey(this.Handle, 13, 0x0002,0x45);
    }

and my button1 does hide the form and make the trayicon visible

    void Button1Click(object sender, EventArgs e)
    {
        trayicon.Visible=true;

        ShowInTaskbar=false;
        this.Hide();




        trayicon.ShowBalloonTip(2000,"Corrector is now minimized","Right click at check symbol to Exit or show and change hotkey",ToolTipIcon.Info);
    }

I also have the function to show the form after double click on the tray icon

    private void trayicon_DoubleClick(object Sender, EventArgs e) 
    {
        this.Show();
        Visible=true;
        ShowInTaskbar=true;
    }

Now the problem, I set global hotkey 13 to exit the program.

1.I opened the program , pressed Ctrl+E (the hotkey) >>> It does exit the program

2.I opened the program press button 1 then Ctrl+E>>> It doesn't exit the program

3.I opened the program,press button 1 and doubleclick the icon, the form show then Ctrl+E>>>> It does exit the program

4.I opened the program,press button 1 and doubleclick the icon, the form show then press button 1 again the form dissappeared then Ctrl+E>>> IT DOES EXIT THE PROGRAM.

That's why I am confused, 2 and 4 should have the same result but on 4 it does work while on 2 it doesn't work

Someone please help me

Thanks

Poom

Edit:!!

My Wndproc

     protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            int id = m.WParam.ToInt32();
            if(id==14)
            {
                Application.Exit();
            }
            if(id==13)
            {
                Application.Exit();
            }
            if(check2==0)
            {
                this.TopMost=true;
                MessageBox.Show("กรุณาเลือกปุ่มที่จะใช้เเก้ก่อน,Select hotkey first","ข้อผิดพลาด",MessageBoxButtons.OK,MessageBoxIcon.Error);
                this.TopMost=false;
            }

            if(id==check2)
            {
            translate();//You can replace this statement with your desired response to the Hotkey.
            }

        }
        base.WndProc(ref m);
    }

Pretty sure your problem is tied to the constant toggling of the ShowInTaskbar property. To my knowledge, doing so destroys and recreates your window, meaning the handle you registered your hotkeys with is now pointing to the wrong thing.

As your code stands now, the hotkeys are only getting assigned when your form is FIRST created, since calling Hide() doesn't dispose of the window (For the record, the Visible calls are redundant; Show()/Hide() is equivalent to toggling Visible to true/false).

As far as I know, there are two ways around this. Either assign your hotkeys after a toggle on the ShowInTaskbar property, or override the OnHandleCreated method of your form and do your assigning there.

I am at a loss as to why your steps 3 and 4 work the way they do; I would figure they both shouldn't exit the program. Does this happen reliably? Perhaps someone smarter could venture a guess as to why it works. Is it possible the Handle is being recycled?

I investigated the problem a little bit further (had the same problem) and saw that the window handle is destroyed right after you send the app to the tray and then immediately the window handle is re-created when i was sent to the tray:

  1. OnHandleDestroyed(EventArgs e) invoked
  2. OnHandleCreated(EventArgs e) right afterwards invoked

Just re-register your hotkeys in OnHandleCreated callback and your hotkeys will work afterwards whenever you send your app to the tray.

PS: when you just minimize to the taskbar, your hotkeys will always work.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM