鱼老大需要让两子程序带参数运行,于是仔细找找了,原来MFC中自带有一个成员变量m_lpCmdLine保存有参数。以下是自己学习的心得,其中包含MFC源码介绍及DEMO:
1、该参数在MFC源码中的位置如下,打开AFXWIN.H,可以找到:
class CWinApp : public CWinThread
{
DECLARE_DYNAMIC(CWinApp)
public:
// Constructor
CWinApp(LPCTSTR lpszAppName = NULL); // app name defaults to EXE name
// Attributes
// Startup args (do not change)
HINSTANCE m_hInstance;
HINSTANCE m_hPrevInstance;
LPTSTR m_lpCmdLine;
int m_nCmdShow;
// Running args (can be changed in InitInstance)
LPCTSTR m_pszAppName; // human readable name
// (from constructor or AFX_IDS_APP_TITLE)
LPCTSTR m_pszRegistryKey; // used for registry entries
CDocManager* m_pDocManager;
// Support for Shift+F1 help mode.
BOOL m_bHelpMode; // are we in Shift+F1 mode?
......
}
2、新建一个MFC对话框程序,其中下面两个函数中添加的源代码如下:
BOOL CB1App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
if((m_lpCmdLine[0] == _T('/0')) || (lstrcmp(m_lpCmdLine, _T("b1")) != 0))
{
m_bCmdRet=TRUE;
}
else
m_bCmdRet=FALSE;
......
}
/
// CB1Dlg message handlers
BOOL CB1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 判断参数是否正确,否则退出
// 参数在App类初始函数中
if(m_bCmdRet)
{
//MessageBox("参数错误!");
EndDialog(0);
}
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
......
}
3、程序运行截图:
4、总结,再次深入了解MFC这个框架部分结构。(PS:有人说MFC已经过时了,有人说MFC是一坨屎,,,,,,当然不同的领域、不同的需求当然说法也是人云亦云了,正如“小马过河”的典故,自己脚下的路才是真实!)
5、VC源码及DEMO下载地址: