背景:
由於下位機做的一些東西,總是需要通過上位機來驗證,以及為了直觀給客戶展示下位機的功能,所以時常需要編寫一些簡單的APP。今天就以VC++6.0為例,簡單的記錄下該如何快速的創建一個APP。
正文:
首先,本次工程文件名為“GPIOTest”,按照MFC向導一路往下,使用Dialog base窗口,結果會生成三個分類文件夾“Source files”、“Head files”、“Resource files”。具體存放什么就不用說了。
其中會生成三個“.cpp”文件,“GPIOTest.cpp”、“GPIOTestDlg.cpp”、“StdAfx.cpp”
運行順序是:
最開始在“GPIOTest.cpp”中:CGPIOTestApp::CGPIOTestApp() --> CGPIOTestApp::InitInstance() -->
對於InitInstance()這個函數有必要貼出來說明下,代碼如下:
BOOL CSBC7111GPIO_APPApp::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 CSBC7111GPIO_APPDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); /* 運行到這里的時候,就在DoModal()這個函數里執行循環,響應 * 各種消息,譬如移動窗體時,調用OnPaint()函數;點擊菜單欄時, * 調用SysCommand()函數等等,直到,點擊了“X”,或者關閉事件, * 即會退出DoModal()循環,返回一個參數。即以下函數所寫。 */ if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
接着進入到“GPIOTestDlg.cpp”文件中的函數:
CGPIOTestAppDlg::CGPIOTestAppDlg(CWnd* pParent /*=NULL*/): CDialog(CGPIOTestAppDlg::IDD, pParent) --> CGPIOTestAppDlg::DoDataExchange(CDataExchange* pDX) --> ::OnInitDialog()。
之后即可以在OnInitDialog()函數內添加自己的代碼了。一般都是對界面的一些控件初始化,由於現在還未使用到類似QT的信號槽之類的功能,所以暫時不做記錄。
另外粗略說明以下兩個函數的作用:
CGPIOTestAppDlg::OnPaint(),顧名思義,即當窗口有變化時會調用該函數,譬如放大縮小,移動等等;
CGPIOTestAppDlg::::OnSysCommand(UINT nID, LPARAM lParam),該函數一般在用戶使用鼠標點擊菜單欄時會使用。
記錄地點:深圳WZ
記錄時間:2016年3月3日