Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   Delphi5 MessageDlg window behind the main form (http://www.delphipages.com/forum/showthread.php?t=217803)

rmt 09-17-2018 06:41 AM

Delphi5 MessageDlg window behind the main form
 
Dear Expert,

In my delpi5 application, I call MessageDlg Window. The Dialog pops up behind the main Form and locks the program as a result. Ho do I get the Dialog Window to appear on top of the form. The code is as below:

HTML Code:

var
  DialogFormHandle: HWnd;

DialogFormHandle := Handle;

WITH SaveDialog1 DO
                        BEGIN
                                BringToFront;
                                Filter:='*.xls';
                                if Execute then
                                BEGIN
                                        vfileName:=SaveDialog1.FileName;
                                        vDir:=ExtractFileDir(vfileName);
                                        vfileName:=ChangeFileExt(vfileName, '');
                                END;   
                        END;

if DirectoryExists(vDir) then
              begin
                  if FileExists(vfileName+'.xls') then
                  begin

                        if MessageDlg('Do you want to overwrite existing file?',mtConfirmation,[mbYes,mbNo],0)=mrYes then

                        begin
                                DeleteFile(vfileName);
                        end
                        ELSE ABORT;
                  end;
              end;
 DialogFormHandle := 0;

Regards,

Norrit 09-17-2018 07:36 AM

Most likely you have the calling form as AlwaysOnTop, or you do something with DialogFormHandle...

And in your code, if you call Abert the DialogFormHandle will NOT be 0. So there's a big difference if the you cancel the request or not.
Not sure what you do with this handle, seems like your code is stripped

rmt 09-17-2018 07:44 AM

Dear Norrit,

The property [formStyle] of the form is [fsNormal].

The actual code is as below:

var
DialogFormHandle: HWnd;

HTML Code:

procedure TformPreviewReport.DataExportToExcel(cdsReport: TClientDataset);
var
          oExcel: TExcelApplication;
          WS : _Worksheet;
          row,col,i,rowCnt,rowCntIndex: integer;
          vfileName,vFieldName,vExcelPos,FileName,vDir: String;
j,k:integer;
begin
    rowCnt:=cdsReport.RecordCount;
    rowCntIndex:=0;
          FileName:=fReportName;

       
          try
              Screen.Cursor:=crHourGlass;
              DialogFormHandle := Handle;
             
                        WITH SaveDialog1 DO
                        BEGIN
                                BringToFront;
                                Filter:='*.xls';
                                if Execute then
                                BEGIN
                                        vfileName:=SaveDialog1.FileName;
                                        vDir:=ExtractFileDir(vfileName);
                                        vfileName:=ChangeFileExt(vfileName, '');
                                END;   
                        END;
             
              if DirectoryExists(vDir) then
              begin
                  if FileExists(vfileName+'.xls') then
                  begin

                        if MessageDlg('Do you want to overwrite existing file?',mtConfirmation,[mbYes,mbNo],0)=mrYes then

                        begin
                                DeleteFile(vfileName);
                        end
                        ELSE ABORT;
                  end;
              end;
           
          finally
          Screen.Cursor:=crDefault;
          DialogFormHandle := 0;
          end;


    Screen.Cursor:=crHourGlass;
    oExcel := TExcelApplication.Create(Self);
    oExcel.DisplayAlerts[0] := False;
    oExcel.Connect;
    oExcel.Workbooks.Add(NULL, 0);
    WS := oExcel.WorkBooks[1].Worksheets[1] as _Worksheet;

    WS.Cells.Item[1,1]:=fReportHeader;

    col:=0;
    with cdsReport do
    begin
        While col<Fields.Count do
        begin
                vFieldName:=Fields[col].FieldName;
                vFieldName:=StringReplace(vFieldName,'_',' ',[rfReplaceAll]);
             
                WS.Cells.Item[2,col+1]:=vFieldName;
                vExcelPos:=GetExcelPosition(col)+'2';


                if (cdsReport.Fields[col].DataType = ftDateTime) then
                begin
                        WS.Range[vExcelPos,vExcelPos].EntireColumn.ColumnWidth:=16;
                       
                                WS.Range[vExcelPos,vExcelPos].EntireColumn.NumberFormat:=DateMask;

                        WS.Range[vExcelPos,vExcelPos].EntireColumn.HorizontalAlignment:=xlright;
                end
                else if (cdsReport.Fields[col].DataType = ftFloat) then
                begin
                        WS.Range[vExcelPos,vExcelPos].EntireColumn.ColumnWidth:=16;
                      WS.Range[vExcelPos,vExcelPos].EntireColumn.NumberFormat:='#,##0.00';//'#,##0';
                end
                else if (cdsReport.Fields[col].DataType = ftInteger) then
                begin
                        WS.Range[vExcelPos,vExcelPos].EntireColumn.ColumnWidth:=16;
                      WS.Range[vExcelPos,vExcelPos].EntireColumn.NumberFormat:='0';
                end
                else
                begin
                        if length(cdsReport.Fields[col].DisplayName)>
cdsReport.Fields[col].Size then
                                WS.Range[vExcelPos,vExcelPos].EntireColumn.ColumnWidth:=length(cdsReport.Fields[col].DisplayName)
                        else
                                WS.Range[vExcelPos,vExcelPos].EntireColumn.ColumnWidth:=cdsReport.Fields[col].Size;
                      WS.Range[vExcelPos,vExcelPos].EntireColumn.NumberFormat:='@';
                end;
                col:=col+1;
        end;


        WS.Range['A1',vExcelPos].EntireRow.Cells.Font.Bold:=True;
        WS.Range['A1',vExcelPos].EntireRow.Interior.Color:=clSilver;
        rowcnt:=rowcnt+2;
        vExcelPos:=GetExcelPosition(col)+inttostr(rowcnt);


        WS.Range['A'+inttostr(rowcnt),vExcelPos].EntireRow.Cells.Font.Bold:=True;


        row:=3;
        k:=cdsreport.recordcount;
        j:=1;

        First;
        //while not eof do
        while j <=k  do
        begin
                rowCntIndex:=rowCntIndex+1;
                col:=0;
                while col<Fields.Count do
                begin

                        if Length(Trim(cdsReport.Fields[col].AsString))>
0 then
                        begin
                                if (cdsReport.Fields[col].DataType = ftDateTime) then
                                        WS.Cells.Item[row,col+1]:=Trim(FormatDateTime(DateMask,Fields[col].AsDateTime))                               
                                else
                                        WS.Cells.Item[row,col+1]:=Fields[col].AsString;
                        end
                        else
                                WS.Cells.Item[row,col+1]:=Fields[col].AsString;
                        col:=col+1;
                end;
                Next;
                row:=row+1;
                j:=j+1;
        end;
    end;

    vfileName:=vfileName+'.xls' ;
    oExcel.ActiveWorkbook.SaveCopyAs(vfileName,0);
    oExcel.Disconnect;
    oExcel.Quit;
    oExcel := Nil;
    Screen.Cursor:=crDefault;
    MessageDlg('Export Completed. Please check '+vfileName,mtInformation,[mbOk],0);
        Close;
end;


Norrit 09-17-2018 09:17 AM

And what are you doing with DialogFormHandle?
It's something global, what is it used for, and where (some other thread perhaps?)

Because executing this code should produce the dialog in front, can you reproduce this in a new application with a simplified version of this code?

Furthermore, code handling when Execute is false is not good, but that's not part of your problem. This will result in uninitialized vFileName and the report is still being generated but cannot be saved. And setting cursor to crHourGlass when just showing some dialogs (save and message) is somewhat trivial, but wouldn't recommend it.

rmt 09-17-2018 09:37 AM

Dear Norrit,

Actually this form is called from another form. Kindly suggest where i went wrong.

Regards,

kolbasz 09-17-2018 10:12 AM

Perhaps you should implement your own MessageDlg with fsStayOnTop FormStyle, like this:
Code:

function MyMessageDlg(const AMsg: string; ADlgType: TMsgDlgType;
  AButtons: TMsgDlgButtons; AParent: TForm): TModalResult;
var
  MsgFrm: TForm;
begin
  MsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  try
    MsgFrm.Position := poDefaultSizeOnly;
    MsgFrm.FormStyle := fsStayOnTop;
    MsgFrm.Left := AParent.Left + (AParent.Width - MsgFrm.Width) div 2;
    MsgFrm.Top := AParent.Top + (AParent.Height - MsgFrm.Height) div 2;
    Result := MsgFrm.ShowModal;
  finally
    MsgFrm.Free
  end;
end;

Then call it whenever is necessary:
Code:

MyMessageDlg('This is just a test', mtInformation, [mbOk], YourForm);

Norrit 09-17-2018 11:50 AM

It's hard to tell where you go wrong.
As mentioned, build a small application in which you can reproduce this and give us that.

Here is still code missing and I'm guessing that it's in the missing part where it goes wrong. You haven't answered my questions either...


All times are GMT. The time now is 04:12 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.