:: My another contributions in Delphi

»Delphi3000.com
posting and uploading delphi articles
[currently as Top 66 uploader]


»Komunitas Delphi Indonesia
starting as a member

Showing posts with label programming article. Show all posts
Showing posts with label programming article. Show all posts

Saturday, February 03, 2007

Another Way to Draw Tiled Image to a Canvas

:: Counted Tile Image Draw ::

Question/Problem/Abstract:

How we can get a counted tiling image on a canvas ?
counted as TileX (Horizontally = columns) and TileY(Vertically = lines)
Eg: TileX = 3, TileY = 3, so we can get a 3x3 tiled image on a canvas

Answer:

procedure CountedTileImage(const aPicture: TPicture;
const aTileX, aTileY: Integer; destCanvas: TCanvas;
const destWidth, destHeight: Integer);
var x, y, tx, ty: integer;
src, dst: TBitmap;
myRect: TRect;
begin
if (not aPicture.Graphic.Empty) and (aPicture.Graphic <> nil) then
begin
src := TBitmap.Create;
try
src.Assign(aPicture.Graphic);
dst := TBitmap.Create;
try
dst.Width := aTileX * aPicture.Graphic.Width;
dst.Height := aTileY * aPicture.Graphic.Height;
y := 0;
ty := 1;
while (ty <= aTileY) do begin
x := 0;
tx := 1;
while (tx <= aTileX) do begin
myRect := Rect(x, y, x+src.Width, y+src.Height);
dst.Canvas.CopyRect(myRect, src.Canvas, Rect(0, 0, src.Width, src.Height));
x := x + src.Width;
tx := tx + 1;
end;
y := y + src.Height;
ty := ty + 1;
end;
finally
destCanvas.StretchDraw(Rect(0,0,destWidth,destHeight), dst);
dst.Free;
end;
finally
src.Free;
end;
end;
end;


Have a good time

Saturday, November 25, 2006

:: Displaying Month Names in Indonesian or Other Language Using FormatDateTime Function

» FormatDateTime Function

Category:

» DateTime & System

Question/Problem/Abstract:
» How we can display long month names in a specific language, eg Indonesia
So, the result will be like this : today is 25 Nopember 2006

Answer:

Actually, that's quiet simple to do that
You only need to replace the default (Delphi) "LongMonthNames" constants with the preferable any language you desire.

For this example, I want to display a specific date into Indonesian Date Format (dd MMMM yyyy)

Which,
» January is Januari in Indonesian
» February is Februari
» March is Maret
» April is April
» May is Mei
» June is Juni
» July is Juli
» August is Agustus
» September is September (unchanged)
» October is Oktober
» November is Nopember
» December is Desember

The example code would be like this :
-------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;

type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

// English:
// this variable ("namaBulan") is used to replace month names' string
// from English into Indonesian
// this variable is used in "replaceLongMonthNamesWithIndonesian" procedure

// dipakai untuk menggantikan string nama bulan dr bhs Inggris ke Indonesia
// digunakan dalam procedure "replaceLongMonthNamesWithIndonesian"
namaBulan: array[1..12] of string =
( 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli',
'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');

// You can change the string constant above with your language choice

implementation

{$R *.dfm}

procedure replaceLongMonthNamesWithIndonesian;
var i : integer;
begin
for i:=1 to High(namaBulan) do
LongMonthNames[i] := namaBulan[i];
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
replaceLongMonthNamesWithIndonesian;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageFmt(
'The date in Indonesian format (dd mmmm yyyy) : %s',
[FormatDateTime('dd MMMM yyyy', DateTimePicker1.Date)]);
end;

end.

-------------------------------------
// end of the code

if you want to change the short name of a month with some other language either, you need to change the "ShortMonthNames" like we did above


Enjoy it

ps : the original code is posted @delphi3000.com [http://www.delphi3000.com/articles/article_4642.asp] by me either


Thursday, November 23, 2006

:: Simple Way To Give Leading Zero In a Number

» String Format Function

Question/Problem/Abstract:

How can we put the leading zero in a Number with specifix n Max Digit place holder ??
Is there any Delphi built in function to do this job ??

Eg :
» if digit place holder is 5 => 00123
» if digit place holder is 3 => 012

Answer:
Actually, Delphi has supported to do this kind of job.
Yes, we can use function "Format" strings to do this
But we must do a little trick here to use it appropriately

Here is my example code:
********************************

function GiveLeadingZero(const aNumber, aMaxDigit: Integer): String;
var formatSpecifier: String;
begin
formatSpecifier := Format('%%.%dd', [aMaxDigit]);

// formatSpecifier will result like this: '%.5d' if aMaxDigit=5
Result := Format(formatSpecifier, [aNumber]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var aNumber: Integer;
begin
if (TryStrToInt(Edit1.Text, aNumber)) then
Edit2.Text := GiveLeadingZero(aNumber, 5)
else
ShowMessage('Value in Edit1 is not a valid integer value');
end;

********************************

To use those code, simply juzt put two TEdit component on the Form with one button to execute the code



I think it solved the problem.

ps : the original code is posted @delphi3000.com [http://www.delphi3000.com/articles/article_4639.asp?SK=] by me either


Sunday, November 05, 2006

:: Easiest Way Drawing Transparent Image

Question/Problem/Abstract:
How to draw an image transparently ?

Answer:
Here is anohter way to draw a transparent image.
Only using Delphi properties and method (Image).

Here is the example code:


// make draw proc to draw transparently
procedure MyTransparentDraw(src, dest: TBitmap; x, y: integer; warna: TColor);
begin
src.Transparent := true;
src.TransparentMode := tmFixed;
src.TransparentColor := warna;
dest.Canvas.Draw(x, y, src);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
TRANS_COLOR = clYellow; // change with transparent color you want
var bmp, bmp2: TBitmap;
begin
if (OpenPictureDialog1.Execute) then begin
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
bmp := TBitmap.Create;
try
bmp.Width := Image1.Width;
bmp.Height := Image1.Height;
bmp.Assign(Image1.Picture.Bitmap);
bmp2 := TBitmap.Create;
try
bmp2.Width := bmp.Width;
bmp2.Height := bmp.Height;
MyTransparentDraw(bmp, bmp2, 0, 0, TRANS_COLOR);
Image1.Canvas.Draw(0, 0, bmp2);
finally
bmp2.Free;
end;
finally
bmp.Free;
end;
end;
end;

To try above code, just copy and paste those code,
then click on button to choose an image to be drawn transparently.
You can change the value of "TRANS_COLOR" above with any other color that you want to be the transparent color of your image


ps : original source of this article was uploaded by h4ry p @ delphi3000.com
http://www.delphi3000.com/articles/article_3115.asp