{"id":17723825,"url":"https://github.com/honghaoz/use-console-in-mfc","last_synced_at":"2025-03-31T13:47:31.486Z","repository":{"id":8706857,"uuid":"10373793","full_name":"honghaoz/Use-Console-in-MFC","owner":"honghaoz","description":"Use console in MFC","archived":false,"fork":false,"pushed_at":"2014-06-04T22:49:27.000Z","size":168,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-07-31T15:47:14.313Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/honghaoz.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-30T02:30:12.000Z","updated_at":"2023-07-31T15:47:14.314Z","dependencies_parsed_at":"2022-07-09T21:16:13.344Z","dependency_job_id":null,"html_url":"https://github.com/honghaoz/Use-Console-in-MFC","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honghaoz%2FUse-Console-in-MFC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honghaoz%2FUse-Console-in-MFC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honghaoz%2FUse-Console-in-MFC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honghaoz%2FUse-Console-in-MFC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/honghaoz","download_url":"https://codeload.github.com/honghaoz/Use-Console-in-MFC/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246476192,"owners_count":20783799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-25T15:44:08.160Z","updated_at":"2025-03-31T13:47:31.456Z","avatar_url":"https://github.com/honghaoz.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"Using Console in MFC\n=====================\n\n**例如一个对话框程序。**\n\n```\nBOOL CTDlg::OnInitDialog()\n{\n\t::AllocConsole(); // 打开控制台资源\n\tfreopen(\"CONOUT$\", \"w+t\", stdout); // 申请写\n\tfreopen(\"CONIN$\",\"r+t\",stdin);\n}\n```\n\n**然后在需要用到的地方添加这段代码：**\n\n```\n#ifndef __GUICON_H__\n#define __GUICON_H__\n#include \u003cwindows.h\u003e\n#include \u003cstdio.h\u003e\n#include \u003cfcntl.h\u003e\n#include \u003cio.h\u003e\n#include \u003ciostream\u003e\n#include \u003cfstream\u003e\nusing namespace std;\n\n// maximum mumber of lines the output console should have\nstatic const WORD MAX_CONSOLE_LINES = 500;\nvoid RedirectIOToConsole();\nvoid RedirectIOToConsole()\n{\n\tint hConHandle;\n\tHANDLE lStdHandle;\n\tCONSOLE_SCREEN_BUFFER_INFO coninfo;\n\tFILE *fp;\n\t// allocate a console for this app\n\tAllocConsole();\n\t// set the screen buffer to be big enough to let us scroll text\n\n\tGetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), \u0026coninfo);\n \tconinfo.dwSize.Y = MAX_CONSOLE_LINES;\n \tSetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);\n \t// redirect unbuffered STDOUT to the console\n \tlStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);\n \thConHandle = _open_osfhandle((INT_PTR)lStdHandle, _O_TEXT);\n \tfp = _fdopen( hConHandle, \"w\" );\n \t*stdout = *fp;\n \tsetvbuf( stdout, NULL, _IONBF, 0 );\n \t// redirect unbuffered STDIN to the console\n \tlStdHandle = GetStdHandle(STD_INPUT_HANDLE);\n \thConHandle = _open_osfhandle((INT_PTR)lStdHandle, _O_TEXT);\n \tfp = _fdopen( hConHandle, \"r\" );\n \t*stdin = *fp;\n \tsetvbuf( stdin, NULL, _IONBF, 0 );\n \t// redirect unbuffered STDERR to the console\n \tlStdHandle = GetStdHandle(STD_ERROR_HANDLE);\n \thConHandle = _open_osfhandle((INT_PTR)lStdHandle, _O_TEXT);\n \tfp = _fdopen( hConHandle, \"w\" );\n \t*stderr = *fp;\n \tsetvbuf( stderr, NULL, _IONBF, 0 );\n \t// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog \n \t// point to console as well\n \tios::sync_with_stdio();\n}\n#endif \n```\n**然后就可以用cout在console里输出了。**\n\n**最后，记得在结束程序前释放掉。**\n\n```\nAllocConsole();  \nfreopen(\"CONOUT$\",\"w+t\",stdout);  \nfreopen(\"CONIN$\",\"r+t\",stdin);  \ncout \u003c\u003c \"Input:\" \u003c\u003cendl;\nint iTest = 0;\ncin \u003e\u003e iTest;\nfclose(stdout);\nfclose(stdin);\nFreeConsole();\n```\n\n\n_Reference:_ \n\n\u003chttp://blog.csdn.net/everettjf/article/details/5931043\u003e\n\u003chttp://blog.csdn.net/acaiacc/article/details/5543669\u003e\n\u003chttp://ygdljg.blog.163.com/blog/static/54601046200893042229423/\u003e\n\n\u003chttp://blog.csdn.net/VisualEleven/article/details/5517541\u003e\n\u003chttp://nianning1981.blog.163.com/blog/static/30830143201002632546873/\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhonghaoz%2Fuse-console-in-mfc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhonghaoz%2Fuse-console-in-mfc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhonghaoz%2Fuse-console-in-mfc/lists"}