CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2008
    Posts
    161

    [RESOLVED] Create an application without a window?

    How can i create an application without a window (GUI).

    Thanks, i need this to make a file watcher for me and my friends.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Create an application without a window?

    In VisualStudio just create a Console Application.

  3. #3
    Join Date
    Mar 2008
    Posts
    161

    Re: Create an application without a window?

    Quote Originally Posted by MNovy View Post
    In VisualStudio just create a Console Application.
    I don't want the console window to be displayed either.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Create an application without a window?

    Make it a form application and hide the main form.

    Or... make it a windows service.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Create an application without a window?

    Application without GUI doesn't make sence unless it is kind of service, so I definitely recommend you to make is as a windows service.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6

    Re: Create an application without a window?

    It can make sense in some cases.

    Take a look at this:

    http://code.msdn.microsoft.com/TheNotifyIconExample

    I created a C# Visual studio project template that can make your GUI Invisible, or even have an Icon that is displayed in the System Tray that does nothing when clicked. and has many more options.

    It is a Visual Studio C# Project Template and all options are driven by Visual Studio User Project Settings, so there is no code needed to make changes, simple change a User Project Setting True/False. Not even a build is requried when options are changed.

    This kind of code is so redundant from project to project, that this can be a real time-saver in your future projects. Not just this example.

    Hope this helps

    How to do this and more and fast:

    1. Download and install this Microsoft Visual Studio Template from Click Here ("There are two C# project templates, one with comments 'TheNotifyIconExamplewc' and one without comments TheNotifyIconExamplewoc'") Click Open Or run to launch the Visual Studio Content Installer. Install Both templates, by making sure their checkbox is checked.

    NOTE: This will automatically install the C# Project Template(s) in Visual Studio using the Visual Studio Content Installer, these template(s) can be deleted/removed at anytime from your Visual Studio C# Project Template Folder.

    2. Open Microsoft Visual C# then do: File -> New Project -> My Templates -> Choose TheNotifyIconExample C# Visual Studio Project Template With Comments, for this example ("You can also choose the one without comments, if you like") .

    Choose a project name and click the "OK" button.


    This Picture displays All Of the Possible Settings Available For ANY Project Using these templates


    3. Using Microsoft Visual C# do: Project -> [Your Project Name] Properties -> Settings

    Set these settings to true and any other Form1 and NotifyIcon settings to false:

    This will display an Icon in The System tray;

    ShowSystemTrayNotifyIcon = true

    This will start your project in the System Tray with no Form on Startup;

    StartUsMinimized = true

    This will display ContextMenuStrip on both single left and right mouse clicks when your Icon in the System Tray is clicked. If you only want single right click mouse clicks to display, set this setting to false instead.

    NotifyIconMouseSingleLeftClickShowsContextMenuStrip = True

    4. Using Microsoft Visual C# from the solutions window.

    Right click on Form1.cs and choose View Designer.

    In the designer window, single left click on form1 image and replace the default Icon assigned, with your Icon in the properties window.

    Single left click TheNotifyIconExample_NotifyIcon and replace the default Icon assigned, with your Icon in the properties window.

    Change the default text in the properties window from "Single Right Click for [Your Project Name]" to "Single left or Right Click for [Your Project Name]" ("Do this only if you enabled single left mouse clicks to display the ContextMenuStrip when your Icon in the System tray is left single clicked, in Step #3 above.")

    5. Single left click the TheNotifyIconExample_contextMenuStrip then single left click Display [Your Project Name] in the properties window set Visible to equal false. This will not allow a form to be displayed, because this menu option to do so, will now not be visible.

    NOTES:

    If you do want a Icon in the System Tray, so that you could display a ContextMenuStrip but not be stoppable via GUI, set the Visible = false property for the ContextMenuStrip item Exit like this example shows for "Display [Your Project Name]" in Step #5 and this will allow you to have no Form1 but have both an Icon in the System tray but no Form1.

    If you wanted an Icon in the System tray to show you are running, but not be stoppable via GUI and not have any ContextMenuStrip all you need to do is Set All NotifyIcon and Form1 settings to false and StartUsMinimized = true, ShowSystemTrayNotifyIcon = true and NotifyIconMouseSingleRightClickDisabled = true. Now your icon will display in the System tray, not react to any mouse clicks, which means you will never display a ContextMenuStrip or Form1 or be GUI stoppable. You would want to also change your NotifyIcon Text like this example did in Step #4, to maybe just display your name as well.

    If you want total GUI invisibility with no Icon in the System Tray and no ContextMenuStrip simply set all NotifyIcon and Form1 settings to false and set StartUsTotallyInvisible = true.

    Now Buld your project.

    As you can see, while this may look tedious these facts are true:

    1. No code changes of any kind were required to do this.

    2. Menu Item text and NotifyIcon text is automatically set to your your safe project name, at project creation.

    3. You can change ANY of these settings at anytime, without any code changes.

    4. When or if you do change these settings no build will ever be required ("Of course if you change NotifyIcon text as we did in this example, you would need to re-build the project, other than that, no setting changes require any re-build, ever of your project.

    5. You can do this with any project and the code to do this is present at project creation.

    There are 784+ combinations of these settings that can be used for any project that require no code or build when changed and all of them have been tested.
    Last edited by ZOverLord; December 13th, 2008 at 06:06 AM. Reason: Added Complete Instructions

  7. #7
    Join Date
    Mar 2008
    Posts
    161

    Re: Create an application without a window?

    Thanks guys.

  8. #8
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Create an application without a window?

    try this code:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                this.Opacity = 0;
            }
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  9. #9
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Create an application without a window?

    i also use another trick:

    go to program.cs and make changes like this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace hideGui
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form frm = new Form1();
                Application.Run();
            }
        }
    }
    then to test it just add a timer to the form1 and in its tick event add code like this:

    Code:
        MessageBox.Show("No Gui!!!");
                timer1.Enabled = false;
    you will see that without any gui pop-up windows open (it means the code execute).

    i changed :
    Code:
     Application.Run(new From1()); // visual studio code
    to
    Code:
    Form frm = new Form1();  // my code
     Application.Run(); // my code
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  10. #10
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Create an application without a window?

    all you have to do is set your project's output type to Windows application, then just kick off your app from main (instead of doing Application.Run or instantiating any forms).

    if you set it to Windows Application, it does not AllocConsole / AttachConsole (which setting it to Console Application does under the covers).

    the only thing you have to do is to add some sort of waiting mechanism @ the end of your Main so the app doesn't run then immediately exit (like a Thread.Sleep(-1) or something).

    the dangerous thing about this is that there is no way to actually stop it once its launched w/out launching the task manager and ending it there.

    a windows app w/ a notify icon (which provides the ability to start / stop) is the better solution.
    Last edited by MadHatter; December 11th, 2008 at 04:51 PM.

  11. #11
    Join Date
    Nov 2008
    Location
    United States
    Posts
    81

    Re: Create an application without a window?

    Opacity to 0&#37; is the best way to do this in my opinion. I wouldn't play around with other methods that can cause bugs later.

    Just make sure if you use opacity you remember to set ShowInTaskbar to false, otherwise it wont work correctly anyway. It will just confuse the user when they start clicking on the taskbar button for the form and nothing happens. That's a really bad design.
    Three5Eight
    Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home

  12. #12
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Create an application without a window?

    good point
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  13. #13

    Re: Create an application without a window?

    Quote Originally Posted by Pale View Post
    How can i create an application without a window (GUI).

    Thanks, i need this to make a file watcher for me and my friends.
    If you want an Icon in the System Tray with or without a ContextMenuStrip, my example shows you how to pull this off here with many combinations:

    http://www.codeguru.com/forum/showpo...67&postcount=6

    This of course, also does support total GUI invisibility if you wish as well, and other options listed in the link above.

    So, do you want an Icon in the System Tray, with or without a ContextMenuStrip, stoppable, or not stoppable via GUI? If so, the other examples shown here, can't support that.

    Example: You may wish to support the ability to start/stop your file watcher, or select which files to watch from the ContextMenuStrip yet do this without a form ever being displayed and do it all instead, using a ContextMenuStrip and an Icon located in the System Tray. stoppable or not stoppable via GUI.

    You can play with the settings quickly, try different combinations, without code changes or even a re-build, and then make up your mind, which combinations you want, and even change them anytime and mutiple times in the future, again, without any code changes or a re-build required using these methods in these templates.
    Last edited by ZOverLord; December 14th, 2008 at 07:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured