Consolidating functions for cleaner code?
Don't be turned off by the length...I don't think it's too difficult of a problem.
Language: C++
Development Environment: Microsoft Visual C++
Libraries Used: MFC
Problem: I am creating a large preference dialog with several "pages". Many of them require the user to specify a file path. Right now, the user will click on the button, and it will jump to OnCommand(). This will verify that the command was from a button, then jump to the Browse() function where it will figure out which button was pressed. Finally, it will call FileDialog() which will launch a file chooser, then return the file path, assign it to the correct variable, and append it to the correct edit control.
I am trying to consolidate all of these "open file" buttons into one class or function, but I'm not sure the best way to approach it. I would love for it to be clean so that I don't have to feed it specific IDs (2001, 2002, ...).
Right now, these three functions (below) are in each of my files...this is messy and unnecessary. I want to have a single file called OpenFile.cpp or something that contains the necessary functions to handle opening a file, and appending the chosen path to the correct text box within the correct dialog.
BOOL FSC_3DPersp::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (HIWORD(wParam) == BN_CLICKED)
{
Browse(LOWORD(wParam));
return TRUE;
}
return CWnd::OnCommand(wParam, lParam);
}
//
CString OpenFile::FileDialog(CWnd* wnd, int uiID) // dialog from which the call came and the ID of the edit control where the path is going
{
CFileDialog dlg(
TRUE // Open = TRUE, Save = FALSE
, NULL //filename extension
, "" // initial filename
, OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST // flags
, "" // filter
, wnd // parent window
);
if (dlg.DoModal() == IDOK)
{
CEdit *Display;
CString path = dlg.GetPathName();
Display = reinterpret_cas开发者_如何学Got<CEdit *>(GetDlgItem(uiID));
Display->SetWindowText((LPCTSTR)path);
return path;
}
}
//
void FSC_3DPersp::Browse(UINT uiID)
{
switch(uiID)
{
case IDC_BUTTON1:
m_strPersTexture = FileDialog(this, 2004);
break;
case IDC_BUTTON2:
m_strSkyFront = FileDialog(this, 2005);
break;
case IDC_BUTTON3:
m_strSkyRight = FileDialog(this, 2006);
break;
case IDC_BUTTON4:
m_strSkyBack = FileDialog(this, 2007);
break;
case IDC_BUTTON5:
m_strSkyTop = FileDialog(this, 2008);
break;
case IDC_BUTTON6:
m_strSkyLeft = FileDialog(this, 2009);
break;
case IDC_BUTTON7:
m_strSkyBottom = FileDialog(this, 2010);
break;
}
}
Header File Definitions:
afx_msg CString FileDialog(CWnd* wnd, int uiID);
afx_msg void Browse(UINT uiID);
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
So how can I adjust the parameters to put all three of them into one file, and how would I reference them? If I did this, I feel like I need to add an additional parameter to the OnCommand() function, but I don't think I can do that.
Thank you in advance for the help!
~ Jon
How about using CMFCEditBrowseCtrl or COXBrowseFileEdit (or other similar classes) instead of normal edit controls?
These classes create edit boxes with a "browse" button which, when clicked, automatically opens a file selection dialog and set the selected file as the edit control text.
精彩评论