Home > OS >  How to remove OPEN AS READ ONLY button feature from the TOpenFileName dialog
How to remove OPEN AS READ ONLY button feature from the TOpenFileName dialog

Time:02-25

When I call this File dialog it displays a nice modern dialog with OPEN OPEN AS READONLY buttons.

How can I remove the OPEN AS READ-ONLY button feature?

OpenSaveFileDialog(editform,'','my|*.my','','Open my',FilSelez,True,False,True,True) 

function OpenSaveFileDialog(      Parent: TWinControl;
                            const DefExt,Filter,InitialDir,Title: string;
                              var FileName: string;
                                  MustExist,OverwritePrompt,NoChangeDir,DoOpen: Boolean): Boolean;
     var ofn: TOpenFileName;
         szFile: array[0..MAX_PATH] of Char;
   begin
         Result := False;
         FillChar(ofn, SizeOf(TOpenFileName), 0);
         with ofn do
           begin
           lStructSize := SizeOf(TOpenFileName);
           hwndOwner := Parent.Handle;
           lpstrFile := szFile;
           nMaxFile := SizeOf(szFile);

           if (Title <> '') then
              lpstrTitle := PChar(Title);

           if (InitialDir <> '') then
              lpstrInitialDir := PChar(InitialDir);

           StrPCopy(lpstrFile, FileName);
           lpstrFilter := PChar(StringReplace(Filter, '|', #0,[rfReplaceAll, rfIgnoreCase]) #0#0);

           if DefExt <> '' then
              lpstrDefExt := PChar(DefExt);
           end;

         if MustExist then
            ofn.Flags := ofn.Flags or OFN_FILEMUSTEXIST;

         if OverwritePrompt then
            ofn.Flags := ofn.Flags or OFN_OVERWRITEPROMPT;

         if NoChangeDir then
            ofn.Flags := ofn.Flags or OFN_NOCHANGEDIR;

         if DoOpen
            then begin
                 if GetOpenFileName(ofn) then
                    begin
                    Result := True;
                    FileName := StrPas(szFile);
                    end;
                 end
            else begin
                 if GetSaveFileName(ofn) then
                    begin
                    Result := True;
                    FileName := StrPas(szFile);
                    end;
                 end;
    end;

CodePudding user response:

Every time you use a new API, you always read its full documentation.

In this case, you go to the docs for the GetOpenFileName function, and you find that it has a single parameter, a structure of type OPENFILENAME. Hence, you go to the documentation for this structure.

At this page, you press Ctrl F in your web browser, and search for "read only" to quickly find this passage:

Flags

A set of bit flags you can use to initialize the dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of the following flags.

[...]

OFN_HIDEREADONLY 0x00000004

Hides the Read Only check box.

Hence, you realise that you only need to add this flag:

ofn.Flags := ofn.Flags or OFN_HIDEREADONLY;
  • Related