zoukankan      html  css  js  c++  java
  • win32程序添加控制台程序

    #include "stdafx.h"
    #include <stdio.h>  
    #include <fcntl.h>  
    #include <io.h>  
    #include <iostream>  
    #include <fstream>
    #include <conio.h>
    
    #ifndef _USE_OLD_IOSTREAMS  
    using namespace std;  
    #endif  
    
    // maximum mumber of lines the output console should have  
    static const WORD MAX_CONSOLE_LINES = 500;  
    
    void RedirectIOToConsole()  
    {  
    	int hConHandle;  
    	long lStdHandle;  
    	CONSOLE_SCREEN_BUFFER_INFO coninfo;  
    	FILE *fp;  
    	
    	// 分配一个控制台程序  
    	AllocConsole();
    	
    	// 设置足够大的屏幕缓存可以让我们滑动文本
    	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
    
    	// 设置控制台屏幕的高度
    	coninfo.dwSize.Y = MAX_CONSOLE_LINES;
    
    	// 设置控制台屏幕缓存大小
    	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),  
    		coninfo.dwSize);
    	
    	// 获取标准输出句柄
    	lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    
    	//打开标准输出句柄,类似打开文件的方式如fopen,返回一个文件描述符
    	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    
    	// 以可写的方式打开
    	fp = _fdopen( hConHandle, "w" );
    
    	*stdout = *fp;
    
    	setvbuf( stdout, NULL, _IONBF, 0 );
    
    	// redirect unbuffered STDIN to the console  
    	lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);  
    	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);  
    	
    	fp = _fdopen( hConHandle, "r" );  
    	*stdin = *fp;  
    	setvbuf( stdin, NULL, _IONBF, 0 );  
    	
    	// redirect unbuffered STDERR to the console  
    	lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);  
    	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);  
    	
    	fp = _fdopen( hConHandle, "w" );  
    	*stderr = *fp;  
    	setvbuf( stderr, NULL, _IONBF, 0 );  
    	
    	// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog  
    	// point to console as well  
    	ios::sync_with_stdio();  
    }
    
    void test()  
    {  
    	int iVar;  
    	RedirectIOToConsole();  
    	// test stdio  
    	fprintf(stdout, "Test output to stdout\n");  
    	fprintf(stderr, "Test output to stderr\n");  
    	fprintf(stdout, "Enter an integer to test stdin: ");  
    	scanf("%d", &iVar);  
    	printf("You entered %d\n", iVar);  
    	//test iostreams  
    	cout << "Test output to cout" << endl;  
    	cerr << "Test output to cerr" << endl;  
    	clog << "Test output to clog" << endl;  
    	cout << "Enter an integer to test cin: ";  
    	cin >> iVar;  
    	cout << "You entered " << iVar << endl;  
    }  
    
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	// TODO: Place code here.
    	test();
    
    	getch();
    	return 0;
    }
    

      

  • 相关阅读:
    小程序文件的作用域
    微信小程序 Image 图片实现宽度100%,高度自适应
    微信小程序中wx.setStorageSync与wx.setStorage的区别
    微信小程序常用组件
    小程序的目录结构及四种文件类型
    【转载】Java程序设计入门 (四)
    【转载】WEB工作原理简述
    【转载】持久层框架 Apache Cayenne 推介
    【转载】面向对象起步 封装、继承、多态
    【转载】Java程序设计入门 (三)
  • 原文地址:https://www.cnblogs.com/kernel0815/p/2512268.html
Copyright © 2011-2022 走看看