winforms 中的弹出窗口(但未注册为新的独立窗口) [英] Popup in winforms (but not registered as a new standalone window)

查看:19
本文介绍了winforms 中的弹出窗口(但未注册为新的独立窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上的主窗体上显示信息消息.

I would like to display information messages on the screen, over my main form.

我遇到的问题是,我不希望将其作为新表单在 Windows 中注册,因为在应用程序之间切换时,它会显示在任务栏中.我有很多这样的信息框弹出来通知你事件.

The problem I have is that I don't want this to be registered in windows as a new form, since this shows up in the taskbar when switching between apps. I have many of these info boxes popping up to notify you of events.

这个想法是它应该像一个对话框一样工作(不要注册为一个独立的表单),除了它不希望你与对话框交互.

The idea is that it should work similar to a dialogbox (don't get registered as a standalone form) with the exception that it don't expect you to interact with the dialog.

到目前为止,我使用以下内容:

So far I use the following:

var notification = new Notification();
notification.ShowInTaskbar = false;
notification.Show();

谁能指出如何防止此窗口被注册为独立窗口

Can anyone please point out how you can prevent this window from being registered as a standalone window

感谢您提供的链接@shf301:隐藏窗口的最佳方法Alt-Tab 程序切换器?

Thanks for the suggested link @shf301 : Best way to hide a window from the Alt-Tab program switcher?

我看过它,但它无关紧要.该链接解释了如何从 alt-tab 菜单中隐藏应用程序.在这个例子中,我不想隐藏我的应用程序,我只想显示弹出窗口而不将它们注册为新窗口.

I had a look at it but it's unrelated. The link explains how to hide an application from the alt-tab menu. In this example I don't want to hide my application, I simply want to show popups without them being registered as new windows.

推荐答案

这并不容易,因为 Windows 窗体基本上不支持弹出窗口.尽管 windows 窗体基于 win32,但支持 win32 弹出窗口.如果你接受一些技巧,下面的代码会让你弹出一个窗口.您决定是否要充分利用它:(这是制作工具提示的方式)

This is not so easy because basically popups are not supported in windows forms. Although windows forms is based on win32 and in win32 popup are supported. If you accept a few tricks, following code will set you going with a popup. You decide if you want to put it to good use : (This is the way for instance tooltips are made)

class PopupWindow : Control
{
    private const int WM_ACTIVATE = 0x0006;
    private const int WM_MOUSEACTIVATE = 0x0021;

    private Control ownerControl;

    public PopupWindow(Control ownerControl)
        :base()
    {
        this.ownerControl = ownerControl;
        base.SetTopLevel(true);
    }

    public Control OwnerControl
    {
        get
        {
            return (this.ownerControl as Control);
        }
        set
        {
            this.ownerControl = value;
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;

            createParams.Style = WindowStyles.WS_POPUP |
                                 WindowStyles.WS_VISIBLE |
                                 WindowStyles.WS_CLIPSIBLINGS |
                                 WindowStyles.WS_CLIPCHILDREN |
                                 WindowStyles.WS_MAXIMIZEBOX |
                                 WindowStyles.WS_BORDER;
            createParams.ExStyle = WindowsExtendedStyles.WS_EX_LEFT |
                                   WindowsExtendedStyles.WS_EX_LTRREADING |
                                   WindowsExtendedStyles.WS_EX_RIGHTSCROLLBAR | 
                                   WindowsExtendedStyles.WS_EX_TOPMOST;

            createParams.Parent = (this.ownerControl != null) ? this.ownerControl.Handle : IntPtr.Zero;
            return createParams;
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr SetActiveWindow(HandleRef hWnd);

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_ACTIVATE:
                {
                    if ((int)m.WParam == 1)
                    {
                        //window is being activated
                        if (ownerControl != null)
                        {
                            SetActiveWindow(new HandleRef(this, ownerControl.FindForm().Handle));
                        }
                    }
                    break;
                }
            case WM_MOUSEACTIVATE:
                {
                    m.Result = new IntPtr(MouseActivate.MA_NOACTIVATE);
                    return;
                    //break;
                }
        }
        base.WndProc(ref m);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(SystemBrushes.Info, 0, 0, Width, Height);
        e.Graphics.DrawString((ownerControl as VerticalDateScrollBar).FirstVisibleDate.ToLongDateString(), this.Font, SystemBrushes.InfoText, 2, 2);
    }
}

稍微试验一下,你必须调整它的位置和大小.用错了,什么都不显示.

Experiment with it a bit, you have to play around with its position and its size. Use it wrong and nothing shows.

这篇关于winforms 中的弹出窗口(但未注册为新的独立窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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