-1

I am using OpenDialog in Delphi 5. My problem is it opens at the back of My Application forms. I set My application form at the TOP using following code

if UpperCase(SmSession.ApplicationName) = 'MYAPP' then
begin
  Params.ExStyle := Params.ExStyle or WS_EX_TOPMOST;
  exit;
end

, so the windows dialog box might be at the back. How can I take it to front without changing my application forms code ?

This is my code to openDialog:

EditParent.OpenDlg.InitialDir := EditParent.FDefaultDir;
EditParent.OpenDlg.FileName := EditParent.FFileName;

if EditParent.OpenDlg.Execute then
Begin
  SplitFileDir(EditParent.OpenDlg.FileName, TmpDir, TmpFile);
  if EditParent.ShowOnlyFileName then
    EditParent.FileName := TmpFile
  else
    EditParent.FileName := EditParent.OpenDlg.FileName;
  EditParent.Directory := TmpDir;
  EditParent.SetPeerDirectoryBrowser;
End;
EditParent.OpenDlg.Free;
inherited Click;

end;

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • You need to hack the Delphi 5 dialogs code to use the correct window handle as the dialog's owner. – David Heffernan Jan 16 '17 at 09:19
  • You 'free' OpenDlg? Why do you do that? – Dsm Jan 16 '17 at 09:19
  • `BringWindowToTop(OpenDialog1.Handle);` – Ilyes Jan 16 '17 at 09:27
  • to Free up the dialog – user7424581 Jan 16 '17 at 09:32
  • BringWindowToTop(EditParent.OpenDlg.Handle); I tried this sir but not working. @Sami – user7424581 Jan 16 '17 at 09:35
  • How to do this @David – user7424581 Jan 16 '17 at 09:38
  • You need to arrange that the `hwndOwner` of the `OPENFILENAME` struct is set to the window handle of your always on top main form. Read about owned windows here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599.aspx#owned_windows – David Heffernan Jan 16 '17 at 09:43
  • @Sami This is a Windows file dialog. I don't think that you understand the issues at all here. For a start, when would you call `BringWindowToTop`? The dialog's window handle is not available at any point in the code above. Furthermore, bringing it to the top is one thing, how would you keep it there. Window ownership is what does that. You also should read the link in my comment above. – David Heffernan Jan 16 '17 at 09:46
  • @DavidHeffernan K – Ilyes Jan 16 '17 at 09:50
  • Sir, will you please give a example. I have just started to work on Delphi @DavidHeffernan – user7424581 Jan 16 '17 at 09:59
  • @DavidHeffernan What about `if EditParent.OpenDlg.Execute(Self.Handle) then`? – Ilyes Jan 16 '17 at 10:04
  • @Sami Not available in Delphi 5. In those older versions handling of window ownership was pretty broken in the VCL. They started to improve matters from Delphi 7. – David Heffernan Jan 16 '17 at 10:07
  • @user7424581 It's going to involve hacking the VCL. I don't have Delphi 5 at hand. If I were you I'd just ignore the VCL file dialog classes and call `GetOpenFileName` directly. Or if you don't need to support XP you could go ahead and use the Vista common item dialogs, `IFileDialog` and friends. – David Heffernan Jan 16 '17 at 10:07
  • if EditParent.OpenDlg.Execute(Self.Handle) it gives error too many actual parameter @sami – user7424581 Jan 16 '17 at 10:08
  • Please try to give example. It's all goes bounce... @DavidHeffernan – user7424581 Jan 16 '17 at 10:30
  • 1
    There are plenty of examples out there of how to call `GetOpenFileName`. It's also well documented on MSDN. You just need to call it passing the window handle of the main form as the owner. I haven't got time to write yet another example. Sometimes you don't get an instant solution where somebody else writes the code for you. Sometimes you need to put the hard yards in. I suspect that you knew nothing of window ownership until today. It takes time for it to sink in. Did you read that MSDN link that I gave you. Do you understand why window ownership is the key to the problem? – David Heffernan Jan 16 '17 at 10:36
  • Ok. I will search on GetOpenFileName – user7424581 Jan 16 '17 at 10:40
  • Maybe there are better sets of common dialog components on torry.net / I remember there even was a library dissecting those common dialogs back to a form with components, so you could enhance it adding your own components to it. It worked albeit VERY slow. Then, computers today are faster than in 1999. – Arioch 'The Jan 16 '17 at 13:27
  • @DavidHeffernan after reading above link i tried following SetWindowPos(EditParent.OpenDlg.Handle,HWND_TOPMOST,0,0,0,0,SWP_NOZORDER); but not working. – user7424581 Jan 17 '17 at 04:11
  • also change delphi code: { TComponent } constructor TComponent.Create(AOwner: TComponent); parma:TCreateParams; begin FComponentStyle := [csInheritable]; parma.ExStyle := Parma.ExStyle or WS_EX_TOPMOST; if AOwner <> nil then AOwner.InsertComponent(Self); end; – user7424581 Jan 17 '17 at 05:03
  • { TOpenDialog } constructor TOpenDialog.Create(AOwner: TComponent); parma:TCreateParams; begin inherited Create(AOwner); Parma.ExStyle := Parma.ExStyle or WS_EX_TOPMOST; FHistoryList := TStringList.Create; FOptions := [ofHideReadOnly, ofEnableSizing]; FFiles := TStringList.Create; FFilterIndex := 1; FFileEditStyle := fsEdit; end; – user7424581 Jan 17 '17 at 05:05
  • None of that looks like it will help. You need to get the window ownership set correctly. – David Heffernan Jan 17 '17 at 07:05
  • how to get windows ownership ? please provide any link so that i can refer it. – user7424581 Jan 17 '17 at 08:29
  • I have applied Params.ExStyle := Params.ExStyle or WS_EX_TOPMOST; WS_EX_TOPMOST style to form at the of Form creation. I want to change it,after creation to set it at background. is there any way for this. – user7424581 Jan 18 '17 at 12:36

1 Answers1

-1

This Property I was using for keep my application window on top always. Params.ExStyle := Params.ExStyle or WS_EX_TOPMOST;

now i commented this line and use another following property Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
Params.WndParent := GetDesktopWindow;
it works.

dummzeuch
  • 10,975
  • 4
  • 51
  • 158