How to add the MFC dialog box on the Win32 Console (adding windows to your console application)

Source: Internet
Author: User

ConsoleProgramThe biggest difference with the window program is that there is no message loop mechanism before, while the latter has. I have been wondering how to add a dialog box in the console application. Maybe you need to pop up a dialog box in the console to allow users to enter some text information or select a message in the drop-down box. Maybe this articleArticleWhat is the obvious purpose in the actual use process.

Now let's take "Hello, world" as an example. In the main () function, we use the _ beginthread API to create a thread. In the thread, we create a simple dialog box (after the dialog box resource file may be generated by other MFC programs, copy the file to the current program directory and add it to the project. This is my understanding .)

 

CodeAs follows:

// DEFINE _ Mt so that _ beginthread () is available <br/> # ifndef _ mt <br/> # DEFINE _ mt <br/> # endif </P> <p> # include "stdio. H "<br/> # include" windows. H "<br/> # include" process. H "<br/> # include" resource. H "</P> <p> // global flag <br/> bool bdone = false; </P> <p> // This function is called by a new thread <br/> void inputthreadproc (void * dummy) <br/>{< br/> // create the dialog window <br/> hwndhwnd =: c Reatedialog (null, <br/> makeintresource (idd_dialog), null, null); <br/> If (hwnd! = NULL) <br/>{< br/> // show dialog <br/>: showwindow (hwnd, sw_show ); <br/>}< br/> else <br/> {<br/> printf ("failed to create dialog/N"); <br/> bdone = true; <br/> return; <br/>}< br/> // message loop to process user input <br/> MSG; <br/> while (1) <br/>{< br/> If (: peekmessage (& MSG, hwnd, 0, 0, pm_remove) <br/>{< br/> If (MSG. message = wm_keyup) <br/>{< br/> int encode rtkey = (INT) MS G. wparam; <br/> // if the user pressed the Escape key, then <br/> // print the text the user entered and quit <br/> If (export rtkey = vk_escape) <br/>{< br/> // get the Edit Control <br/> hwnd hedit =: getdlgitem (hwnd, idc_edit); <br/> If (hedit) <br/> {<br/> // get the input text the user entered <br/> // and print it to the console window <br/> char ptext [3201]; <br/> int nsize =: getwindowte XT (hedit, <br/> ptext, 3200); <br/> ptext [nsize] = 0; <br/> printf ("/Nyou have entered "); <br/> printf ("following text in a second"); <br/> printf ("thread:/n % s/n", ptext ); <br/>}< br/> else <br/> {<br/> printf ("failed to get Edit Control/N "); <br/>}< br/> // destroy the dialog and get out of <br/> // The message loop <br/>: destroywindow (hwnd ); <br/> bdone = true; <br/> break; <br/>} <Br/>}< br/> // process message <br/>: translatemessage (& MSG); <br/>:: dispatchmessage (& MSG ); <br/>}< br/> else <br/> {<br/> // if there is no message to process, <br/> // then sleep for a while to avoid burning <br/> // too much CPU cycles <br/>: Sleep (100 ); <br/>}</P> <p> void main (INT argc, char ** argv) <br/>{< br/> printf ("Hello, world of console apps/N"); <br/> // re Ate a new thread to allow user input <br/> If (_ beginthread (inputthreadproc, 0, null) =-1) <br/>{< br/> printf ("failed to create thread"); <br/> return; <br/>}< br/> // wait for the new thread to finish <br/> while (! Bdone) <br/>{< br/> // sleep 3 seonds <br/>: Sleep (3000 ); <br/> printf ("main thread running/N "); <br/>}</P> <p> /********************** *****************************/<br/>

 

Source: http://www.codeproject.com/KB/winsdk/winconsole.aspx

During the translation process, the main idea is translated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.