Put text and numbers in boxes - delphi

Using FastReport , how can I put Text and Numbers from database in boxes like :
|_|_|_|_|_|_|_|_|_|_|
So "Sami" it becomes :
and the same for numbers too, I try to do it with TfrxLineView but I fail.

Take the easy way :
Drop 4 TfrxMemoView component in your report (or as you need) like :
In OnPreview event of your report, set your code for example:
procedure TForm1.frxReport1Preview(Sender: TObject);
var Str : WideString; I : Integer; Mem : TfrxMemoView;
begin
Str := 'Sami'; // Or get it from query/table (database)
// Find the TFrxMemoView Component and set in it the String you want
for I := 1 to 4 do
begin
Mem := frxReport1.FindObject('M'+IntToStr(I)) as TfrxMemoView;
Mem.Text := Str[I];
end;
end;
The result will be :
Update :
You can also do it programmatically as :
var RT : TfrxBand;
Mem : array [1..100] of TfrxMemoView ;
i : Byte;
Name : WideString;
begin
// Find the band
RT := frxReport1.FindObject('RT') as TfrxBand;
// Set the String
Name := 'DELPHI FAST REPORT';
for I := 1 to Length(Name) do
begin
Mem[i] := TfrxMemoView.Create(RT);
Mem[i].Text := Name[i];
Mem[i].Font.Style := [fsBold];
Mem[i].Frame.Width := 2;
Mem[i].Height := 20;
Mem[i].AutoWidth := False;
Mem[i].HAlign := haCenter;
Mem[i].Frame.Typ := [ftLeft , ftBottom , ftRight];
Mem[i].Width := 20;
if i =1 then
Mem[i].Left := 0
else
Mem[i].Left := Mem[i-1].Left + 5 + 15;
end;
frxReport1.ShowReport();
end;
The result is :

There is no ready made control for displaying characters in boxes as you ask for. Therefore you need to paint this yourself on the canvas you choose.
Here's an example of how to do it in a TPaintBox, pbText is here a string field of the demo form, and holds the text to be displayed in the paint box:
procedure TForm17.PaintBox1Paint(Sender: TObject);var
i, n, x, y: integer;
siz: TSize;
pb: TPaintBox;
begin
n := 10; // character cells
pb := Sender as TPaintBox;
siz := pb.Canvas.TextExtent('Wp');
// draw character cells
x := 4; y := siz.cy+2;
for i := 0 to n do
begin
pb.Canvas.MoveTo(i * siz.cx + x, 0);
pb.Canvas.LineTo(i * siz.cx + x, y);
end;
pb.Canvas.MoveTo(x, y);
pb.Canvas.LineTo(n * siz.cx + 4, y);
// draw characters horizontally in center of box
for i := 1 to Length(pbText) do
begin
x := (4 + (i-1)*siz.cx + (siz.cx - pb.Canvas.TextWidth(pbText[i])) div 2);
y := 0;
pb.Canvas.TextOut(x, y, UpperCase(pbText[i])); // force upcase
// pb.Canvas.TextOut(x, y, pbText[i]); // or don't
end;
end;
And to use it
procedure TForm17.Button1Click(Sender: TObject);
begin
pbText := 'Sami Wiim';
PaintBox1.Invalidate;
end;

Very simple and and a little ugly method to do this.
In Fast Report:
-place a Text object like this : [TEST_STR];
-set text style to Underline;
2.In Delphi
-make function to convert a string to formated string.
For example :
input: SAMI
output: | S | A | M | I |
-in OnGetValue event of FastReport call this function:
procedure TMainForm.frxReport1GetValue(const VarName: string;
var Value: Variant);
begin
if VarName = 'TEST_STR' then Value := MyFunctionToFormatStr('SAMI');
end;
That's all.
The result look like :

Related

Delphi How To Convert String To Binary Using Only Pascal [duplicate]

This question already has answers here:
Converting decimal/integer to binary - how and why it works the way it does?
(6 answers)
Closed 4 years ago.
I have done some Example to convert a string to binary but i couldn't find a way to walk on each character in the string and complete the whole calculations process and then step to the next character in the string, Here is my code:
var i,j, rest, results :integer;
restResult : string;
begin
results := 1;
for i := 1 to length(stringValue) do
begin
while (results > 0) do
begin
results := ord(stringValue[i]) div 2;
rest := ord(stringValue[i]) mod 2;
restResult := restResult + inttostr(rest);
end;
end;
// Get The Rests Backwards
for i := length(restResult) downto 1 do
begin
result := result + restResult[i];
end;
The application always get into infinite loop, any suggestions?
Your results := ord(stringValue[i]) div 2; remains the same, because stringValue[i] does not change, so while loop is infinite.
To solve this mistake:
for i := 1 to length(stringValue) do
begin
t := ord(stringValue[i]);
repeat
restResult := restResult + inttostr(t mod 2);
t := t div 2;
until t = 0;
end;
But note that you cannot divide resulting string into pieces for distinct chars, because length of binary representation will vary depending on char itself.
This is example of code with fixed length for representation of char (here AnsiChar):
function AnsiStringToBinaryString(const s: AnsiString): String;
const
SBits: array[0..1] of string = ('0', '1');
var
i, k, t: Integer;
schar: string;
begin
Result := '';
for i := 1 to Length(s) do begin
t := Ord(s[i]);
schar := '';
for k := 1 to 8 * SizeOf(AnsiChar) do begin
schar := SBits[t mod 2] + schar;
t := t div 2
end;
Result := Result + schar;
end;
end;
'#A z': (division bars are mine)
01000000|01000001|00100000|01111010
# A space z

Read all section .ini values into stringgrid

I use Delphi XE7. Is there any way to load all values from an .ini file into a stringgrid in different coloumns?
My .ini file looks like
[1038]
AValue = a1
BValue = b1
CValue = c1
DValue = d1
[1031]
AValue = a2
BValue = b2
CValue = c2
DValue = d2
I use this procedure for filling the grid:
procedure TForm1.ReadIntoGrid(const aIniFileName, aSection: string;
const aGrid: TStringGrid);
var
Ini: TIniFile;
SL: TStringList;
i: Integer;
begin
SL := TStringList.Create;
try
Ini := TIniFile.Create(aIniFileName);
try
aGrid.ColCount := 2;
Ini.ReadSectionValues(aSection, SL);
aGrid.RowCount := SL.Count;
for i := 0 to SL.Count - 1 do
begin
aGrid.Cells[0,i] := SL.Names[i];
aGrid.Cells[1,i] := SL.ValueFromIndex[i];
end;
finally
Ini.Free;
end;
finally
SL.Free;
end;
end;
It works fine, I get this:
My question is...
How can I read all section values (1038 and 1031) into the grid next to the 1038 values? Values will be fixed all time.
To give you some ideas:
First, i think you should add one paramater to your procedure:
procedure TForm1.ReadIntoGrid(const aIniFileName, aSection: string;
const aGrid: TStringGrid; const aColumn: Integer = 1);
Second, rewrite this part of your method :
for i := 0 to SL.Count - 1 do
begin
aGrid.Cells[0,i] := SL.Names[i];
aGrid.Cells[1,i] := SL.ValueFromIndex[i];
end;
replace with
for i := 0 to SL.Count - 1 do
begin
aGrid.Cells[0,i] := SL.Names[i];
aGrid.Cells[aColumn,i] := SL.ValueFromIndex[i];
end;
ps: Obviously you dont nead to rewrite the value into the first column.
So now assume you are calling the method like this:
ReadIntoGrid('MyIniFile.ini','1038', MyGrid, 1);
ReadIntoGrid('MyIniFile.ini','1031', MyGrid, 2);

Exporting DBgrid to CSV?

I have a DB grid which is sorted (the user clicked a few radio buttons and checkboxes to influence the display).
I would like to export all of the data (not just what is visible in the grid), sorted identically, to CSV - how do I do so? The data - not the user settings, just to clarify.
Thanks in advance for any help
[Update] I build sqlQuery bit by bit, depending on the user's settings of checkboxes & radio groups, then, when one of them changes, I
ActivityADQuery.SQL.Clear();
ActivityADQuery.SQL.Add(sqlQuery);
ActivityADQuery.Open(sqlQuery);
That is to say that there isn't a hard coded query, it varies and I want to export the current settings.
I don't know enough if I want to export from the grid or the dataset (I am just not a db guy, this is my first DBgrid), but I suspect that I want the grid, because it has a subset of fields of he dataset.
I guess that TJvDBGridCSVExport is a Jedi component(?) I have tried to avoid them so far, great as they sound, because I prefer discreet, stand-alone, components to installing a huge collection. That may not be the cleverest thing to do, but it's how I feel - ymmv (and prolly does)
Another solution, works also with (multi)selected rows:
procedure TReportsForm.ExportToCSV(const aGrid : TDBGrid; const FileName : String);
Var
I, J : Integer;
SavePlace : TBookmark;
Table : TStrings;
HeadTable : String;
LineTable : String;
First : Boolean;
Begin
HeadTable := '';
LineTable := '';
Table := TStringList.Create;
First := True;
Try
For I := 0 To Pred(aGrid.Columns.Count) Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
// Use the text from the grid, in case it has been set programatically
// E.g., we prefer to show "Date/time" than "from_unixtime(activity.time_stamp, "%D %b %Y %l:%i:%S")"
// HeadTable := HeadTable + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ','; // Previous separated wth semi-colon, not comma! (global)
First := False;
End
Else
begin
// HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ',';
end;
Delete(HeadTable, Length(HeadTable), 1); // Remove the superfluous trailing comma
Table.Add(HeadTable);
First := True;
// with selection of rows
If aGrid.SelectedRows.Count > 0 Then
Begin
For i := 0 To aGrid.SelectedRows.Count - 1 Do
Begin
aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
For j := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[J].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[J].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[J].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
First := True;
End;
End
Else
//no selection
Begin
SavePlace := aGrid.DataSource.Dataset.GetBookmark;
aGrid.DataSource.Dataset.First;
Try
While Not aGrid.DataSource.Dataset.Eof Do
Begin
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[I].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[I].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
aGrid.DataSource.Dataset.Next;
First := True;
End;
aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
Finally
aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
End;
End;
Table.SaveToFile(FileName);
Finally
Table.Free;
End;
End; // ExportToCSV()
You could use a own tiny procedure wich could be adapted to your needs
Procedure Dataset2SeparatedFile(ads: TDataset; const fn: String; const Separator: String = ';');
var
sl: TStringList;
s: String;
i: Integer;
bm: TBookmark;
Procedure ClipIt;
begin
s := Copy(s, 1, Length(s) - Length(Separator));
sl.Add(s);
s := '';
end;
Function FixIt(const s: String): String;
begin
// maybe changed
Result := StringReplace(StringReplace(StringReplace(s, Separator, '', [rfReplaceAll]), #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
// additional changes could be Quoting Strings
end;
begin
sl := TStringList.Create;
try
s := '';
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayLabel) + Separator;
end;
ClipIt;
bm := ads.GetBookmark;
ads.DisableControls;
try
ads.First;
while not ads.Eof do
begin
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayText) + Separator;
end;
ClipIt;
ads.Next;
end;
ads.GotoBookmark(bm);
finally
ads.EnableControls;
ads.FreeBookmark(bm);
end;
sl.SaveToFile(fn);
finally
sl.Free;
end;
end;

Add dropshadow to a TBitmap

I have a routine that takes screenshots (TBitmap), I need to add drop-shadow to the final TBitmap/image, I have this code (which used to work but...) something isn't right, the drop-shadow simply isn't drawn:
// --------------------------------------------------------------------- //
procedure TakeScreenshot();
var
lCapRect : TRect;
DestBitmap : TBitmap;
begin
// Take the screenshot & assign it to DestBitmap
// ...
// Add the drop shadow to DestBitmap
DestBitmap.Width := DestBitmap.Width + 6;
DestBitmap.Height := DestBitmap.Height + 6;
PaintShadow(DestBitmap.Canvas, lCapRect);
end;
// --------------------------------------------------------------------- //
procedure PaintShadow(ACanvas : TCanvas; ARect : TRect);
var
AColor : TColor;
i, iMax : Integer;
h1, h2, v1, v2 : Integer;
begin
AColor := ACanvas.Brush.Color;
iMax := 6;
h1 := ARect.Left;
h2 := ARect.Right;
v1 := ARect.Top;
v2 := ARect.Bottom;
with ACanvas do
begin
for i := iMax downto 0 do
begin
ACanvas.Pen.Mode := pmMask;
Pen.Color := DarkenColorBy(AColor, ((iMax - i) * 4 + 10));
MoveTo(h1 + 4{i}, v2 + i);
LineTo(h2 + i + 1, v2 + i);
end; // for
for i := iMax downto 0 do
begin
ACanvas.Pen.Mode := pmMask;
Pen.Color := DarkenColorBy(AColor, ((iMax - i) * 4 + 10));
MoveTo(h2 + i, v1 + 4{i});
LineTo(h2 + i, v2 + i);
end; // for
end; // with
end;
// --------------------------------------------------------------------- //
function Max(const A, B: Integer): Integer;
begin
if (A > B) then
Result := A
else
Result := B;
end;
// --------------------------------------------------------------------- //
function DarkenColorBy(BaseColor : TColor; Amount : Integer) : TColor;
begin
Result := RGB(Max(GetRValue(ColorToRGB(BaseColor)) - Amount, 0),
Max(GetGValue(ColorToRGB(BaseColor)) - Amount, 0),
Max(GetBValue(ColorToRGB(BaseColor)) - Amount, 0));
end;
My question is: How can I fix this (OR anyone know a simple way to add dropshadow to a TBitmap)?
The final image is meant to be saved as bmp/jpg, not shown in a TImage, so I really need to add drop shadow to the image itself.
PS. I'm using Delphi 7 Pro, my app is restricted to Windows XP or later
EDIT
lCapRect depends on the settings (whether I'm capturing the active monitor, window or all the desktop monitors), but let's say it's calculated this way:
lCapRect.Right := Screen.DesktopLeft + Screen.DesktopWidth;
lCapRect.Bottom := Screen.DesktopTop + Screen.DesktopHeight;
lCapRect.Left := Screen.DesktopLeft;
lCapRect.Top := Screen.DesktopTop;
The bitmap does contain the screenshot (+ 6 pixels added to the bottom & right sides to make room for the dropshadow), it's just that the drop shadow drawing doesn't happen
You haven't shown how you are calculating lCapRect. For not drawing off the bitmap regarding your PaintShadow procedure, it has to be smaller than the bitmap, example:
lCapRect := DestBitmap.Canvas.ClipRect;
// Add the drop shadow to DestBitmap
DestBitmap.Width := DestBitmap.Width + 6;
DestBitmap.Height := DestBitmap.Height + 6;
PaintShadow(DestBitmap.Canvas, lCapRect);

Word blocks in TMemo

I am trying to make a basic Hex viewer out of a TMemo, I know this is probably not ideal but it will be only me personally using it so that does not really matter.
(1)
Firstly, suppose a Memo is filled with Hex information like so:
How could I get a count of all the text blocks shown, ignoring the white space? So using the image the result in this case would be 28.
This is what I tried and I know it is completely wrong as I am accessing the Memo lines but I don't know how to access each character.
I cant seem to solve this simple problem :(
function CountWordBlocks(Memo: TMemo): Integer;
var
i: Integer;
vCount: Integer;
begin
for i := 0 to Memo.Lines.Count - 1 do
begin
if Length(Memo.Lines.Strings[i]) = 2 then
begin
Inc(vCount);
end;
end;
Result := vCount;
end;
Here is the code I am using to display the Hex values in the Memo:
procedure ReadFileAsHex(const AFileName: string; ADestination: TStrings);
var
fs: TFileStream;
buff: Byte;
linecount: Byte;
line: string;
begin
linecount := 0;
line := '';
fs := TFileStream.Create(AFileName, fmOpenRead);
try
ADestination.BeginUpdate;
try
while fs.Position < fs.Size do
begin
fs.Read(buff, 1);
line := line + IntToHex(buff, 2) + ' ';
Inc(linecount);
if linecount = 16 then
begin
ADestination.Add(line);
line := '';
linecount := 0;
end;
end;
if Length(line) <> 0 then
ADestination.Add(line);
finally
ADestination.EndUpdate;
end;
finally
fs.Free;
end;
end;
(2)
If I click onto the Memo and a text block is under the cursor, how could I know which number the selected block is out of all the others?
So using the same first image, the caret is at the top line next to 68, so the result would be 3 as it is the third text block out of 28.
This should be so easy but I cannot think clearly, I don't have the right programming mind yet and so really struggle with basic logic and solving problems!
(3)
Finally I would like to select a block at runtime by passing a block number value. I tried this without much success:
procedure FindBlock(Memo: TMemo; BlockNumber: Integer);
var
i: Integer;
txt: string;
ThisWhite, PrevWhite: boolean;
vRead: Integer;
begin
txt := Memo.Text;
vRead:= 0;
PrevWhite := True;
for i := 1 to Length(txt) do
begin
ThisWhite := Character.IsWhiteSpace(txt[i]);
if PrevWhite and not ThisWhite then
begin
Inc(vRead);
PrevWhite := False;
end;
PrevWhite := ThisWhite;
if vRead = BlockNumber then
begin
Memo.SelStart := vRead;
Memo.SetFocus;
Exit;
end;
end;
end;
(1)
This works:
function TForm1.CountBlocks: integer;
var
i: Integer;
txt: string;
ThisWhite, PrevWhite: boolean;
begin
txt := Memo1.Text;
result:= 0;
PrevWhite := true;
for i := 1 to Length(txt) do
begin
ThisWhite := Character.IsWhiteSpace(txt[i]);
if PrevWhite and not ThisWhite then
begin
inc(result);
PrevWhite := false;
end;
PrevWhite := ThisWhite;
end;
end;
However, it can be optimized if more detailed information about the memo contents is available. For instance, if you know that each line consists of four blocks, then the number of blocks is simply 4*Memo1.Lines.Count. My code above will even accept blocks of different width.
(2)
Simply replace
for i := 1 to Length(txt) do
by
for i := 1 to Memo1.SelStart + 1 do
Since you are in control of the formatting of your lines, and the lines have a fixed format, it is very easy to calculate the number of bytes being displayed without resorting to looping through the individual lines one a time. Every line displays 3 characters per byte, and every line other than the last line displays 16 bytes, thus 48 characters per complete 16-byte line. Use those facts to your advantage to calculate the number of bytes based on the number of complete 16-byte lines present, and then you can add on the number of remaining bytes from just the last line:
function CountWordBlocks(Memo: TMemo): Integer;
var
Count: Integer;
begin
Count := Memo.Lines.Count;
if Count > 0 then
Result := (16 * (Count-1)) + (Length(Memo.Lines[Count-1]) div 3);
else
Result := 0;
end;
You can do something similar to translate a character offset within the Memo into a work block number:
function GetCurrentWordBlock(Memo: TMemo): Integer;
var
SelStart, LineStart, LineNum: Integer
begin
Result := 0;
SelStart := Memo.SelStart;
if SelStart < 0 then Exit;
LineStart := Memo.Perform(EM_LINEINDEX, SelStart, 0);
if LineStart < 0 then Exit;
LineNum := Memo.Perform(EM_LINEFROMCHAR, LineStart, 0);
Result := (16 * LineNum) + ((SelStart - LineStart) div 3) + 1;
end;
To select a given block number, you can do this:
procedure FindBlock(Memo: TMemo; BlockNumber: Integer);
var
LineNum, LineStart: Integer;
begin
if BlockNumber < 1 then Exit;
LineNum = (BlockNumber - 1) div 16;
LineStart = Memo.Perform(EM_LINEINDEX, LineNum, 0);
if LineStart < 0 then Exit;
Memo.SelStart = LineStart + (((BlockNumber - 1) - (16 * LineNum)) * 3);
Memo.SelLength := 2;
Memo.SetFocus;
end;

Resources