Sunday, February 28, 2010

Form Inheritance (01) – Base Form

Dalam pembuatan program di Delphi, banyak cara dan style orang mengcoding dan me-manage “project”, salah satunya saya. Dan saya juga punya style sendiri untuk coding dan me-manage “project” dari aplikasi yang saya buat ini. Salah satu style yang pasti saya bikin di dalam pembuatan aplikasi adalah pembuatan Form Dasar (Base Form) untuk di inheritance, inheritance ini di perlukan karena ada behavior standard dari form yang memang saya pasti perlukan, diantaranya adalah sbb:
 

* punya variable Last Key (inget jaman bahoela di clipper)
* punya standard Navigasi keys untuk perpindahan tombol menggunakan tombol [enter], [key-up], [key-down]. Standard Navigasi keys ini-pun harus ber-behave benar terhadap control {ComboBox}, {ListBox}, {DateEdit}, dan {DBGrid}.
    * [ESC] adalah tombol standard untuk keluar dari Forms.
    * dan so-pasti jika Forms di CLOSE, otomatis harus langsung di FREE, heh.. biar ndak coding lagi “action := caFree”…., bosen.

penamaan untuk form saya buat dengan style saya yaitu:
* Base Form ex: Nama File “wfBase”, Nama Object “TfBase” Var “fBase”

* Apps Form ex: Nama File “afCust”, Nama Object “TfCust” Var “fCust”

      di bawah ini adalah minimal code yang saya butuhkan, tapi bila ada penambahan Control selain dari bawaan Delphi, ya… harus di cari padanan “Base Class” ex: ComboBox-nya dari Developer Express atau Raize atau LMD agar behaviornya bisa berjalan dengan semestinya (ex: jika combo sudah drop down harus fasilitas panah atas dan bawah mati).

      demikian semoga membantu….

      nb. untuk form inheritance harap melihat ke help Delphi ya…. :-)
    unit wfBase;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics,
      Controls, Forms, Dialogs, StdCtrls, ExtCtrls, DBCtrls,
      Grids;

    type
    TfBase = class(TForm)
      procedure FormClose( Sender: TObject;
        var Action: TCloseAction);
      procedure FBKeyDown( Sender: TObject;
        var Key: Word; Shift: TShiftState);
      procedure FBKeyPress(Sender: TObject; var Key: Char);
      protected
      procedure WndProc(var Message: TMessage);    override;
      procedure DoClose(var Action: TCloseAction); override;
      { Private declarations }
    public
      LastKey:  Integer;
      KeyMsg:   TMessage;
      { Public declarations }
    end;

    var
      fBase: TfBase;

    implementation

    {$R *.dfm}
    //——————————————————————————
    procedure TfBase.FormClose(Sender: TObject;
      var Action: TCloseAction);
    begin
      Action:= caFree;
    end;
    //——————————————————————————
    procedure TfBase.FBKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    Var
      sClass: string;
    begin
      if not (Shift = []) then exit;
      if Screen.ActiveControl = Nil then Exit;
        sClass := Screen.ActiveControl.ClassName;
      if Screen.ActiveControl is TCustomCombo then begin
        if TCustomCombo(Screen.ActiveControl).DroppedDown
          then Exit;
      end;
      if Screen.ActiveControl is TDBLookupComboBox then begin
        if TDBLookupComboBox(Screen.ActiveControl).ListVisible
          then Exit;
      end;
      if Screen.ActiveControl is TCustomGrid
        then exit; //Base Class for Delphi.Grid
      Case Key of
        VK_DOWN: begin
          SelectNext(Screen.ActiveControl, True, True );
          Key := 0;
        end;
        VK_UP: begin
          SelectNext(Screen.ActiveControl, False, True );
          Key := 0;
        end;
      End;
    end;
    //——————————————————————————
    procedure TfBase.FBKeyPress(Sender: TObject; var Key: Char);
    begin
      if Key=#27 then close;
      if Key<>#13 then exit;
      if Screen.ActiveControl = Nil then exit;
      if Screen.ActiveControl is TCustomCombo then begin
        if TCustomCombo(Screen.ActiveControl).DroppedDown
          then Exit;
      end;
      if Screen.ActiveControl is TCustomGrid
        then exit; //Base Class for Delphi.Grid

      SelectNext(Screen.ActiveControl, True, True );
      Key:= #0;
    end;
    //——————————————————————————
    procedure Tfbase.WndProc(var Message: TMessage);
    begin
      if Message.Msg=45102 then begin
        LastKey:= Message.WParam;
        KeyMsg := Message;
      end;
      inherited;
    end;
    //——————————————————————————
    procedure TfBase.DoClose(var Action: TCloseAction);
    begin
      Action:= caFree;
    end;
    //——————————————————————————
    end.

Thursday, February 18, 2010

How to Display Menu Item Hints

Download Source
When a mouse is over a component (a TButton, for example) if ShowHint property is True and there is some text in the Hint property, the hint / tooltip window will be displayed for the component.
Hints for Menu Items?
By (Windows) design, even if you set the value for the Hint property to a Menu item, the popup hint will not get displayed.
However, the Windows Start Menu items do display hints, and the Favorites menu in the Internet Explorer also displays menu item hints.

It is quite common to use the OnHint event of the global Application variable, in Delphi applications, to display menu item (long) hints in a status bar.

Windows do not expose the messages needed to support a traditional OnMouseEnter event. However, the WM_MENUSELECT message is sent when the user selects a menu item.

The WM_MENUSELECT implementation of the TCustomForm (ancestor of the TForm) sets the menu item hint into Application.Hint that can be used in the Application.OnHint event.

If you want to add menu item popup hints (tooltips) to your Delphi application menus you *only* need to handle the WM_MenuSelect message properly.
The TMenuItemHint class - popup hints for menu items!
Since you cannot rely on the Application.ActivateHint method to display the hint window for menu items (as menu handling is completely done by Windows), to get the hint window displayed you must create your own version of the hint window - by deriving a new class from the THintWindow.

Here's how to create a TMenuItemHint class - a hint widow that actually gets displayed for menu items!

Note: download the full source to explore it more easily.

First, you need to handle the WM_MENUSELECT Windows message:

    type
       TForm1 = class(TForm)
       ...
       private
         procedure WMMenuSelect(var Msg: TWMMenuSelect) ; message WM_MENUSELECT;
       end
    ...
    implementation
    ...
    procedure TForm1.WMMenuSelect(var Msg: TWMMenuSelect) ;
    var
       menuItem : TMenuItem;
       hSubMenu : HMENU;
    begin
       inherited; // from TCustomForm (so that Application.Hint is assigned)

       menuItem := nil;
       if (Msg.MenuFlag <> $FFFF) or (Msg.IDItem <> 0) then
       begin
         if Msg.MenuFlag and MF_POPUP = MF_POPUP then
         begin
           hSubMenu := GetSubMenu(Msg.Menu, Msg.IDItem) ;
           menuItem := Self.Menu.FindItem(hSubMenu, fkHandle) ;
         end
         else
         begin
           menuItem := Self.Menu.FindItem(Msg.IDItem, fkCommand) ;
         end;
       end;

       miHint.DoActivateHint(menuItem) ;
    end; (*WMMenuSelect*)

Quick info: the WM_MENUSELECT message is sent to a menu's owner window (Form1 !) when the user selects (not clicks!) a menu item. Using the FindItem method of the TMenu class, you can get the menu item currently selected. Parameters of the FindItem function relate to the properties of the message received. Once we know what menu item the mouse is over, we call the DoActivateHint method of the TMenuItemHint class. Note: the miHint variable is defined as "var miHint : TMenuItemHint" and is created in the Form's OnCreate event handler.

Now, what's left is the implementation of the TMenuItemHint class.

Here's the interface part:

    TMenuItemHint = class(THintWindow)
    private
       activeMenuItem : TMenuItem;
       showTimer : TTimer;
       hideTimer : TTimer;
       procedure HideTime(Sender : TObject) ;
       procedure ShowTime(Sender : TObject) ;
    public
       constructor Create(AOwner : TComponent) ; override;
       procedure DoActivateHint(menuItem : TMenuItem) ;
       destructor Destroy; override;
    end;

You can find the full implementation in the sample project.

Basically, the DoActivateHint function calls the ActivateHint method of the THintWindow using the TMenuItem's Hint property (if it is assigned).
The showTimer is used to ensure that the HintPause (of the Application) elapses before the hint is displayed. The hideTimer uses Application.HintHidePause to hide the hint window after a specified interval.
When would you use Menu Item Hints?
While some might say that it is not a good design to display hints for menu items, there are situations where actually displaying menu item hints is much better than using a status bar. A most recently used (MRU) menu item list is one such case. A custom task bar menu is another.

Wednesday, February 3, 2010

Fungsi Delphi Terbilang

Fungsi terbilang,



Source dapat di-Download


function SayIt(sValue: string):string;
const
Angka : array [1..20] of string =
('', 'satu', 'dua', 'tiga', 'empat',
'lima', 'enam', 'tujuh', 'delapan',
'sembilan', 'sepuluh', 'sebelas',
'duabelas', 'tigabelas', 'empatbelas',
'limabelas', 'enambelas', 'tujuhbelas',
'delapanbelas', 'sembilanbelas');
sPattern: string = '000000000000000';

var
S : string;
Satu, Dua, Tiga, Belas, Gabung: string;
Sen, Sen1, Sen2: string;
Hitung : integer;
One, Two, Three: integer;

begin
One := 1;
Two := 2;
Three := 3;
Hitung := 1;
Kata := '';
S := copy(sPattern, 1, length(sPattern) - length(trim(sValue))) + sValue;
Sen1 := Copy(S, 14, 1);
Sen2 := Copy(S, 15, 1);
Sen := Sen1 + Sen2;
while Hitung < 5 do
begin
Satu := Copy(S, One, 1);
Dua := Copy(S, Two, 1);
Tiga := Copy(S, Three, 1);
Gabung := Satu + Dua + Tiga;

if StrToInt(Satu) = 1 then
Kata := Kata + 'seratus '
else
if StrToInt(Satu) > 1 Then
Kata := Kata + Angka[StrToInt(satu)+1] + ' ratus ';

if StrToInt(Dua) = 1 then
begin
Belas := Dua + Tiga;
Kata := Kata + Angka[StrToInt(Belas)+1];
end
else
if StrToInt(Dua) > 1 Then
Kata := Kata + Angka[StrToInt(Dua)+1] + ' puluh ' +
Angka[StrToInt(Tiga)+1]
else
if (StrToInt(Dua) = 0) and (StrToInt(Tiga) > 0) Then
begin
if ((Hitung = 3) and (Gabung = '001')) or
((Hitung = 3) and (Gabung = ' 1')) then
Kata := Kata + 'seribu '
else
Kata := Kata + Angka[StrToInt(Tiga)+1];
end;

if (hitung = 1) and (StrToInt(Gabung) > 0) then
Kata := Kata + ' milyar '
else
if (Hitung = 2) and (StrToInt(Gabung) > 0) then
Kata := Kata + ' juta '
else
if (Hitung = 3) and (StrToInt(Gabung) > 0) then
begin
if (Gabung = '001') or (Gabung = ' 1') then
Kata := Kata + ''
else
Kata := Kata + ' ribu ';
end;
Hitung := Hitung + 1;
One := One + 3;
Two := Two + 3;
Three := Three + 3;
end;

if length(Kata) > 1 then Kata := Kata + ' rupiah ';

if (StrToInt(Sen) > 0) and (StrToInt(Sen) < 20) then
begin
if StrToInt(Sen) < 10 then Sen := Copy(Sen, 2, 1);
Kata := Kata + Angka[StrToInt(Sen)+1] + ' sen';
end
else
if StrToInt(Sen) > 19 then
Kata := Kata + Angka[StrToInt(Sen1)+1] + 'puluh ' +
Angka[StrToInt(Sen2)+1] + ' sen';
Result := Kata;
end;

Mengganti Nama Direktori menggunakan Delphi


Program pengolahan file, salah satunya mengganti nama direktori, sebagian fungsi dari banyaknya fungsi management file. Berikut sourcecode programnya: Download


unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
edt1: TEdit;
edt2: TEdit;
lblDirectory: TLabel;
lbl1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
st1, st2: PChar;
begin
st1:=PChar(edt1.Text);
st2:=PChar(edt2.Text);
MoveFile(st1, st2);
end;


end.

Tuesday, February 2, 2010

ComboBox dengan icon

ComboBox dengan icon
ComboBox Item dengan Icon? Tidak Masalah. Menggunakan OwnerDraw, kita bisa membuat semua menjadi mungkin.
Buat ComboBox dan ImageList dalam sebuah Form. Isi ImageList dengan icon untuk ComboBox item dan atur Style of ComboBox menjadi csOwnerDrawFixed atau csOwnerDrawVariable. Dan yang terakhir ketikkan event OnDrawItem pada ComboBox


unit Unit1;

interface

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

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
ImageList1: TImageList;
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
ComboBox: TComboBox;
bitmap: TBitmap;
begin
ComboBox := (Control as TComboBox);
Bitmap := TBitmap.Create;
try
ImageList1.GetBitmap(Index, Bitmap);
with ComboBox.Canvas do
begin
FillRect(Rect);
if Bitmap.Handle <> 0 then Draw(Rect.Left + 2, Rect.Top, Bitmap);
Rect := Bounds(Rect.Left + ComboBox.ItemHeight + 2, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
DrawText(handle, PChar(ComboBox.Items[Index]), length(ComboBox.Items[index]), Rect, DT_VCENTER+DT_SINGLELINE);
end;
finally
Bitmap.Free;
end;
end;
end.

Selamat Mencoba !!!

Tuesday, January 26, 2010

Delphi Duniaku

Meluangkan waktu untuk berbagi..
Sebagai media sharing...
Tentunya juga sebagai media pengingat..
Semoga bisa berguna untuk semua

Apakah yang dimaksud dengan Delphi? 
Delphi adalah sebuah lingkungan pemrograman Windows canggih, cocok untuk pemula dan profesional programer.

Menggunakan Delphi Anda dapat dengan mudah membuat self-contained, user friendly, aplikasi Windows yang sangat efisien dalam waktu yang sangat singkat - dengan minimum coding manual. Delphi menyediakan semua alat yang anda butuhkan untuk mengembangkan, menguji dan menyebarkan aplikasi Windows, termasuk sejumlah besar yang disebut komponen dapat digunakan kembali. Borland Delphi, di dalamnya versi terbaru, menyediakan solusi lintas platform bila digunakan dengan Borland Kylix - Borland's RAD alat untuk platform Linux. Delphi akar terletak pada Borland Turbo Pascal, diperkenalkan pada pertengahan tahun 1980-an. Object Pascal, berorientasi objek ekstensi untuk Pascal, merupakan bahasa dasar Delphi. Visual Component Library, atau VCL, adalah Object Pascal hirarki objek yang memungkinkan Anda untuk mendesain aplikasi. Cara yang lebih baik menggambarkan Delphi adalah Object Pascal berbasis lingkungan pengembangan visual.