Sign in to follow this  
Ned_K

Associating a Window Procedure with the Console?

Recommended Posts

Is it possible to create a peekmessage loop for the console that dispatches console messages to a window procedure? Anyone have some simple code showing this or a link to a discussion elsewhere? Thanks!

Share this post


Link to post
Share on other sites
This is not possible. The console is a special invocation of cmd.exe and you are really limited to what you can do with it.

The closest thing you can do to process console events is setup a console control handler using SetConsoleCtrlHandler. Here's a simply example showing it:


#include <windows.h>
#include <stdio.h>

HANDLE hExitEvent = 0;

BOOL __stdcall ConsoleHandler(DWORD ConsoleEvent)
{
switch(ConsoleEvent)
{
case CTRL_LOGOFF_EVENT:
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT:
{
SetEvent(hExitEvent);
return TRUE;
}
}
return FALSE;
}

int main(int argc, char * argv[])
{
hExitEvent = CreateEvent(0, TRUE, FALSE, 0);
SetConsoleCtrlHandler(ConsoleHandler, TRUE);
WaitForSingleObject(hExitEvent, INFINITE);
SetConsoleCtrlHandler(ConsoleHandler, FALSE);
return 0;
}



Normally, if you did not set the console control handler, you would get a "This application is not responding" dialog when you tried to close it. Since the close event is properly handled now, you can gracefully exit the console in any number of ways (ctrl + c, close the window, on demand).

Share this post


Link to post
Share on other sites
Judging from your recent posts, it sounds like you want to create a "pretty" console window. If so, you might want to look at the source code for similar projects like this one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this