如何创建一个" unfocusable"在C#中形成的? [英] How do I create an "unfocusable" form in C#?

查看:156
本文介绍了如何创建一个" unfocusable"在C#中形成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待建立在C#的形式,不能接受焦点,也就是当我点击窗体上的按钮时,焦点不会从当前具有焦点的应用程序窃取。

I'm looking to create a form in C# that cannot accept focus, i.e. when I click a button on the form, focus is not stolen from the application that currently has the focus.

对于这样的一个例子请参阅Windows屏幕键盘。请注意,当你点击一个按钮,焦点不是从你目前正在使用的应用程序采取的。

See the Windows on-screen keyboard for an example of this. Note that when you click a button, the focus is not taken from the application you're currently using.

我怎样才能实现这一行为?

How can I implement this behaviour?

更新:

原来这是为覆盖的CreateParams 属性一样简单和加入 WS_EX_NOACTIVATE 来扩展窗口样式。感谢您指出我在正确的方向!

Turns out it's as simple as overriding the CreateParams property and adding WS_EX_NOACTIVATE to the extended window style. Thanks for pointing me in the right direction!

不幸的是这有不良副作用,即它与形式的运动食堂,即你仍然可以通过拖放周围的形式屏幕上,但在拖动时不显示窗口的边框,因此很难精确定位。

Unfortunately this has the undesirable side-effect that it messes with form movement, i.e. you can still drag and drop the form around the screen but the window's border is not displayed while dragging so it's difficult to precisely position it.

如果有人知道如何解决这个问题我们将不胜感激。

If anyone knows how to solve this it would be appreciated.

推荐答案

要通过鼠标禁用激活:

class NonFocusableForm : Form
{
    protected override void DefWndProc(ref Message m)
    {
        const int WM_MOUSEACTIVATE = 0x21;
        const int MA_NOACTIVATE = 0x0003;

        switch(m.Msg)
        {
            case WM_MOUSEACTIVATE:
                m.Result = (IntPtr)MA_NOACTIVATE;
                return;
        }
        base.DefWndProc(ref m);
    }
}

要显示无需激活形式(唯一方式

To show form without activation (the only one way that worked for me in case of borderless form):

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr handle, int flags);

    NativeMethods.ShowWindow(form.Handle, 8);



标准的方式来做到这一点(好像它并不适用于所有形式的风格下工作):

Standard way to do this (seems like it doesn't work for all form styles):

    protected override bool ShowWithoutActivation
    {
        get { return true; }
    }

如果有激活的形式的其它方法,它们可以在抑制类似的方式。

If there are other ways of activating the form, they can be suppressed in similar manner.

这篇关于如何创建一个" unfocusable"在C#中形成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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