其他程序正在运行时显示弹出窗口的Windows应用程序

时间:2013-05-13 14:00:40

标签: c# .net windows visual-studio

首先,我没有使用.NET编程的经验,所以这可能是一个相当不错的问题......

我想知道是否可以构建像xFire这样的应用程序 - 它会在用户的游戏中显示弹出窗口。

由于主要应用程序将具有焦点,我的应用程序如何管理显示它的消息/弹出窗口?主应用程序是否必须允许访问?或者什么?

干杯!

2 个答案:

答案 0 :(得分:2)

看起来你正在寻找NotifyIcon,它会像Windows中的知道一样显示一些弹出窗口。 这个例子有点长,但文档肯定会帮助你理解。

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();

        // Initialize contextMenu1 
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] {this.menuItem1});

        // Initialize menuItem1 
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

        // Set up how the form should be displayed. 
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Text = "Notify Icon Example";

        // Create the NotifyIcon. 
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

        // The Icon property sets the icon that will appear 
        // in the systray for this application.
        notifyIcon1.Icon = new Icon("appicon.ico");

        // The ContextMenu property sets the menu that will 
        // appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed, 
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

    }

    protected override void Dispose( bool disposing )
    {
        // Clean up any components being used. 
        if( disposing )
            if (components != null)
                components.Dispose();            

        base.Dispose( disposing );
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e) 
    {
        // Show the form when the user double clicks on the notify icon. 

        // Set the WindowState to normal if the form is minimized. 
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;

        // Activate the form. 
        this.Activate();
    }

    private void menuItem1_Click(object Sender, EventArgs e) {
        // Close the form, which closes the application. 
        this.Close();
    }
}

请参阅MSDN

答案 1 :(得分:1)

“由于主要应用程序将具有焦点,我的应用程序如何管理显示它的消息/弹出窗口?主应用程序是否必须允许访问或什么?”

应用程序可以将自己设置为“TopMost”,这意味着它出现在其他不是TopMost的“普通”应用程序之前。一个表现良好的通知将在不从当前应用程序中窃取焦点的情况下出现(这通常通过ShowWindow()API和SW_SHOWNA标志来实现)。这不需要当前活动应用程序的任何权限。

看看TaskbarNotifier, a skinnable MSN Messenger-like popup in C# and now in VB.NET too

相关问题