Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2018, 06:41 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 280
Default 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,
__________________
Rmt
Reply With Quote
  #2  
Old 09-17-2018, 07:36 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

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
Reply With Quote
  #3  
Old 09-17-2018, 07:44 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 280
Default

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&#91;col&#93;.FieldName;
                vFieldName:=StringReplace(vFieldName,'_',' ',&#91;rfReplaceAll&#93;);
               
                WS.Cells.Item&#91;2,col+1&#93;:=vFieldName;
                vExcelPos:=GetExcelPosition(col)+'2';


                if (cdsReport.Fields&#91;col&#93;.DataType = ftDateTime) then
                begin
                        WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.ColumnWidth:=16;
                        
                                WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.NumberFormat:=DateMask; 

                        WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.HorizontalAlignment:=xlright;
                end
                else if (cdsReport.Fields&#91;col&#93;.DataType = ftFloat) then
                begin
                        WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.ColumnWidth:=16;
                       WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.NumberFormat:='#,##0.00';//'#,##0';
                end
                else if (cdsReport.Fields&#91;col&#93;.DataType = ftInteger) then
                begin
                        WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.ColumnWidth:=16;
                       WS.Range&#91;vExcelPos,vExcelPos&#93;.EntireColumn.NumberFormat:='0';
                end
                else
                begin
                        if length(cdsReport.Fields&#91;col&#93;.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&#91;col&#93;.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;
__________________
Rmt

Last edited by rmt; 09-17-2018 at 07:54 AM.
Reply With Quote
  #4  
Old 09-17-2018, 09:17 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

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.
Reply With Quote
  #5  
Old 09-17-2018, 09:37 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 280
Default

Dear Norrit,

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

Regards,
__________________
Rmt
Reply With Quote
  #6  
Old 09-17-2018, 10:12 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

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);
Reply With Quote
  #7  
Old 09-17-2018, 11:50 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,305
Default

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...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:24 AM.


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