Board index » delphi » Q: LongInt(TRect) typecast alternative

Q: LongInt(TRect) typecast alternative

I want to convert window co-ordinates to a single longinteger. In
other words, one might keep the Form1.left, .right., .top and .bottom
in a TRect record.

I want to save these in the inifile, but I don't want to save 4 lines,
one for each corner. The reason is this slows down the load-up speed,
and there are many, many windows.

So this is what I want to do:

     MyInifile.writeinteger('Window Position','Window1',    
                        LongInt(Window1Rect))

But of course Delphi does not allow the typecase TRect to Longint.

I am using Delphi 16-bit. I am looking for a neat way to convert the
coordinates to a single figure and back.

Email appreciated. I respond to every email.
Matthew

 

Re:Q: LongInt(TRect) typecast alternative


Okay guys, I got a solution to my 2 posts. There are two functions:

1. GetStringFromWindowCoordinates() retrieves the co-ordinates
    into a string format 'Left,Top,Height,Width'.

2. SetWindowCoordinatesFromString() sets any window to the
    co-ordinates using the above string format.

This can then be stored with a single line of code in the ini file.

Saving in ini file:

   MyIniFile.WriteString('Window Position',Window1,
                         GetStringFromWindowCoordinates(MyForm1)  )

Restoring from ini file:

   SetWindowCoordinatesFromString ( MyForm1,

           MyIniFile.ReadString('Window Position',Window1,
ConstDefaultWinPos1String  )
              )

Who can improve on this? Code is below.

Email appreciated. I respond to every email.
Matthew

Function GetStringFromWindowCoordinates(FormItem:TForm):string;
begin
   Result:= inttostr(FormItem.left)+','+
            inttostr(FormItem.top)+','+
            inttostr(FormItem.height)+','+
            inttostr(FormItem.width);
end;

Procedure SetWindowCoordinatesFromString(FormItem:TForm;
               StringItem:String);
var RestStr,LeftStr, TopStr, HeightStr,WidthStr: string;
    Delim1, Delim2, Delim3: integer;
begin
   RestStr:=StringItem;

   Delim1:=pos(',',RestStr);
   LeftStr:=copy(RestStr,1,Delim1-1);
   RestStr:=copy(RestStr,Delim1+1,100);

   Delim2:=pos(',',RestStr);
   TopStr:=copy(RestStr,1,Delim2-1);
   RestStr:=copy(RestStr,Delim2+1,100);

   Delim3:=pos(',',RestStr);
   HeightStr:=copy(RestStr,1,Delim3-1);

   WidthStr:=copy(RestStr,Delim3+1,100);

   FormItem.left:=  strtoint(LeftStr);
   FormItem.top:=   strtoint(TopStr);
   FormItem.height:=strtoint(HeightStr);
   FormItem.width:= strtoint(WidthStr);

end;

Quote
Com...@lottery.powernet.co.uk  (Matthew) wrote:
>I want to convert window co-ordinates to a single longinteger. In
>other words, one might keep the Form1.left, .right., .top and .bottom
>in a TRect record.
>I want to save these in the inifile, but I don't want to save 4 lines,
>one for each corner. The reason is this slows down the load-up speed,
>and there are many, many windows.
>So this is what I want to do:
>     MyInifile.writeinteger('Window Position','Window1',    
>                        LongInt(Window1Rect))
>But of course Delphi does not allow the typecase TRect to Longint.
>I am using Delphi 16-bit. I am looking for a neat way to convert the
>coordinates to a single figure and back.
>Email appreciated. I respond to every email.
>Matthew

Other Threads