Solved

URGENT:  Creating a Tray Application in C#

Posted on 2003-12-11
8
431 Views
Last Modified: 2010-04-16
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
Comment
Question by:jjacksn
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9930163
You might need to use an API called ShowWindow

ShowWindow(Handle, SW_HIDE);
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9930178
testn,

this should hide thie window from the alt-tab?  
0
 
LVL 18

Accepted Solution

by:
testn earned 500 total points
ID: 9930205
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 5

Author Comment

by:jjacksn
ID: 9930230
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
 
LVL 18

Expert Comment

by:testn
ID: 9931483
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
 
LVL 9

Expert Comment

by:malharone
ID: 9932111
an easier way ...
set
form.showintaskbar=false
0
 
LVL 9

Expert Comment

by:malharone
ID: 9932136
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
 
LVL 5

Author Comment

by:jjacksn
ID: 10240067
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question