调用这个函数怎么都不能显示出一个对话框,代码参照Charles Petzold的Programming Windows写的如下:FileInitialize在MainWndProc中WM_CREATE时调用
FileOpenDlg在“打开...”时调用。传入参数为(hwnd, "C:\\", "test.bmp")
调试跟踪进去,变量赋值等都没有错,执行到
    result = GetOpenFileName(&ofn) ;
这一句时,Step in都没有用,直接返回0,没有对话框弹出。查看ErrCode也是0。环境Window XP + SP2,Visual C++ 6(试了一下,在Visual Studio 2008中也同样)
#include <windows.h>
#include <commdlg.h>static OPENFILENAME ofn ;
void FileInitialize (HWND hwnd)
{
    static TCHAR szFilter[] =  TEXT ("BitMap Files (*.bmp)\0*.bmp\0")  \
                                TEXT ("All Files (*.*)\0*.*\0\0") ;
 
    ofn.lStructSize        = sizeof (OPENFILENAME) ;
    ofn.hwndOwner          = hwnd ;
    ofn.hInstance          = NULL ;
    ofn.lpstrFilter        = szFilter ;
    ofn.lpstrCustomFilter  = NULL ;
    ofn.nMaxCustFilter     = 0 ;
    ofn.nFilterIndex       = 0 ;
    ofn.lpstrFile          = NULL ;           // Set in Open and Close functions
    ofn.nMaxFile           = MAX_PATH ;
    ofn.lpstrFileTitle     = NULL ;           // Set in Open and Close functions
    ofn.nMaxFileTitle      = MAX_PATH ;
    ofn.lpstrInitialDir    = NULL ;
    ofn.lpstrTitle         = NULL ;
    ofn.Flags              = 0 ;              // Set in Open and Close functions
    ofn.nFileOffset        = 0 ;
    ofn.nFileExtension     = 0 ;
    ofn.lpstrDefExt        = TEXT ("bmp") ;
    ofn.lCustData          = 0L ;
    ofn.lpfnHook           = NULL ;
    ofn.lpTemplateName     = NULL ;
}BOOL FileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
    BOOL result;
    DWORD ErrCode;
    ofn.hwndOwner          = hwnd ;
    ofn.lpstrFile          = pstrFileName ;
    ofn.lpstrFileTitle     = pstrTitleName ;
    ofn.Flags              = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;

    result = GetOpenFileName(&ofn) ;
    if(!result)
ErrCode = GetLastError();
    return result;
}

解决方案 »

  1.   

    Note   This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.
      

  2.   

    这句话只是对于Windows的移动操作系统而言的吧。
      

  3.   


    ofn.lpstrFile=pstrTitleName;
    ofn.nMaxFile=255;    //不要忘了该句 ofn.lpstrInitialDir=pstrFileName;
      

  4.   

    FileInitialize在MainWndProc中WM_CREATE时调用 
    =================
    你检查一下这个函数所做赋值操作是不是都成功了,比如ofn.hwndOwner= hwnd 这个
      

  5.   

    单步看下,我基本上不用这个,都用CFileDialog
      

  6.   

    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HWND hwnd;              // owner window
    HANDLE hf;              // file handle// Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    //
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    //
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;// Display the Open dialog box. if (GetOpenFileName(&ofn)==TRUE) 
        hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
            0, (LPSECURITY_ATTRIBUTES) NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL);
      

  7.   


    这句我用
    ofn.nMaxFile = MAX_PATH
    写的啊,是260.
      

  8.   

    赋值是成功的,而且在FileOpenDlg调用时赋值也成功
      

  9.   

    ofn.lpstrFile        = NULL ; 这个成员返回文件名,不能是NULL。
      

  10.   

    这只是初始化为NULL
    我在调用FileOpenDlg的时候传入了一个MAX_PATH大小的数组,内容是"C:\\"