在用CFileDialog时,需要有一个结构OPENFILENAME,其中有一向就是lstrFilter,其实就是对话框打开时,对文件的过滤,可是不知怎么弄,如只想留下.txt文件,应该怎么写?MSDN是这么说的:
lpstrFilter 
Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters. 
The first string in each pair is a display string that describes the filter (for example, "Text Files"), and the second string specifies the filter pattern (for example, "*.TXT"). To specify multiple filter patterns for a single display string, use a semicolon to separate the patterns (for example, "*.TXT;*.DOC;*.BAK"). A pattern string can be a combination of valid file name characters and the asterisk (*) wildcard character. Do not include spaces in the pattern string. The system does not change the order of the filters. It displays them in the File Types combo box in the order specified in lpstrFilter. 我不知道这个buffer如何定义。新手,请指教亚!If lpstrFilter is NULL, the dialog box does not display any filters. 

解决方案 »

  1.   

    ok,好了,多谢!不过我不明白,下面两种写法有什么不同:
      CString filter="Txt(*.txt)\0*.txt\0";
       .lpstrFilter=filter;   .lpstrFilter="Txt(*.txt)\0*.txt\0";还请指教!
      

  2.   

    .lpstrFilter="Txt(*.txt)\0*.txt\0";//字符储存在data segment,一直存在。CString filter="Txt(*.txt)\0*.txt\0
       .lpstrFilter=filter; ";//CString调用new为字符串赋值,字符储存在堆中,当filter失效时,字符所在的内存被释放,造成lpstrFilter失效。所以最好使用上面的那种方法
      

  3.   

    .lpstrFilter="TXT(*.txt)\0*.txt\0\0"
    结束用2个\0,其它分隔全是一个\0
      

  4.   

    .lpstrFilter="TXT(*.txt)\0*.txt\0\0"画蛇添足,一个\0就够了,"会自动添加一个\0
      

  5.   

    LPCTSTR lpszFilter = TEXT("TXT(*.txt)|*.txt||");CFileDialog dlgTest(TRUE,lpszFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,lpszFilter,NULL);

    if(dlgTest.DoModal()==IDOK)
    {
    //点OK的处理
    }
      

  6.   

    楼上nkwesley(江南丝竹)是比较传统的写法^_^