Make C # Console as if it were a Windows form (close, tray icon, Event Response, manual exit, etc)

Source: Internet
Author: User
Tags exit in

Preface:You can define this article as an article that is not very practical and does nothing to do. Well, indeed, it hurts. I also agree that even if you don't want to do webservers, winform, and so on, you just want to use the console as a service and a program, okay? Yes, but the console exits when it is closed at a time. Sometimes it is a bit wrong. There is no tray icon in the console, and you want to minimize the tray icon. What else do you have to think about? Come on, let's implement them.

Need to know:The console application is a console program. The console is not a winform, so we cannot set its close events. Although you can add a tray icon by adding a reference to the console, the tray icon has no event. Wow, it's not impossible.

So you also need to know:We can capture close events by referencing external dll APIs. We can add a message event loop to the console to capture the event response so that the tray icon can trigger the event.

So our idea is:Disable the close button so that you can enter exit in the console to exit. When the console is closed by other events, you can proceed. Use Application. DoEvents () to capture message event processing, but use an endless loop for control. How can we listen for input? We are opening a thread to listen for input. It's a problem!

Don't panic. What else do you need to solve:First, understand what the code I provided implements. Enable the disable button, add the tray icon, and handle the event. What do you want to do? Of course you can not do it. If you want to hurt yourself, let's solve this problem. The tray icon does not disappear when you exit the console. Alas, this is a bug. How can this problem be solved? Capture the Close event and clear the tray icon when you want to close it. First, let me tell you how to implement it. I have succeeded in the experiment. If it is not shown here, it will hurt you too.

The Code is as follows:

/*
* Demo of disabling the close button on the console and minimizing it to the system tray
*
* Control through the ConsoleWin32 class
* Add reference System. Runtime. InteropServices; and System. Threading; to disable the close button.
* Add reference System. Drawing; and System. Windows. Forms; for System Tray
*
*/

UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. Linq;
UsingSystem. Text;
UsingSystem. Runtime. InteropServices;
UsingSystem. Threading;
UsingSystem. Drawing;
UsingSystem. Windows. Forms;

NamespaceTray_beta_1
{
ClassProgram
{
Static Bool_ IsExit= False;

Static VoidMain (String[] Args)
{
Console. Title= "TestConsoleLikeWin32";
Lelewin32helper. ShowNotifyIcon ();
Lelewin32helper. DisableCloseButton (Console. Title );

Thread threadMonitorInput= NewThread (NewThreadStart (MonitorInput ));
ThreadMonitorInput. Start ();

While(True)
{
Application. DoEvents ();
If(_ IsExit)
{
Break;
}
}
}

Static VoidMonitorInput ()
{
While(True)
{
StringInput=Console. ReadLine ();
If(Input= "Exit")
{
_ IsExit= True;
Thread. CurrentThread. Abort ();
}
}
}
}

ClassConsoleWin32Helper
{
StaticConsoleWin32Helper ()
{
_ Policyicon. Icon= NewIcon (@"G: BruceLi TestConsoleAppTestConsoleAppsTraySmall. ico");
_ Policyicon. Visible= False;
_ Policyicon. Text= "Tray";

ContextMenu menu= NewContextMenu ();
MenuItem item= NewMenuItem ();
Item. Text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.