Introduction
The title of this article must be
confusing to some people, because once you add a window, your app will no longer
be a console app.� But never mind the title, here is what I really
want to talk about.
Most of my co-workers have mainframe and UNIX backgrounds.� Even
though they are working in the windows (NT) environment now, they don't
know or care that much about windows.� Most of the programs we are
developing are tuxedo servers, which are console applications.� I
have plenty of GUI experiences with MFC, but have never done any serious
WIN32 API programming.� I might be wrong, but I think the difference
between a console app and a windows app is that a windows app has a message
pump, which is a loop in the code that processes incoming messages, and
a console app does not have this loop.� In MFC apps the message pump is buried
deeply in the framework classes that I never have to deal with it directly.
I am always wondering what does it take to add a dialog box into an
existing console application.� For example you may want to pop up
a dialog box in a console application to ask the user to type some input
text or make a decision by selecting an item from a combo box.� I
don't think the code provided in this article will be very useful in real
applications, but it does demonstrate something interesting.
I began with the famous "Hello, world" program and modified the main
function to create a new thread using the _beginthread
api and then wait for the thread to terminate.� In the new thread,
a simple dialog box was created from a resource and user can type any text
into it.� When the user presses the ESCAPE key, the input text entered
by the user will be printed to the console window, the new thread will
end and so will the program.� As you can see, the code is very simple
and the binary is very small.
Let me know if you find the code useful (no, you don't have to pay me
or give me credit). For my other articles and software, please visit
my home page. Thank you.
Source Listing
#ifndef _MT
#define _MT
#endif
#include "stdio.h"
#include "windows.h"
#include "process.h"
#include "resource.h"
bool bDone = false;
void InputThreadProc( void *dummy )
{
HWNDhWnd = ::CreateDialog(NULL,
MAKEINTRESOURCE(IDD_DIALOG),NULL,NULL);
if ( hWnd!=NULL )
{
::ShowWindow(hWnd,SW_SHOW);
}
else
{
printf("Failed to create dialog\n");
bDone = true;
return;
}
MSG msg;
while(1)
{
if ( ::PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) )
{
if ( msg.message==WM_KEYUP )
{
int nVirtKey = (int) msg.wParam;
if ( nVirtKey==VK_ESCAPE )
{
HWND hEdit = ::GetDlgItem(hWnd,IDC_EDIT);
if ( hEdit )
{
char pText[3201];
int nSize = ::GetWindowText( hEdit,
pText, 3200 );
pText[nSize] = 0;
printf("\nYou have entered the ");
printf("following text in a second ");
printf("thread:\n\n%s\n\n",pText);
}
else
{
printf("Failed to get edit control\n");
}
::DestroyWindow(hWnd);
bDone = true;
break;
}
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
{
::Sleep(100);
}
}
}
void main( int argc, char** argv )
{
printf("Hello, world of console apps\n");
if( _beginthread(InputThreadProc, 0, NULL )==-1)
{
printf("Failed to create thread");
return;
}
while ( !bDone )
{
::Sleep(3000);
printf("main thread running\n");
}
}