激活一个表单和处理按钮在同一时间点击? [英] Activate a form and process button click at the same time?

查看:125
本文介绍了激活一个表单和处理按钮在同一时间点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中使用Windows窗体。

我有一个包含toolStripButtons的几个工具栏的主表单。使用另一个包含数据的表单后,主表单失去焦点,点击一个toolStripButton不会触发OnClick事件:第一次点击激活主窗体,只有第二次点击实际上是按下按钮。我需要用户只点击一次按钮触发一个Click事件,关于如何做到这一点的任何想法?
$ b

注意:


  • 我正在使用MDI 点击父窗体按钮也没有问题。但现在最重要的是让表单自由地浮动在多个显示器上。
  • 工作表单具有作为Owner属性的主窗体,这样它们就保持在主窗体的顶部。
  • li>
  • 当我点击一个不活动的窗体的按钮时,MouseHover,MouseEnter,MouseDown和MouseUp都不会被触发。它只是触发主窗体的Activate事件。
  • 在主窗体中有一个treeView(位于面板内部的一个tabContainer中的tabControl内)。它的项目是在第一次鼠标点击时选择的,即使主窗体不活动。我想不是所有的控件都是相同的!


解决方案

它继承 ToolStrip 并处理 WndProc 。这是做到这一点的一种方法。还有其他的。

  private class MyToolStrip:ToolStrip 
{
private const uint WM_LBUTTONDOWN = 0x201;
private const uint WM_LBUTTONUP = 0x202;

private static bool down = false;
$ b保护覆盖void WndProc(ref Message m)
{
if(m.Msg == WM_LBUTTONUP&&!down)
{
m.Msg =(int)WM_LBUTTONDOWN;
base.WndProc(ref m);
m.Msg =(int)WM_LBUTTONUP;
}

if(m.Msg == WM_LBUTTONDOWN)down = true;
if(m.Msg == WM_LBUTTONUP)down = false;
base.WndProc(ref m);
}
}

我也看到了这个解决方案:

  protected override void WndProc(ref Message m)
{
// WM_MOUSEACTIVATE = 0x21
if m.Msg == WM_MOUSEACTIVATE&&&&&&&&&< this.Focused)
this.Focus();
base.WndProc(ref m);



$ b我在我工作的最后一个地方遇到了这个问题,我认为解决方案I想出了更像后者,但我没有访问我使用的确切代码。

I'm using Windows Forms in C#.

I have a main form with a couple of toolbars that contain toolStripButtons. After working with another form that contains data, the main form loses focus and clicking on a toolStripButton does not trigger OnClick event: the first click activates the main form, and only the second click actually pushes the button. I need the user to click a button only once to trigger a Click event, any ideas on how to do that? Thanks.

Notes:

  • I was using MDI and there were no problems clicking on the parent's form buttons. But now the paramount is to have forms freely floating across multiple displays.
  • The worker forms have the main form as the Owner property, this way they stay on top of the main form.
  • When I click on the button of an inactive form, none of MouseHover, MouseEnter, MouseDown nor MouseUp fires. It's just main form's Activate event that fires.
  • There is a treeView (inside a tabControl, inside a splitContainer, inside a panel), on the main form. Its items are selected upon a first mouse click, even if the main form is inactive. I guess not all controls are equal!

解决方案

What you need to do is create a class that inherits ToolStrip and handles the WndProc. This is one way to do it. There are others.

private class MyToolStrip : ToolStrip
{
    private const uint WM_LBUTTONDOWN = 0x201;
    private const uint WM_LBUTTONUP   = 0x202;

    private static bool down = false;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONUP && !down)
        {
            m.Msg = (int)WM_LBUTTONDOWN;
            base.WndProc(ref m);
            m.Msg = (int)WM_LBUTTONUP;
        }

        if (m.Msg == WM_LBUTTONDOWN) down = true;
        if (m.Msg == WM_LBUTTONUP)   down = false;
        base.WndProc(ref m);
    }
}

I've also seen this solution:

protected override void WndProc(ref Message m)
{
    // WM_MOUSEACTIVATE = 0x21
    if (m.Msg == WM_MOUSEACTIVATE && this.CanFocus && !this.Focused)
        this.Focus();
    base.WndProc(ref m);
}

I ran into this at the last place I worked, I think the solution I came up with worked more like the latter, but I don't have access to the exact code I used.

这篇关于激活一个表单和处理按钮在同一时间点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆