我知道这是一个简单的问题,但是我已经习惯了使用Borland和包装器,因此这对我来说是一种新方法。有人可以简单地告诉我如何打开仅从Visual Studio C++控制台应用程序获取.obj文件的OpenDialog吗?

非常感谢!

最佳答案

除了入口点(“GUI”应用程序中的WinMain)外,控制台应用程序和GUI应用程序之间实际上没有任何区别,并且如果不从控制台启动,则控制台应用程序将在启动期间打开控制台窗口。

所有Win32 API均可用,因此您需要使用GetOpenFileName调用,如下所示:

OPENFILENAME    ofn;
char *FilterSpec ="Object Files(*.obj)\0*.obj\0Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0";
char *Title ="Open....";
char szFileName[MAX_PATH];
char szFileTitle[MAX_PATH];
int             Result;

*szFileName = 0;
*szFileTitle = 0;

/* fill in non-variant fields of OPENFILENAME struct. */
ofn.lStructSize       = sizeof(OPENFILENAME);
ofn.hwndOwner         = GetFocus();
ofn.lpstrFilter       = FilterSpec;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter    = 0;
ofn.nFilterIndex      = 0;
ofn.lpstrFile         = szFileName;
ofn.nMaxFile          = MAX_PATH;
ofn.lpstrInitialDir   = "."; // Initial directory.
ofn.lpstrFileTitle    = szFileTitle;
ofn.nMaxFileTitle     = MAX_PATH;
ofn.lpstrTitle        = Title;
ofn.lpstrDefExt   = default_extension;

ofn.Flags             = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;

if (!GetOpenFileName ((LPOPENFILENAME)&ofn))
{
    return (-1); // Failed or cancelled
}
else
{
    this->filename.Set(szFileName);
}

10-05 18:13