MFC程序带参数运行实例



      鱼老大需要让两子程序带参数运行,于是仔细找找了,原来MFC中自带有一个成员变量m_lpCmdLine保存有参数。以下是自己学习的心得,其中包含MFC源码介绍及DEMO:

 

      1、该参数在MFC源码中的位置如下,打开AFXWIN.H,可以找到:

 

 

[cpp]  view plain  copy
 print ?
  1. class CWinApp : public CWinThread  
  2. {  
  3.     DECLARE_DYNAMIC(CWinApp)  
  4. public:  
  5.   
  6. // Constructor  
  7.     CWinApp(LPCTSTR lpszAppName = NULL);     // app name defaults to EXE name  
  8.   
  9. // Attributes  
  10.     // Startup args (do not change)  
  11.     HINSTANCE m_hInstance;  
  12.     HINSTANCE m_hPrevInstance;  
  13.     LPTSTR m_lpCmdLine;  
  14.     int m_nCmdShow;  
  15.   
  16.     // Running args (can be changed in InitInstance)  
  17.     LPCTSTR m_pszAppName;  // human readable name  
  18.                                 //  (from constructor or AFX_IDS_APP_TITLE)  
  19.     LPCTSTR m_pszRegistryKey;   // used for registry entries  
  20.     CDocManager* m_pDocManager;  
  21.   
  22.     // Support for Shift+F1 help mode.  
  23.     BOOL m_bHelpMode;           // are we in Shift+F1 mode?  
  24.   
  25.     ......  
  26. }  

 

      2、新建一个MFC对话框程序,其中下面两个函数中添加的源代码如下:

 

[cpp]  view plain  copy
 print ?
  1. BOOL CB1App::InitInstance()  
  2. {  
  3.     AfxEnableControlContainer();  
  4.   
  5.     // Standard initialization  
  6.     // If you are not using these features and wish to reduce the size  
  7.     //  of your final executable, you should remove from the following  
  8.     //  the specific initialization routines you do not need.  
  9.   
  10. #ifdef _AFXDLL  
  11.     Enable3dControls();         // Call this when using MFC in a shared DLL  
  12. #else  
  13.     Enable3dControlsStatic();   // Call this when linking to MFC statically  
  14. #endif  
  15.   
  16.     if((m_lpCmdLine[0] == _T('/0')) || (lstrcmp(m_lpCmdLine, _T("b1")) != 0))     
  17.     {     
  18.         m_bCmdRet=TRUE;  
  19.     }   
  20.     else  
  21.         m_bCmdRet=FALSE;  
  22.   
  23.     ......  
  24. }  

 

 

[cpp]  view plain  copy
 print ?
  1. /////////////////////////////////////////////////////////////////////////////  
  2. // CB1Dlg message handlers  
  3. BOOL CB1Dlg::OnInitDialog()  
  4. {  
  5.     CDialog::OnInitDialog();  
  6.   
  7.     // 判断参数是否正确,否则退出  
  8.     // 参数在App类初始函数中  
  9.     if(m_bCmdRet)  
  10.     {  
  11.         //MessageBox("参数错误!");  
  12.         EndDialog(0);  
  13.     }  
  14.     // Add "About..." menu item to system menu.  
  15.   
  16.     // IDM_ABOUTBOX must be in the system command range.  
  17.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  18.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  19.   
  20.     ......  
  21. }  

 

       3、程序运行截图:

 

       MFC运行参数 "b1.exe b1"

 

       4、总结,再次深入了解MFC这个框架部分结构。(PS:有人说MFC已经过时了,有人说MFC是一坨屎,,,,,,当然不同的领域、不同的需求当然说法也是人云亦云了,正如“小马过河”的典故,自己脚下的路才是真实!)

 

 

       5、VC源码及DEMO下载地址:

 

      鱼老大需要让两子程序带参数运行,于是仔细找找了,原来MFC中自带有一个成员变量m_lpCmdLine保存有参数。以下是自己学习的心得,其中包含MFC源码介绍及DEMO:

 

      1、该参数在MFC源码中的位置如下,打开AFXWIN.H,可以找到:

 

 

[cpp]  view plain  copy
 print ?
  1. class CWinApp : public CWinThread  
  2. {  
  3.     DECLARE_DYNAMIC(CWinApp)  
  4. public:  
  5.   
  6. // Constructor  
  7.     CWinApp(LPCTSTR lpszAppName = NULL);     // app name defaults to EXE name  
  8.   
  9. // Attributes  
  10.     // Startup args (do not change)  
  11.     HINSTANCE m_hInstance;  
  12.     HINSTANCE m_hPrevInstance;  
  13.     LPTSTR m_lpCmdLine;  
  14.     int m_nCmdShow;  
  15.   
  16.     // Running args (can be changed in InitInstance)  
  17.     LPCTSTR m_pszAppName;  // human readable name  
  18.                                 //  (from constructor or AFX_IDS_APP_TITLE)  
  19.     LPCTSTR m_pszRegistryKey;   // used for registry entries  
  20.     CDocManager* m_pDocManager;  
  21.   
  22.     // Support for Shift+F1 help mode.  
  23.     BOOL m_bHelpMode;           // are we in Shift+F1 mode?  
  24.   
  25.     ......  
  26. }  

 

      2、新建一个MFC对话框程序,其中下面两个函数中添加的源代码如下:

 

[cpp]  view plain  copy
 print ?
  1. BOOL CB1App::InitInstance()  
  2. {  
  3.     AfxEnableControlContainer();  
  4.   
  5.     // Standard initialization  
  6.     // If you are not using these features and wish to reduce the size  
  7.     //  of your final executable, you should remove from the following  
  8.     //  the specific initialization routines you do not need.  
  9.   
  10. #ifdef _AFXDLL  
  11.     Enable3dControls();         // Call this when using MFC in a shared DLL  
  12. #else  
  13.     Enable3dControlsStatic();   // Call this when linking to MFC statically  
  14. #endif  
  15.   
  16.     if((m_lpCmdLine[0] == _T('/0')) || (lstrcmp(m_lpCmdLine, _T("b1")) != 0))     
  17.     {     
  18.         m_bCmdRet=TRUE;  
  19.     }   
  20.     else  
  21.         m_bCmdRet=FALSE;  
  22.   
  23.     ......  
  24. }  

 

 

[cpp]  view plain  copy
 print ?
  1. /////////////////////////////////////////////////////////////////////////////  
  2. // CB1Dlg message handlers  
  3. BOOL CB1Dlg::OnInitDialog()  
  4. {  
  5.     CDialog::OnInitDialog();  
  6.   
  7.     // 判断参数是否正确,否则退出  
  8.     // 参数在App类初始函数中  
  9.     if(m_bCmdRet)  
  10.     {  
  11.         //MessageBox("参数错误!");  
  12.         EndDialog(0);  
  13.     }  
  14.     // Add "About..." menu item to system menu.  
  15.   
  16.     // IDM_ABOUTBOX must be in the system command range.  
  17.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  18.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  19.   
  20.     ......  
  21. }  

 

       3、程序运行截图:

 

       MFC运行参数 "b1.exe b1"

 

       4、总结,再次深入了解MFC这个框架部分结构。(PS:有人说MFC已经过时了,有人说MFC是一坨屎,,,,,,当然不同的领域、不同的需求当然说法也是人云亦云了,正如“小马过河”的典故,自己脚下的路才是真实!)

 

 

       5、VC源码及DEMO下载地址:

 

       http://download.csdn.net/source/1536508


http://blog.csdn.net/wangningyu/article/details/4399039

   转自:   http://download.csdn.net/source/1536508

      鱼老大需要让两子程序带参数运行,于是仔细找找了,原来MFC中自带有一个成员变量m_lpCmdLine保存有参数。以下是自己学习的心得,其中包含MFC源码介绍及DEMO:

 

      1、该参数在MFC源码中的位置如下,打开AFXWIN.H,可以找到:

 

 

[cpp]  view plain  copy
 print ?
  1. class CWinApp : public CWinThread  
  2. {  
  3.     DECLARE_DYNAMIC(CWinApp)  
  4. public:  
  5.   
  6. // Constructor  
  7.     CWinApp(LPCTSTR lpszAppName = NULL);     // app name defaults to EXE name  
  8.   
  9. // Attributes  
  10.     // Startup args (do not change)  
  11.     HINSTANCE m_hInstance;  
  12.     HINSTANCE m_hPrevInstance;  
  13.     LPTSTR m_lpCmdLine;  
  14.     int m_nCmdShow;  
  15.   
  16.     // Running args (can be changed in InitInstance)  
  17.     LPCTSTR m_pszAppName;  // human readable name  
  18.                                 //  (from constructor or AFX_IDS_APP_TITLE)  
  19.     LPCTSTR m_pszRegistryKey;   // used for registry entries  
  20.     CDocManager* m_pDocManager;  
  21.   
  22.     // Support for Shift+F1 help mode.  
  23.     BOOL m_bHelpMode;           // are we in Shift+F1 mode?  
  24.   
  25.     ......  
  26. }  

 

      2、新建一个MFC对话框程序,其中下面两个函数中添加的源代码如下:

 

[cpp]  view plain  copy
 print ?
  1. BOOL CB1App::InitInstance()  
  2. {  
  3.     AfxEnableControlContainer();  
  4.   
  5.     // Standard initialization  
  6.     // If you are not using these features and wish to reduce the size  
  7.     //  of your final executable, you should remove from the following  
  8.     //  the specific initialization routines you do not need.  
  9.   
  10. #ifdef _AFXDLL  
  11.     Enable3dControls();         // Call this when using MFC in a shared DLL  
  12. #else  
  13.     Enable3dControlsStatic();   // Call this when linking to MFC statically  
  14. #endif  
  15.   
  16.     if((m_lpCmdLine[0] == _T('/0')) || (lstrcmp(m_lpCmdLine, _T("b1")) != 0))     
  17.     {     
  18.         m_bCmdRet=TRUE;  
  19.     }   
  20.     else  
  21.         m_bCmdRet=FALSE;  
  22.   
  23.     ......  
  24. }  

 

 

[cpp]  view plain  copy
 print ?
  1. /////////////////////////////////////////////////////////////////////////////  
  2. // CB1Dlg message handlers  
  3. BOOL CB1Dlg::OnInitDialog()  
  4. {  
  5.     CDialog::OnInitDialog();  
  6.   
  7.     // 判断参数是否正确,否则退出  
  8.     // 参数在App类初始函数中  
  9.     if(m_bCmdRet)  
  10.     {  
  11.         //MessageBox("参数错误!");  
  12.         EndDialog(0);  
  13.     }  
  14.     // Add "About..." menu item to system menu.  
  15.   
  16.     // IDM_ABOUTBOX must be in the system command range.  
  17.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  18.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  19.   
  20.     ......  
  21. }  

 

       3、程序运行截图:

 

       MFC运行参数 "b1.exe b1"

 

       4、总结,再次深入了解MFC这个框架部分结构。(PS:有人说MFC已经过时了,有人说MFC是一坨屎,,,,,,当然不同的领域、不同的需求当然说法也是人云亦云了,正如“小马过河”的典故,自己脚下的路才是真实!)

 

 

       5、VC源码及DEMO下载地址:

 

       http://download.csdn.net/source/1536508

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值