Adding Windows To Your Console Application

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

Collapse
/********************** 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 ************************/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值