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

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

问题描述

我怎样才能在 WinForms 中创建一个弹出"窗口?

<小时>

由于我使用了我自己编造的词popup",让我举个这个所谓的popup"窗口的例子:

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

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

  • 下拉式窗口(可以延伸到其父窗体的边界之外,不出现在任务栏中,不是模态的,也不会窃取焦点):

  • 主菜单窗口(可以延伸到其父窗体的边界之外,不出现在任务栏中,不是模态的,也不会窃取焦点):

  • 更新 弹出式 窗口不会自动生成 )

  • 没有出现在任务栏中(即 Window 对应该出现哪些窗口的启发式方法没有起作用,也没有 WS_EX_APPWINDOW 扩展窗口样式)
  • 不是模态(即不会禁用其所有者")
  • 不偷专注
  • 始终在其所有者"之上
  • 在与之交互时不会成为活动"窗口(所有者保持活动状态)

Windows 应用程序已经在设法创建此类窗口.如何在 WinForms 应用程序中执行此操作?

相关问题

  • 如何在本机代码中实现以上所有功能?
  • 如何在 Delphi 中创建弹出窗口?
  • 我有这个本机代码来显示弹出"窗口 - 在 .NET 中执行相同操作需要哪些 P/Invokes?
  • 我在 .NET 中有一组 P/Invoke - 我可以重用常规 WinForm,覆盖某些方法,以达到相同的效果吗?
  • 我有 WinForm,我通过覆盖某些方法将其显示为弹出窗口" - 是否有内置的 Control 可以充当我的弹出窗口?
  • 如何在WinForms中模拟下拉窗口?
<小时>

尝试#1

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

PopupForm dd = new PopupForm ();dd.显示(这个);

使用 PopupForm:

public class PopupForm: Form{公共弹出窗体(){初始化组件();}私人无效初始化组件(){this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;this.WindowState = FormWindowState.Normal;this.ShowInTaskbar = false;}protected override bool ShowWithoutActivation{ 得到 { 返回真;} }}

几乎解决了这个问题,但后来我发现被提醒弹出"窗口的另一个属性:它们不会从它们的所有者"表单中获取焦点变成与鼠标或键盘交互时激活.

解决方案

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

private void showPopup_Click(object sender, EventArgs e){PopupForm popupForm = new PopupForm();//使 "this" 成为 form2 的所有者popupForm.Show(this);}

PopupForm 应该如下所示:

public 部分类 PopupForm : Form{私人布尔_激活=假;公共弹出窗体(){初始化组件();}//确保弹出窗口在第一次显示时没有被激活protected override bool ShowWithoutActivation{得到{返回真;}}私有常量 int WM_NCACTIVATE = 0x86;[DllImport("user32.dll", CharSet = CharSet.Auto)]公共静态外部 IntPtr SendMessage(IntPtr hWnd,int msg,IntPtr wParam,IntPtr lParam);受保护的覆盖无效 WndProc(参考消息 m){//需要激活弹出窗口以便用户与之交互,//但我们希望保持所有者窗口的外观相同.if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero)){//弹出窗口正在被激活,确保父级保持激活状态_激活=真;SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero);_激活=假;//如果要改变弹出窗口的外观,请在此处调用 base.WndProc}别的{base.WndProc(参考 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天全站免登陆