Solved

URGENT:  Creating a Tray Application in C#

Posted on 2003-12-11
8
429 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
Comment Utility
You might need to use an API called ShowWindow

ShowWindow(Handle, SW_HIDE);
0
 
LVL 5

Author Comment

by:jjacksn
Comment Utility
testn,

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

Accepted Solution

by:
testn earned 500 total points
Comment Utility
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
 
LVL 5

Author Comment

by:jjacksn
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:testn
Comment Utility
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
Comment Utility
an easier way ...
set
form.showintaskbar=false
0
 
LVL 9

Expert Comment

by:malharone
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now