如何在win32控制台加入MFC對話方塊(Adding Windows To Your Console Application)

來源:互聯網
上載者:User

     控制台程式與window程式最大的區別在於前面沒有訊息迴圈機制,而後者有。我一直在想如何才能在控制台應用程式中添加一個對話方塊,也許有時你需要在控制台彈出一個對話方塊讓使用者輸入一些文本資訊或者在下拉框中選擇一項資訊,也許這篇文章在實際用過程中有什麼明顯的用途。

      現在我們以“Hello,world”為例來說明。我們在Main()函數中通過_beginthread API 建立一個線程,線上程中,建立一個簡單的對話方塊(對話方塊資源檔可能由其它MFC程式產生後,將檔案複製到當前程式目錄下,並自行添加到工程中,這是我自己理解。)

 

代碼如下:

// 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 = ::CreateDialog(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 msg;<br /> while(1)<br /> {<br /> if ( ::PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) )<br /> {<br /> if ( msg.message==WM_KEYUP )<br /> {<br /> int nVirtKey = (int) msg.wParam;<br /> // if the user pressed the ESCAPE key, then<br /> // print the text the user entered and quit<br /> if ( nVirtKey==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 = ::GetWindowText( hEdit,<br /> pText, 3200 );<br /> pText[nSize] = 0;<br /> printf("/nYou have entered the ");<br /> printf("following text in a second ");<br /> printf("thread:/n/n%s/n/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 /> }<br /> }<br />} </p><p>void main( int argc, char** argv )<br />{<br /> printf("Hello, world of console apps/n");<br /> // create 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 /> }<br />} </p><p>/************************** end ************************/<br />

 

原文出處:http://www.codeproject.com/KB/winsdk/winconsole.aspx 

翻譯過程中,將主要的思想翻譯過來。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.