首页 > 解决方案 > 如何停止 Wndproc() 循环?

问题描述

我已经覆盖了 wndproc() 以使用热键。但表格将不再关闭。并关闭程序在 wndproc() 中陷入无限循环。

 protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == 0x0312)
        {


            Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);                  

            int id = m.WParam.ToInt32();                                        


            MessageBox.Show("Hotkey has been pressed!");
            // do something
        }

    }

我已将 e.cancel 设置为 true 并在 OnFoemClosing 方法中添加了以下代码`

 e.Cancel = false;
 base.OnFormClosing(e);

但什么也没有发生。

标签: c#hotkeyswndproc

解决方案


[DllImport("user32.dll")]
static extern bool DestroyWindow(IntPtr hWnd);

//Closes the form
btnCloseWindow_Click(object sender, EventArgs e)
{
     DestroyWindow(this.Handle);
}`[DllImport("user32.dll")]
static extern bool DestroyWindow(IntPtr hWnd);

//Closes the form
btnCloseWindow_Click(object sender, EventArgs e)
{
     DestroyWindow(this.Handle);
}`

推荐阅读