【问题标题】:How to allow button click events if window in the background如果窗口在后台,如何允许按钮单击事件
【发布时间】:2019-01-21 16:24:57
【问题描述】:

我有一个应用程序 ParentApp,它启动另一个应用程序 ChildApp

ParentAppChildApp 运行时显示一个表单 - 只是一种 Childapp 当前正在运行 显示带有一个取消按钮,可以杀死 ChildApp

我希望ParentAppChildApp 运行时不可用,所以每当有人点击ParentApp 时,我想将ChildApp 带到前台,这很好。

所以我已将此事件处理程序添加到 ParentApp 以响应表单的 Activated 事件。

private void ParentAppForm_Activated(object sender, EventArgs e)
{
    IntPtr childHwnd = _childApp.MainWindowHandle;
    SetForegroundWindow(childHwnd );
}

GotFocus 似乎不起作用)

不幸的是,如果用户单击ParentAppForm 上的Cancel 按钮,则永远不会点击该按钮的事件处理程序,因为Activated 事件首先触发并将前台窗口设置为另一个进程。

是否存在 - 即使应用程序不在前台也允许按钮事件触发?

【问题讨论】:

    标签: c# winforms events process


    【解决方案1】:

    作为一种快速的解决方法,您可以在激活表单时检查鼠标指针是否在 Button.Bounds 所描述的区域内。

    您可以使用PointToClientCursor.Position 坐标将鼠标指针位置转换为Form.Bounds 坐标。

    类似这样的:

    private void ParentAppForm_Activated(object sender, EventArgs e)
    {
        if (this.button1.Bounds.Contains(this.PointToClient(Cursor.Position)))
            ChildForm.Close();  //Or ChildApp.Kill()
        else
            ChildForm.BringToFront();  //Or SetForegroundWindow(ChildApp.Handle)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多