我放了一个opendialog组件,写了同样一段程序,在xp环境下会起作用,会有提示‘不是文本文件’,在win7下就不会提示。
procedure TForm10.ToolButton1Click(Sender: TObject);
var
  fname:string;
begin
  with OpenDialog1 do
  begin
    Filter:='文本文件 .txt';
    DefaultExt:= 'ini';
    FileName:= '';
    Options:= [ofHideReadOnly,ofFileMustExist,ofPathMustExist] ;
    if Execute then
    begin
      if ofExtensionDifferent in Options then
        MessageDlg('这不是文本文件',mtWarning,[mbYes],0)
      else
      begin
        RichEdit1.Lines.LoadFromFile(OpenDialog1.FileName);
        fname:= OpenDialog1.FileName;
        StatusBar1.Panels[3].Text:= fname;
      end;
    end;
  end;
end;1、问题是ofExtensionDifferent in Options不可能为true,因为前面options已经赋值了,不包含ofExtensionDifferent这个枚举,无法理解。2、为什么在win7这个条件ofExtensionDifferent in Options就始终是false,求教育。

解决方案 »

  1.   

    先是给options赋值
    Options:= [ofHideReadOnly,ofFileMustExist,ofPathMustExist] 
    然后判断
    ofExtensionDifferent in Options
    明明Options里面没有ofExtensionDifferent,执行起来(在xp下)却是true,为什么呢?
      

  2.   

    设置断点查看了opendialog1.Options的值,在xp和win7下确实不一样,是因为api不一样了吗?
      

  3.   


        .....
        TOpenDialog.Execute里的代码,后面修改了FOptions的值,
        Result := TaskModalDialog(Func, OpenFileName);
        if Result then
        begin
          GetFileNames(OpenFilename);
          if (Flags and OFN_EXTENSIONDIFFERENT) <> 0 then
            Include(FOptions, ofExtensionDifferent) else  //这里
            Exclude(FOptions, ofExtensionDifferent); //这里