:: My another contributions in Delphi

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


»Komunitas Delphi Indonesia
starting as a member

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


Saturday, November 18, 2006

:: How To Use Macro In Delphi's IDE









See the picture first

Question/Problem/Abstract:

How to make such changes [see above picture] efficiently n quicker
* Left picture : before quote formatting
* Right picture : after use macro to give quote formatting

Answer:
You can do that only using Macro
see my previous posting about Macro Shortcut In Delphi
and then follow below steps :

1. Move your text cursor into "SELECT" line
2. Activate the Recording Macro by pressing [Ctrl]+[Shift]+[r]
3. Press [Home] button in order to move the cursor to the beggining of that line
4. Press [Ctrl]+[Right Arrow] button
5. Press ['] { give opening quote you deserve }
6. Press [End] button
7. Press [space] button in order to give trailing space for that line
8. Press ['] { give closing quote you deserve }
9. Press [space] button in order to give extra space again for that line
10. Press [+] button to insert plus char at that position
11. Press [Down Arrow] in order to move down the text cursor
12. Deactivate/stop the Recording Macro by pressing [Ctrl]+[Shift]+[r] again
13. Last step is press [Ctrl]+[Shift]+[p] to play/use we've recorded macro for the remaining lines

It's simple isn't it ?
:D

Increase your coding efficiency now !!

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