譬如我想在 console控制台的程序中,利用CTRL+X退出程序,该怎么做啊?
解决方案 »
- 有关画像处理的一段代码请求解释(小女初学画像,请各位大侠相助)
- 高手请进,关于TCP接收数据的问题
- 关注http://community.csdn.net/Expert/topic/3380/3380208.xml?temp=.642956拿大奖!
- 多线程程序中数据同步的问题
- 请问windows api里有没有旋转位图的函数
- 关于DATAGRID的一个问题 “The rowset is not bookmarkable”这是怎么回事?
- 关于createprocess的使用
- 一个我解决不了的问题,请各高手帮忙了,谢谢
- VC写的OCX控件,VB调用它的触发事件就会报错?
- 怎样在mfc中调用api中有的函数
- 菜鸟的创建bmp问题的帖子
- 同一台电脑上运行的两个控制台程序如何进行通讯?
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL+C signal.
case CTRL_C_EVENT:
Beep(1000, 1000);
return TRUE;
// CTRL+CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
return TRUE;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
return FALSE;
}
}
void main(void)
{
BOOL fSuccess;
fSuccess = SetConsoleCtrlHandler(
(PHANDLER_ROUTINE) CtrlHandler, // handler function
TRUE); // add to list
if (! fSuccess)
MyErrorExit("Could not set control handler");
}
VOID MouseEventProc(MOUSE_EVENT_RECORD);
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
VOID KeyEventProc(KEY_EVENT_RECORD);
VOID GetInputEvents(VOID);
DWORD main(VOID)
{
HANDLE hStdin;
DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
INPUT_RECORD irInBuf[128];
// Get the standard input handle.
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
MyErrorExit("GetStdHandle");
// Save the current input mode, to be restored on exit.
if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
MyErrorExit("GetConsoleMode");
// Enable the window and mouse input events.
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (! SetConsoleMode(hStdin, fdwMode) )
MyErrorExit("SetConsoleMode");
// Loop to read and handle the input events.
while (1)
{
// Wait for the events.
if (! ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead) ) // number of records read
MyErrorExit("ReadConsoleInput");
// Dispatch the events to the appropriate handler.
for (i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT: // mouse input
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
ResizeEventProc(
irInBuf[i].Event.WindowBufferSizeEvent);
break;
case FOCUS_EVENT: // disregard focus events
case MENU_EVENT: // disregard menu events
break;
default:
MyErrorExit("unknown event type");
break;
}
}
}
return 0;
} KeyEventProc键盘事件处理函数,处理下按键状态,该函数参数类型:KEY_EVENT_RECORD
MSDN查询下该结构的定义比较详细