• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Showing a form on the Windows taskbar

I'm using the following code to place a child form on the taskbar

procedure TfrmTemplate.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.WndParent := 0;
end;

However whenever a dialog box is shown focus is set to the application main form. When the dialog box is closed focus returns to the child form. Is there any way around this.

Thanks

Jo
0
nnbbb09
Asked:
nnbbb09
  • 3
  • 2
1 Solution
 
Slick812Commented:
What types of "Dialog" boxes do you refer to, if it's a Modal TForm then you can adjust the focus when it closes
0
 
nnbbb09Author Commented:

It's both modal TForms and also windows open/save dialogs.
Its the setting of focus to the application main form that I want to prevent. eg : If the child form has focus and a TOpenDialog is shown I don't want the application main form to come back into view while the dialog is open. This is what is happening at the moment.
0
 
Slick812Commented:
you could try the TForm's BringToFront


procedure TForm2.buton_OpenDiagClick(Sender: TObject);
var
FName: String;
begin
if OpenDialog1.Execute then
FName := OpenDialog1.FileName;
Form2.BringToFront
end;

or try

begin
Form3.ShowModal;
SetFocus(Form2.Handle);
end;
0
 
Slick812Commented:
After I posted my sugestions above, I re-Read your question and saw that I was thinking about something different than your problem. So I tested a couple of things and came up with these. If you use your CreateParams trick for a modal form and set WndParent to active Form like
"Params.WndParent := Form2.Handle;"
then your active form will not go behind the Main Form. However, I could not get a solution to the Open Dialog Box except to create it from Windows API, so here's some code -


Form1 is the "MainForm", Form2 if the Form with a Title bar, and Form# is the Modal Form called from Form2

{For Form2}
  public
    { Public declarations }
    DialogText: String;
    DialogNumber: Integer;
    procedure CreateParams (var Params : TCreateParams); override;
  end;


var
  Form2: TForm2;

implementation

uses newfade3, Commdlg;

{$R *.DFM}

procedure TForm2.CreateParams(var Params: TCreateParams);
begin
 inherited CreateParams(Params);
 Params.WndParent := 0;
end;

function OpenDialog(Open: Boolean; InitialDir: String; var FileName: String) : Boolean;
var
OFName1 : TOpenFilename;
TempFilename : String;

begin
{if you want to change the Open and save Dialog options
then you will need to look up in API Help index "OPENFILENAME"
and set the parameters of OFName1 below}
Setlength(TempFileName,1026);
  with OFName1 do
    begin
    lStructSize := sizeof(OFName1);
    hwndowner := Form2.Handle;
    hInstance := sysInit.hInstance;
    lpstrFile := @TempFileName[1];
    FillChar(lpstrFile^,1026,0);
    nMaxFile := 1024;

   if Open then
   lpstrTitle:='Open a File'
   else lpstrTitle:='Save this File';
   lpstrDefExt := 'txt';
   if not Open then
   StrLCopy(lpstrFile, @FileName[1],Length(FileName)+1);

   lpstrFilter := 'Text file (*.txt)'#0'*.txt'#0'All files (*.*)'#0'*.*'#0#0;
   if (Length(InitialDir) > 2) then
   lpstrInitialDir := @InitialDir[1]
   else
   lpstrInitialDir := 'C:\';

  lpstrCustomFilter := nil;
  nMaxCustFilter := 0;
  nFilterIndex := 0;
  lpstrFileTitle := nil;
  nMaxFileTitle := 0;
  Flags:=OFN_EXPLORER or OFN_LONGNAMES or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or OFN_HIDEREADONLY;
  lCustData := integer(@OFName1);
  lpTemplateName := nil;
  lpfnHook := nil;
  end;
 
  if Open then
  result := GetOpenFileName(OFName1)
  else Result := GetSaveFileName(OFName1);
  if result then
  begin
  FileName := copy(TempFileName, 1, pos(#0, TempFilename) -1);
  end else
  FileName := '';

end;

procedure TForm2.sbut_OpenDiagClick(Sender: TObject);
var
FileName: String;
begin
FileName := 'New1.txt';
{for the OpenDialog a "True" gives a Open and a False gives a Save
The FileName will be returned with the result filename
and if OpenDialog is false then the Dialog Box was canceled}
if OpenDialog(False, 'C:\Stuff', FileName) then
Label1.Caption := FileName;
end;

procedure TForm2.sbut_ShowForm3Click(Sender: TObject);
begin
Application.CreateForm(TForm3, Form3);
Form3.ShowModal;
Label1.Caption := DialogText+IntToStr(DialogNumber);
end;

end.

- - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -  -- - -

{For Form3, the modal form
move Form3 in the menu - Project - Options - Forms, to Availible Forms}

public
    { Public declarations }
    procedure CreateParams (var Params : TCreateParams); override;
  end;

var
  Form3: TForm3;

implementation

{$R *.DFM}

procedure TForm3.CreateParams(var Params: TCreateParams);
begin
 inherited CreateParams(Params);
 Params.WndParent := Form2.Handle;
{set the parent here to the Active Form}
end;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form2.DialogText := Edit1.Text;
Form2.DialogNumber := Edit1.Tag;
{get all the Data from your form here because you will Free it}
Action := caFree;
end;

end.

 - - - - - - - - - - - - - - - - - - - - - - - - -

I did NOT use the Modal Form3 except from From2, if you need to use it from another Form you would have to change the WndParent in the CreateParams, Sorry about the first post
0
 
nnbbb09Author Commented:
Sorry for the delay in accepting.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now