【问题标题】:How to stop Wndproc() loop?如何停止 Wndproc() 循环?
【发布时间】:2019-07-01 07:21:26
【问题描述】:

我已重写 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);

但是什么也没发生。

【问题讨论】:

  • 消息框可能是解决所有问题的关键
  • 您是否尝试过设置 KeyPreview 属性 = true 并管理 keyPress 事件来调整 WndProc?
  • Keypreiew 的问题是关联的 from 必须打开才能工作。如果表单被最小化或未选择它就不起作用。但我需要一个与中断服务程序配合使用的热键。
  • 你为 e.Cancel 设置了什么?
  • 在 OnClosingForm 方法的主体中我设置了 'e.Cancel=false'

标签: c# hotkeys wndproc


【解决方案1】:
[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);
}`

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 1970-01-01
    • 2022-01-11
    • 2014-05-30
    • 2010-09-26
    • 2012-06-19
    • 2013-09-11
    • 2012-11-29
    • 2012-01-16
    相关资源
    最近更新 更多