zoukankan      html  css  js  c++  java
  • MFC下调用控制台和控制台下MFC库的支持

    1.MFC下调用控制台

    在CWinApp的InitInstance中对话框的DoModal之前加入

    1 AllocConsole();                                          // 开辟控制台
    2 SetConsoleTitle(_T("测试窗口"));                  // 设置控制台窗口标题
    3 freopen("CONOUT$","w",stdout);                // 重定向输出
    4 freopen( "CONIN$", "r+t", stdin );              // 申请读

    在CWinApp的ExitInstance中加入

    1 FreeConsole();//释放控制台

    由于直接关闭控制台会出错,所以禁用关闭。

    在对话框的OnInitDialog中

    1 char   szBuf[100]; 
    2 GetConsoleTitle(szBuf,   100);                 //得到控制台标题
    3 HWND   hwnd   =   ::FindWindow(NULL,   szBuf);      //查找控制台 
    4 HMENU   hmenu   =   ::GetSystemMenu(hwnd,   FALSE);   //获取菜单
    5 ::RemoveMenu(hmenu,   SC_CLOSE,MF_BYCOMMAND);      //移除关闭

    下面是向控制台写的方法,两种:

    (1)直接printf或cout

    1 printf("Hello World!
    "); // 写数据


    (2)

    1 HANDLE   outPut; 
    2 outPut   =   GetStdHandle(STD_OUTPUT_HANDLE); 
    4 WriteConsole(outPut,"Hello World!
    ", strlen("Hello World!
    "),NULL,NULL);

    2.如何在控制台程序下添加MFC库的支持
    1.单击菜单栏的项目->属性->配置属性

    2.在右边MFC的使用中选择“在共享 DLL 中使用 MFC”

    3.新建一个stdafx.h头文件,并在里面加入如下代码

     1 #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit
     2  
     3  #ifndef VC_EXTRALEAN
     4  #define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers
     5  #endif
     6  
     7  #include <afx.h>
     8  #include <afxwin.h>         // MFC core and standard components
     9  #include <afxext.h>         // MFC extensions
    10  #ifndef _AFX_NO_OLE_SUPPORT
    11  #include <afxdtctl.h>           // MFC support for Internet Explorer 4 Common Controls
    12  #endif
    13  #ifndef _AFX_NO_AFXCMN_SUPPORT
    14  #include <afxcmn.h>                     // MFC support for Windows Common Controls
    15  #endif // _AFX_NO_AFXCMN_SUPPORT
    16  
    17  #include <iostream>


    4.在cpp文件中包含该头文件#include "stdafx.h"

    5.实例:

     1 #include "stdafx.h"
     2  using namespace std;
     3  int main(void)
     4  {
     5      CString str("Hello World");
     6      AfxMessageBox(str);
     7      USES_CONVERSION;
     8      char * pstr = T2A(str);
     9      cout << pstr;
    10  }
  • 相关阅读:
    google glog 使用方法
    LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
    c++ ‘nullptr’ 在此作用域中尚未声明
    Impala 使用的端口
    忽略“Signal: SIGSEGV (Segmentation fault)”
    查看python脚本的运行pid,让python脚本后台运行
    阿里云主机运行速度慢的解决办法
    在Git.oschina.net中配置TortoiseGit使用sshkey,无需输入账号和密码
    抓取国家的学校编码数据
    CAS统一登录认证好文汇集贴
  • 原文地址:https://www.cnblogs.com/wind-net/p/3153971.html
Copyright © 2011-2022 走看看