www.pudn.com > WinConsole.zip > test.cpp
// define _MT so that _beginthread( ) is available #ifndef _MT #define _MT #endif #include#include #include #include "resource.h" // global flag bool bDone = false; // this function is called by a new thread void InputThreadProc( void *dummy ) { // create the dialog window HWND hWnd = ::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 following text in a second 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"); } }