合并函数以获得更清晰的代码?
不要被长度吓倒......我不认为这是一个太难的问题。
语言: C++
开发环境: Microsoft Visual C++
使用的库: MFC
问题: 我正在创建一个大型首选项对话框有几个“页面”。其中许多要求用户指定文件路径。现在,用户单击按钮,它将跳转到 OnCommand()。这将验证该命令是否来自按钮,然后跳转到 Browse() 函数,在该函数中它将找出按下了哪个按钮。最后,它将调用 FileDialog(),它将启动文件选择器,然后返回文件路径,将其分配给正确的变量,并将其附加到正确的编辑控件。
我试图将所有这些“打开文件”按钮合并到一个类或函数中,但我不确定实现它的最佳方法。我希望它是干净的,这样我就不必向它提供特定的 ID(2001、2002,...)。
现在,这三个函数(如下)位于我的每个文件中......这是混乱且不必要的。我想要一个名为 OpenFile.cpp 的文件或包含处理打开文件所需函数的文件,以及将所选路径附加到正确对话框中的正确文本框。< /em>
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_cast<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;
}
}
头文件定义:
afx_msg CString FileDialog(CWnd* wnd, int uiID);
afx_msg void Browse(UINT uiID);
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
那么我如何调整参数将它们全部放入一个文件中,以及如何引用它们?如果我这样做,我觉得我需要向 OnCommand() 函数添加一个额外的参数,但我认为我不能这样做。
预先感谢您的帮助!
〜乔恩
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_cast<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用 CMFCEditBrowseCtrl 或 COXBrowseFileEdit (或其他类似的类)而不是普通的编辑控件?
这些类创建带有“浏览”按钮的编辑框,单击该按钮时,会自动打开文件选择对话框并将所选文件设置为编辑控制文本。
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.