如何制作“弹出"窗口(提示,下拉,弹出)窗口在Winforms中? [英] How to make a "popup" (hint, drop-down, popup) window in Winforms?

查看:98
本文介绍了如何制作“弹出"窗口(提示,下拉,弹出)窗口在Winforms中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WinForms中制作弹出窗口"?


由于我使用了自己的虚构词弹出" ,所以让我举一个所谓的弹出"窗口的示例:

  • 一个工具提示窗口(可以扩展到其父窗体的边界之外,不出现在任务栏中,不是模态的,并且不会窃取焦点):

  • 一个弹出菜单窗口(可以扩展到其父表单的边界之外,不出现在任务栏中,不是模态的,并且不会窃取焦点):

  • 一个下拉窗口(可以扩展到其父表单的边界之外,不出现在任务栏中,不是模态的,并且不会窃取焦点):

  • 主菜单窗口(可以扩展到其父表单的边界之外,不出现在任务栏中,不是模态的,并且不会占据焦点):

  • 更新 弹出窗口窗口不会使其自身(所有者"窗口仍为活动窗口):

我在这个神话般的弹出窗口"中寻找的属性是:

Windows应用程序已经在设法创建此类窗口.如何在WinForms应用程序中做到这一点?

相关问题


尝试#1

我尝试了Show(onwer) + ShowWithoutActivation方法:

PopupForm dd = new PopupForm ();
dd.Show(this);

使用PopupForm:

public class PopupForm: Form
{
    public PopupForm()
    {
        InitilizeComponent();
    }

    private void InitilizeComponent()
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = false;
    }

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

非常快解决了这个问题,但是后来我发现让我想起了弹出"窗口的另一个属性:它们不会从其所有者"形式获取焦点通过鼠标或键盘进行交互时处于活动状态.

解决方案

您想要一个拥有的窗口.在您的主要形式中:

private void showPopup_Click(object sender, EventArgs e)
{
    PopupForm popupForm = new PopupForm();
    // Make "this" the owner of form2
    popupForm.Show(this);
}

PopupForm应该看起来像这样:

public partial class PopupForm : Form
{
    private bool _activating = false;

    public PopupForm()
    {
        InitializeComponent();
    }

    // Ensure the popup isn't activated when it is first shown
    protected override bool ShowWithoutActivation
    {
        get
        {
            return true;
        }
    }

    private const int WM_NCACTIVATE = 0x86;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    protected override void WndProc(ref Message m)
    {
        // The popup needs to be activated for the user to interact with it,
        // but we want to keep the owner window's appearance the same.
        if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero))
        {
            // The popup is being activated, ensure parent keeps activated appearance
            _activating = true;
            SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero);
            _activating = false;
            // Call base.WndProc here if you want the appearance of the popup to change
        }
        else
        {
            base.WndProc(ref m);
        }
    }
}

并确保PopupForm.ShowInTaskbar = false.

How can i make, what i will call, a "popup" window" in WinForms?


Since i used my own made-up word "popup", let me give examples of this so-called "popup" window:

  • a tooltip window (can extend outside the boundaries of its parent form, doesn't appear in the taskbar, is not modal, and doesn't steal focus):

  • a popup menu window (can extend outside the boundaries of its parent form, doesn't appear in the taskbar, is not modal, and doesn't steal focus):

  • a drop-down window (can extend outside the boundaries of its parent form, doesn't appear in the taskbar, is not modal, and doesn't steal focus):

  • A main menu window (can extend outside the boundaries of its parent form, doesn't appear in the taskbar, is not modal, and doesn't steal focus):

  • Update A popup window not make itself the "active" window when interacted with using a mouse or keyboard (the "owner" window remains the active window):

The attributes that i'm looking for in this mythical "popup" are that it:

  • can extend outside the boundaries of its parent form (i.e. is not a child window)
  • doesn't appear in the taskbar (i.e. Window's heuristics of which windows should appear doesn't kick in, nor does it have WS_EX_APPWINDOW extended window style)
  • is not modal (i.e. doesn't disable its "owner")
  • doesn't steal focus
  • is always on-top of of it's "owner"
  • does not become the "active" window when interacted with (the owner remains active)

Windows applications are already managing to create such windows. How can i do it in a WinForms application?

Related questions

  • How do i achieve all the above in native code?
  • How do i create a popup window in Delphi?
  • i have this native code to show a "popup" window - what P/Invokes are required to perform the same actions in .NET?
  • i have a set of P/Invoke's in .NET - can i reuse a regular WinForm, overriding certain methods, to achieve the same effect?
  • i have WinForm that i'm showing as a "popup" by overriding certain methods - is there a built-in Control that can act as a popup for me?
  • How to simulate a drop-down window in WinForms?

Attempt#1

i tried the Show(onwer) + ShowWithoutActivation method:

PopupForm dd = new PopupForm ();
dd.Show(this);

with PopupForm:

public class PopupForm: Form
{
    public PopupForm()
    {
        InitilizeComponent();
    }

    private void InitilizeComponent()
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = false;
    }

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

Very nearly solved the problem, but then i discovered was reminded of another property of "popup" windows: they do not take focus from their "owner" form become active when interacted with by mouse or keyboard.

解决方案

You want an owned window. In your main form:

private void showPopup_Click(object sender, EventArgs e)
{
    PopupForm popupForm = new PopupForm();
    // Make "this" the owner of form2
    popupForm.Show(this);
}

PopupForm should look like this:

public partial class PopupForm : Form
{
    private bool _activating = false;

    public PopupForm()
    {
        InitializeComponent();
    }

    // Ensure the popup isn't activated when it is first shown
    protected override bool ShowWithoutActivation
    {
        get
        {
            return true;
        }
    }

    private const int WM_NCACTIVATE = 0x86;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    protected override void WndProc(ref Message m)
    {
        // The popup needs to be activated for the user to interact with it,
        // but we want to keep the owner window's appearance the same.
        if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero))
        {
            // The popup is being activated, ensure parent keeps activated appearance
            _activating = true;
            SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero);
            _activating = false;
            // Call base.WndProc here if you want the appearance of the popup to change
        }
        else
        {
            base.WndProc(ref m);
        }
    }
}

And make sure that PopupForm.ShowInTaskbar = false.

这篇关于如何制作“弹出"窗口(提示,下拉,弹出)窗口在Winforms中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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