一、如何处理所有的控制台消息。
第一步,首先要安装一个事件钩子,也就是说要建立一个回调函数。调用Win32 API,原型如下:
BOOL SetConsoleCtrlHandler(
PHANDLER_ROUTINE HandlerRoutine, // 回调函数
BOOL Add // 表示添加还是删除
);
参数HandlerRoutine是一个指向函数的指针,原型如下:
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType // 控制事件类型
);
所有的HandlerRoutine函数只有一个参数dwCtrlType,他表示控制台发出了什么消息。参数有下列值:
CTRL_C_EVENT - 当用户按下了CTRL+C,或者由GenerateConsoleCtrlEvent API发出.
CTRL_BREAK_EVENT - 用户按下CTRL+BREAK, 或者由GenerateConsoleCtrlEvent API发出.
CTRL_CLOSE_EVENT - 当试图关闭控制台程序,系统发送关闭消息。
CTRL_LOGOFF_EVENT - 用户退出时,但是不能决定是哪个用户.
CTRL_SHUTDOWN_EVENT - 当系统被关闭时.
当收到事件的时候,HandlerRoutine可以选择处理,或者简单的忽略。如果回调函数选择忽略,函数返回FALSE,系统将处理下一个钩子程序。如果处理消息,程序在处理完消息后应该返回TRUE。
CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT和CTRL_SHUTDOWN_EVENT通常被用来处理一些程序的清理工作,然后调用ExitProcess API。另外,这三个事件有超时机制,CTRL_CLOSE_EVENT是5秒,另外两个是20秒。如果程序超时候,系统将会弹出结束进程的对话框。如果用户选择了结束进程,任何清理工作都不会做,所以应该在超时时间内完成工作。下面是一个回调函数的例子:
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
char mesg[128];
switch(CEvent)
{
case CTRL_C_EVENT:
MessageBox(NULL,
"CTRL+C received!","CEvent",MB_OK);
break;
case CTRL_BREAK_EVENT:
MessageBox(NULL,
"CTRL+BREAK received!","CEvent",MB_OK);
break;
case CTRL_CLOSE_EVENT:
MessageBox(NULL,
"Program being closed!","CEvent",MB_OK);
break;
case CTRL_LOGOFF_EVENT:
MessageBox(NULL,
"User is logging off!","CEvent",MB_OK);
break;
case CTRL_SHUTDOWN_EVENT:
MessageBox(NULL,
"User is logging off!","CEvent",MB_OK);
break;
}
return TRUE;
}
好,现在已经有了回调函数,再来看看怎么安装钩子:
if (SetConsoleCtrlHandler(
(PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
{
// unable to install handler...
// display message to the user
printf("Unable to install handler!/n");
return -1;
}
第一个参数是函数指针,就是上面的那个函数。第二个参数是标志,如果为TRUE那么就安装钩子,如果为FALSE那么删除钩子。
好了,在安装了钩子后,我们就能收到控制台消息了,在程序退出前,要删除钩子。很简单吧。
二、控制台程序的全屏显示控制,这里用到了kernal32中的两个系统函数GetConsoleDisplayMode和SetConsoleDisplayMode,请看下面的一个小例子程序:
// created: 2006/11/25
// created: 25:11:2006 23:57
// filename: console_close.cpp
// file path: console_close
// file base: console_close
// file ext: cpp
// author: 张 尹
// my web : http://zyin3526.blog.163.com
//
// purpose:
// *********************************************************************
#include " stdafx.h "
#include < stdio.h >
#include < tchar.h >
#include < windows.h >
#include < wincon.h >
#include < string >
#include < tchar.h >
// *********************************************************************
// funcname : full_screen
// author : 张 尹
// created : 2006/11/25
// my web : http://zyin3526.blog.163.com
//
// purpose : 本函数实现控制台程序窗口的全屏显示功能,
// 使用了系统的两个函数说明如下:
// Kernel32!GetConsoleDisplayMode (NT specific!)
// The function retrieves current display mode of
// the console application.
// BOOL GetConsoleDisplayMode(
// LPDWORD lpdwMode // address of variable for
// // current value of display mode
// );
// Kernel32!SetConsoleDisplayMode (NT specific!)
// The function changes current display mode of
// the console application.
// BOOL SetConsoleDisplayMode(
// HANDLE hOut, // standard output handle
// DWORD dwNewMode, // specifies the display mode
// LPDWORD lpdwOldMode // address of variable for
// // previous value of display mode
// );
// *********************************************************************
void full_screen()
... {
typedef BOOL (WINAPI *PROCGETCONSOLEDISPLAYMODE)(LPDWORD);
typedef BOOL (WINAPI *PROCSETCONSOLEDISPLAYMODE)(HANDLE,DWORD,LPDWORD);
PROCGETCONSOLEDISPLAYMODE GetConsoleDisplayMode;
PROCSETCONSOLEDISPLAYMODE SetConsoleDisplayMode;
HMODULE hKernel32 = GetModuleHandle("kernel32");
if (hKernel32)
...{
GetConsoleDisplayMode = (PROCGETCONSOLEDISPLAYMODE)
GetProcAddress(hKernel32,"GetConsoleDisplayMode");
if (GetConsoleDisplayMode)
...{
SetConsoleDisplayMode = (PROCSETCONSOLEDISPLAYMODE)
GetProcAddress(hKernel32,"SetConsoleDisplayMode");
DWORD dwOldMode;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleDisplayMode(hOut,true,&dwOldMode); // 这里第二个参数为true表示全屏模式
}
}
return;
}
// **********************************************************************
// funcname : Handler.cpp
// author : 张 尹
// created : 2006/11/25
// my web : http://zyin3526.blog.163.com
//
// purpose : 本函数实现是控制台程序的消息处理程序
// **********************************************************************
BOOL WINAPI Handler(DWORD ctrlEvent)
... {
switch (ctrlEvent)
...{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
...{
// char c_var;
// _tprintf (_T ("你按了CTRL+C 真的要离开吗(Y/N)?"));
// scanf("%c",&c_var);
// if ((c_var == 'y') || (c_var == 'Y'))
// {
// return false;
// }
if (MessageBox(NULL,
"张尹提醒:你真的要离开吗?",
"关闭 !!!",
MB_OKCANCEL | MB_ICONQUESTION | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_APPLMODAL)==IDOK)
...{
ExitProcess(0);
}
else
...{
return TRUE;
}
return true;
}
default:
return true;
}
return true;
}
void main( int argc, char * argv[])
... {
char Title[] = ...{"打字联系程序V1.0 (作者:张尹 http://zhangyin.cnblogs.com)"};
SetConsoleTitle(Title);
// full_screen();
if (!SetConsoleCtrlHandler(Handler, TRUE))
...{
LPTSTR lpvSysMsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpvSysMsg, 0, NULL);
_tprintf(_T("设置事件句柄时出错: %s "), lpvSysMsg);
}
char buff[50] = ...{0};
printf("^-^你好,现在是全屏模式练习打字,退出请按Ctrl+C ");
for (;;)
...{
printf("请输入: ");
scanf("%s", buff);
printf("你输入的字母是: %s ",buff);
Sleep(50);
}
return;
}