Event processing of console procedures

zhaozj2021-02-17  49

Event processing of console procedures

Author: Peng Wei (West dare handsome) Email: pwei@grasp.com.cn

Reprint, please ask the author agreed

2003-7-10

The console program is very powerful and convenient in the role of the Windows program, like VC, C #, Delphi, and more, a lot of powerful languages ​​support console procedures. She does not have a complex GUI, which is completely 32-bit programs, which can call APIs other than the GDI function, support multithreading, supporting MFC, and more. Use her to debug procedures, learning program design, and experiment, etc., it is not appropriate. I often write my experimental program to write the console method, which is very convenient.

The Console program is not as messaging like the Win32 GUI program, so it is not known when the program is interrupted. If the program is handling a long job, when the user wants to exit the system, or press CTRL C (Ctrl Break), or when the system will be turned off, our data may be lost. Is there any way? Oh, no, there is a way. Look below, I will talk to you about event processing about the console program.

To make the console program have event handling power to use the following API functions, their prototype declaration is as follows:

Bool setconsolectrlhandler

Phandler_Routine HandlerRoutine, // Handler Function

Bool Add // Add or Remove Handler

);

HandlerRoutine points to an event handler, yes, maybe you have guessed, this function is equivalent to the window handling function in the Win32 GUI program. The prototype of this function is as follows:

Bool WinApi Handlerroutine

DWORD DWCTRLTYPE / / CONTROL SIGNAL TYPE

);

DWCTRTYPE of this function indicates a received control signal, which may be one of the following values:

Letter

Describe

CTRL_C_EVENT

A Ctrl C signal is received, the signal or from the keyboard, or from the generateconsolectrlevent function

CTRL_BREAK_EVENT

A CTRL BREAK signal is received, the signal or from the keyboard, or from the generateconsolectrlevent function

CTRL_CLOSE_EVENT

When the user system turns off the console, the system will send this signal to this

CTRL_LOGOFF_EVENT

When the user exits the system, the system will send this signal to all Console programs. This signal cannot be displayed which user exits.

CTRL_SHUTDOWN_EVENT

This signal is sent to all Console programs when the system will be turned off.

In the program, the HandlerRoutine can perform the corresponding processing or ignore the event when receiving the above events. If you choose to ignore the event, HandlerRoutine must return false, otherwise returns True.

When we install HandlerRoutine with SetConsolectrlHandler, the add parameter should be set to true. If you want to delete the already installed handlerroutine, use this function, just set the add to false.

In addition, you can generate Ctrl_C_Event and CTRL_BREAK_EVENT events with the GenerateConsolectrlevent function, and we can use this function. We can make more smartly and flexible control programs in our program. This API method is very simple, I don't say it here, I can participate in the MSDN or API manual. Below we will write a very simple program:

INT _Tmain (int Argc, _tchar * argv [])

{

Char BUF [256];

IF (SetConsolectrlhandler (// Installation Event Processing

(PHANDLER_ROUTINE) ConsoleHandler, True) == false)

{

// installation failed

Printf ("Unable to Install Event Handler! / N");

Return -1;

}

GenerateConsolectrlevent (ctrl_c_event, 0); // Manually generate an event

Scanf ("% s", buf); // simulate waiting incident, if you don't run this

When the program is programmed, the program is flashing, it is not anxious.

// The program.

Return 0;

}

Bool WinApi ConsoleHandler (DWORD CEVENT)

{

Switch (CEVENT)

{

Case ctrl_c_event:

Messagebox (NULL,

"Ctrl C Received!", "Signal", MB_OK;

Break;

Case Ctrl_break_event:

Messagebox (NULL,

"Ctrl Break Received!", "Signal", MB_OK;

Break;

Case Ctrl_Close_Event:

Messagebox (NULL,

"Program Being Closed!", "Signal", MB_OK;

Break;

Case ctrl_logoff_event:

Messagebox (NULL,

"User is logging off!", "Sternal", mb_ok);

Break;

Case Ctrl_shutdown_event:

Messagebox (NULL,

"User is logging off!", "Sternal", mb_ok);

Break;

}

Return True;

}

This program is very simple, but the principle is very useful, I hope you can understand what I am talking about, okay, don't say it, I have been very busy, I have time, I want to discuss it with you. I hope to be helpful.

转载请注明原文地址:https://www.9cbs.com/read-28527.html

New Post(0)