[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1032
  • Last Modified:

Starting an application with TrayIcon but no Form

Hi,

I try to run my application with no form, just a trayicon should be shown.
This works at all but my trayicon has no menu.
When it'll be used with a form the trayicon works fine - with context menu.

What is worng there?

Thanks,

Andre
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
 
namespace tcAnwesenheit
{
    static class Program
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uiAction, int uiParam, ref bool pvParam, int fWinIni);
        public const int screensaverrunning = 0x72;
        public static bool bScreenSaverrunning = false;
 
        private static ADSS.TrayPopup.TrayPopup popUpWnd1;
        private static System.Windows.Forms.NotifyIcon notifyIcon1;
        private static System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private static System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem stopToolStripMenuItem;
        private static System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private static System.Windows.Forms.ToolStripMenuItem einstellungenToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem beendenToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem verbindenMitToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem lokalToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem serverToolStripMenuItem;
        private static System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
        private static System.Windows.Forms.ToolStripMenuItem offlineToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
        private static System.Windows.Forms.ToolStripMenuItem startenToolStripMenuItem;
        private static System.Windows.Forms.ToolStripMenuItem beendenToolStripMenuItem1;
        private static System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private static System.Windows.Forms.ToolStripMenuItem ändernToolStripMenuItem;
        private static System.Windows.Forms.ToolStripSeparator toolStripSeparator4;
        private static System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
        private static System.Windows.Forms.ToolStripComboBox toolStripComboBox2;
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            notifyIcon1 = new System.Windows.Forms.NotifyIcon();
            popUpWnd1 = new ADSS.TrayPopup.TrayPopup();
            contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();
            testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            stopToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            einstellungenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            beendenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            verbindenMitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            lokalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            serverToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            offlineToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            startenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            beendenToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            ändernToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            toolStripComboBox1 = new System.Windows.Forms.ToolStripComboBox();
            toolStripComboBox2 = new System.Windows.Forms.ToolStripComboBox();
            contextMenuStrip1.SuspendLayout();
 
            // 
            // notifyIcon1
            // 
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;
            notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            notifyIcon1.Text = "notifyIcon1";
            notifyIcon1.Visible = true;
            notifyIcon1.MouseDown += new System.Windows.Forms.MouseEventHandler(notifyIcon1_MouseDown);
            // 
            // popUpWnd1
            // 
            popUpWnd1.BottomColor = System.Drawing.Color.LightSkyBlue;
            popUpWnd1.color = System.Drawing.Color.White;
            popUpWnd1.font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            popUpWnd1.icon = ((System.Drawing.Icon)(resources.GetObject("popUpWnd1.icon")));
            popUpWnd1.opacity = 100;
            popUpWnd1.TopColor = System.Drawing.Color.DodgerBlue;
            // 
            // contextMenuStrip1
            // 
            contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            testToolStripMenuItem,
            stopToolStripMenuItem,
            toolStripSeparator1,
            toolStripMenuItem1,
            toolStripSeparator2,
            verbindenMitToolStripMenuItem,
            toolStripSeparator3,
            einstellungenToolStripMenuItem,
            beendenToolStripMenuItem});
            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new System.Drawing.Size(150, 154);
            // 
            // testToolStripMenuItem
            // 
            testToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("testToolStripMenuItem.Image")));
            testToolStripMenuItem.Name = "testToolStripMenuItem";
            testToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
            testToolStripMenuItem.Text = "Kommen";
            // 
            // stopToolStripMenuItem
            // 
            stopToolStripMenuItem.Enabled = false;
            stopToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("stopToolStripMenuItem.Image")));
            stopToolStripMenuItem.Name = "stopToolStripMenuItem";
            stopToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            stopToolStripMenuItem.Text = "Gehen";
            // 
            // toolStripSeparator2
            // 
            toolStripSeparator2.Name = "toolStripSeparator2";
            toolStripSeparator2.Size = new System.Drawing.Size(149, 6);
            // 
            // einstellungenToolStripMenuItem
            // 
            einstellungenToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("einstellungenToolStripMenuItem.Image")));
            einstellungenToolStripMenuItem.Name = "einstellungenToolStripMenuItem";
            einstellungenToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
            einstellungenToolStripMenuItem.Text = "Einstellungen";
            // 
            // beendenToolStripMenuItem
            // 
            beendenToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("beendenToolStripMenuItem.Image")));
            beendenToolStripMenuItem.Name = "beendenToolStripMenuItem";
            beendenToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
            beendenToolStripMenuItem.Text = "Beenden";
            // 
            // toolStripSeparator3
            // 
            toolStripSeparator3.Name = "toolStripSeparator3";
            toolStripSeparator3.Size = new System.Drawing.Size(149, 6);
            // 
            // verbindenMitToolStripMenuItem
            // 
            verbindenMitToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            lokalToolStripMenuItem,
            serverToolStripMenuItem,
            offlineToolStripMenuItem});
            verbindenMitToolStripMenuItem.Name = "verbindenMitToolStripMenuItem";
            verbindenMitToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
            verbindenMitToolStripMenuItem.Text = "Verbinden mit";
            // 
            // lokalToolStripMenuItem
            // 
            lokalToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("lokalToolStripMenuItem.Image")));
            lokalToolStripMenuItem.Name = "lokalToolStripMenuItem";
            lokalToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            lokalToolStripMenuItem.Text = "Lokal";
            // 
            // serverToolStripMenuItem
            // 
            serverToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("serverToolStripMenuItem.Image")));
            serverToolStripMenuItem.Name = "serverToolStripMenuItem";
            serverToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            serverToolStripMenuItem.Text = "Server";
            // 
            // offlineToolStripMenuItem
            // 
            offlineToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("offlineToolStripMenuItem.Image")));
            offlineToolStripMenuItem.Name = "offlineToolStripMenuItem";
            offlineToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            offlineToolStripMenuItem.Text = "Offline";
            // 
            // toolStripMenuItem1
            // 
            toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            startenToolStripMenuItem,
            beendenToolStripMenuItem1,
            ändernToolStripMenuItem,
            toolStripSeparator4,
            toolStripComboBox1,
            toolStripComboBox2});
            toolStripMenuItem1.Name = "toolStripMenuItem1";
            toolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
            toolStripMenuItem1.Text = "Projekt";
            // 
            // startenToolStripMenuItem
            // 
            startenToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("startenToolStripMenuItem.Image")));
            startenToolStripMenuItem.Name = "startenToolStripMenuItem";
            startenToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
            startenToolStripMenuItem.Text = "Starten";
            // 
            // beendenToolStripMenuItem1
            // 
            beendenToolStripMenuItem1.Enabled = false;
            beendenToolStripMenuItem1.Image = ((System.Drawing.Image)(resources.GetObject("beendenToolStripMenuItem1.Image")));
            beendenToolStripMenuItem1.Name = "beendenToolStripMenuItem1";
            beendenToolStripMenuItem1.Size = new System.Drawing.Size(181, 22);
            beendenToolStripMenuItem1.Text = "Beenden";
            // 
            // ändernToolStripMenuItem
            // 
            ändernToolStripMenuItem.Enabled = false;
            ändernToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("ändernToolStripMenuItem.Image")));
            ändernToolStripMenuItem.Name = "ändernToolStripMenuItem";
            ändernToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
            ändernToolStripMenuItem.Text = "Ändern";
            // 
            // toolStripSeparator1
            // 
            toolStripSeparator1.Name = "toolStripSeparator1";
            toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
            // 
            // toolStripSeparator4
            // 
            toolStripSeparator4.Name = "toolStripSeparator4";
            toolStripSeparator4.Size = new System.Drawing.Size(178, 6);
            // 
            // toolStripComboBox1
            // 
            toolStripComboBox1.Name = "toolStripComboBox1";
            toolStripComboBox1.Size = new System.Drawing.Size(121, 23);
            // 
            // toolStripComboBox2
            // 
            toolStripComboBox2.Name = "toolStripComboBox2";
            toolStripComboBox2.Size = new System.Drawing.Size(121, 23);
 
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            Application.Run();
        }
 
        private static void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!popUpWnd1.visible)
                {
                    popUpWnd1.Width = 400;
                    popUpWnd1.Height = 300;
                    popUpWnd1.opacity = 75;
                    popUpWnd1.TimeToLive = 5;
 
                    popUpWnd1.ShowWindow("", "Bli bla blub");
                }
            }
        }
 
        private static void timer1_Tick(object sender, EventArgs e)
        {
            int i = SystemParametersInfo(screensaverrunning, 0, ref bScreenSaverrunning, 0);
            if (bScreenSaverrunning)
            {
                // MessageBox.Show("Machst Du Licht aus?");
                //do Desired Action
            }
        }
    }
}

Open in new window

0
andre72
Asked:
andre72
  • 2
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
With a quick look over your code, it looks like you never assign the the ContextMenuStrip to the NotifyIcon - something that is probably done now in the form's code somewhere, which is why it works when you load/display the form.

I have a similar application; I created a form, and that forms constructor handles initialization of the menu's and tray icon.  Then I create an instance of the form, without ever assigning it to a variable or showing it.

public static class Program
{
  static void main()
  {
      new frmStartup();
      Application.Run();
  }
}

public class frmStartup : System.Windows.Form
{
  public frmStartup()
  {
    NotifyIcon ni = new NotifyIcon();
    ContextMenuStrip cms = new ContextMenuStrip();

    cms.Items.Add(blah blah blah);
    ni.ContextMenuStrip = cms;    // <-- This is the line missing from your main()
  }
}
0
 
andre72Author Commented:
But this is done here:
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
0
 
Todd GerbertIT ConsultantCommented:
Sorry...a lot there to read, I overlooked it.

It's the contextMenuStrip1.SuspendLayout(); on line 69.  Either remove it, or add contextMenuStrip1.ResumeLayout() later in your code.
0
 
andre72Author Commented:
Doesn't matter as you got it - thanks!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now