[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

URGENT: Creating a Tray Application in C#

I want to create a tray application in C#, which I have done.  However, to run the app, I am calling Sytem.Applicaton(run) which loads the window form of my class.  This in turn, loads a blank form, which is hidden and does not appear in the task bar.  The tray app runs find and all is well and good.  

However, a user can alt-tab to it, and sense the form is useless, I would like to avoid this.  Is there a way I can avoid this?

Also, I need to send windows messages to it.  Is this possible to do with no window?  
0
jjacksn
Asked:
jjacksn
  • 3
  • 3
  • 2
1 Solution
 
testnCommented:
You might need to use an API called ShowWindow

ShowWindow(Handle, SW_HIDE);
0
 
jjacksnAuthor Commented:
testn,

this should hide thie window from the alt-tab?  
0
 
testnCommented:
using System.Runtime.InteropServices;

        [DllImport("user32.dll")]
        private static extern uint ShowWindow( IntPtr hWnd, int nCmd );

......

where SW_HIDE = 0

in your form... this refer to System.Windows.Forms.Form instance
ShowWindow(this.Handle, SW_HIDE);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jjacksnAuthor Commented:
testn, thanks, worked perfectly.  

Now, I guessing this is not the proper way to be doing this.  Must one have an actual window open to receive messages?  If I implement IMessageFilter, can I just somehow enter the message pump in main and then receive the messages and mouse clicks properly vs. doing what I am doing now?
0
 
testnCommented:
You're right.... there is no proper way to do that as far as I know... I am sure why tray application will not accept the windows message. *deeply exhale*
0
 
malharoneCommented:
an easier way ...
set
form.showintaskbar=false
0
 
malharoneCommented:
this is the actual form .. hides the current form with systray icon.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WindowsApplication3
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.NotifyIcon notifyIcon1;
            private System.ComponentModel.IContainer components;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
                  this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
                  //
                  // notifyIcon1
                  //
                  this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
                  this.notifyIcon1.Text = "notifyIcon1";
                  this.notifyIcon1.Visible = true;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Name = "Form1";
                  this.ShowInTaskbar = false;
                  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                  this.Text = "Form1";
                  this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                  this.Load += new System.EventHandler(this.Form1_Load);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  this.Visible=false;
            }
      }
}
0
 
jjacksnAuthor Commented:
testn, i lied.  that didn't really work.  It doesn' t show up in the taskbar or the task manager, but one can still alt-tab to it.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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