Click here to Skip to main content
Licence 
First Posted 13 Jun 2001
Views 304,098
Bookmarked 70 times

Adding Windows To Your Console Application

By | 14 Jun 2001 | Article
Sample code to add a dialog (and message pump) to console applications

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

/********************** test.cpp **************************/ 

// define _MT so that _beginthread( ) is available 
#ifndef _MT 
#define _MT 
#endif 

#include "stdio.h" 
#include "windows.h" 
#include "process.h" 
#include "resource.h" 

// global flag 
bool bDone = false; 

// this function is called by a new thread 
void InputThreadProc( void *dummy ) 
{ 
    // create the dialog window 
    HWNDhWnd = ::CreateDialog(NULL,
        MAKEINTRESOURCE(IDD_DIALOG),NULL,NULL); 
    if ( hWnd!=NULL ) 
    { 
        // show dialog 
        ::ShowWindow(hWnd,SW_SHOW); 
    } 
    else 
    { 
        printf("Failed to create dialog\n"); 
        bDone = true; 
        return; 
    } 
    // message loop to process user input 
    MSG msg; 
    while(1) 
    { 
        if ( ::PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) ) 
        { 
            if ( msg.message==WM_KEYUP ) 
            { 
                int nVirtKey = (int) msg.wParam; 
                // if the user pressed the ESCAPE key, then 
                // print the text the user entered and quit 
                if ( nVirtKey==VK_ESCAPE ) 
                { 
                    // get the edit control 
                    HWND hEdit = ::GetDlgItem(hWnd,IDC_EDIT); 
                    if ( hEdit ) 
                    { 
                        // get the input text the user entered 
                        // and print it to the console window 
                        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"); 
                    } 
                    // destroy the dialog and get out of
                    // the message loop 
                    ::DestroyWindow(hWnd); 
                    bDone = true; 
                    break; 
                } 
            } 
            // process message 
            ::TranslateMessage(&msg); 
            ::DispatchMessage(&msg); 
        } 
        else 
        { 
            // if there is no message to process, 
            // then sleep for a while to avoid burning 
            // too much CPU cycles 
            ::Sleep(100); 
        } 
    } 
} 

void main( int argc, char** argv ) 
{ 
    printf("Hello, world of console apps\n"); 
    // create a new thread to allow user input 
    if( _beginthread(InputThreadProc, 0, NULL )==-1) 
    { 
        printf("Failed to create thread"); 
        return; 
    } 
    // wait for the new thread to finish 
    while ( !bDone ) 
    { 
        // sleep 3 seonds 
        ::Sleep(3000); 
        printf("main thread running\n"); 
    } 
} 

/************************** end ************************/ 


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembercyrilthom22:18 21 Sep '10  
Generallink to another solution PinmemberAndries Aarden21:38 29 Jan '09  
Generalusing buttons PinmemberCbraxa1:45 31 Jul '06  
GeneralRe: using buttons PinmemberOh Chi Min6:31 12 Dec '09  
GeneralGood Article Pinmemberdarena17:54 15 May '06  
GeneralRe: Good Article PinmemberXiangyang Liu20:54 15 May '06  
GeneralTest program UNICODE enabled Pinmemberernsts0:44 22 Apr '05  
GeneralConsole close button PinmemberKuling0:06 18 Jun '04  
GeneralRe: Console close button PinmemberXiangyang Liu0:26 18 Jun '04  
Questionhow to supress the console window!!!! its urgent PinsussAnonymous18:28 7 Apr '04  
AnswerRe: how to supress the console window!!!! its urgent PinmemberLK00119:47 7 Apr '04  
AnswerRe: how to supress the console window!!!! its urgent PinsussRob Krakoar2:56 10 Apr '04  
GeneralRe: how to supress the console window!!!! its urgent Pinmemberpwasser15:16 15 Feb '09  
GeneralHi everyone PinsussAnonymous0:33 8 Sep '03  
GeneralWindows/Console Apps (or the other way around?) PinsussDaniel Mathews20:21 23 Jun '03  
GeneralRe: Windows/Console Apps (or the other way around?) Pinmemberbrusithekid2:06 3 Dec '03  
GeneralUsing consoles in win32 applications PinsussAnonymous2:26 15 Feb '03  
GeneralRe: Using consoles in win32 applications PinmemberDaniel Lohmann2:59 15 Feb '03  
GeneralRe: Using consoles in win32 applications PinsussAnonymous1:49 16 Feb '03  
GeneralRe: Using consoles in win32 applications PinmemberGilgamesh8216:20 9 May '07  
Generallinux Pinmemberomicron2:56 25 Mar '02  
GeneralConsole Window Not Always Hiding PinmemberAnonymous22:30 11 Oct '01  
AnswerRe: Console Window Not Always Hiding Pinmemberzoom17:48 20 Oct '08  
QuestionMinimize Console App to System Tray? PinmemberGargan11:54 7 Oct '01  
AnswerRe: Minimize Console App to System Tray? PinmemberVC++ Boy2:18 8 Oct '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 15 Jun 2001
Article Copyright 2001 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid