+62 812-1171-5379 Fast Respond

Tips dan Trik Delphi - Form

Scan Barcode / Form / Windows / Delphi 7 - XE

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
     if ord(key) = VK_RETURN then
     begin
          ListBox1.Items.Add(Edit1.Text);
          Edit1.Text := '';
     end;
end;

                                        
Menampilkan dialog Unigui / Form / Windows / Delphi 7 - XE
procedure TMainForm.UniButton4Click(Sender: TObject);
begin
     MessageDlg('Apakah anda lapar ?', mtConfirmation, mbYesNoCancel,
          procedure(Sender: TComponent; Res: Integer)
          begin
               if res = mrYes Then ShowMessage('Anda memilih yes');
               if res = mrNo Then ShowMessage('Anda memilih no');
               if res = mrCancel Then ShowMessage('Anda memilih cancel');
          end
        );
end;

Membuat tulisan miring / Form / Windows / Delphi 7 - XE
procedure TForm1.Button1Click(Sender: TObject);
var
  lf : TLogFont;
  tf : TFont;
begin
  with Form1.Canvas do begin
    Font.Name := 'Arial';
    Font.Size := 24;
    tf := TFont.Create;
    tf.Assign(Font);
    GetObject(tf.Handle, sizeof(lf), @lf);
    lf.lfEscapement := 450;
    lf.lfOrientation := 450;
    tf.Handle := CreateFontIndirect(lf);
    Font.Assign(tf);
    tf.Free;
    TextOut(20, Height div 2, 'Rotated Text!');
  end;
end;

Scorll pada DB grid / Form / Windows / Delphi 7 - XE
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TWheelDBGrid = class(TDBGrid)
  public
    property OnMouseWheel;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TWheelDBGrid(DBGrid1).OnMouseWheel := DBGridMouseWheel;
end;

function GetNumScrollLines: Integer;
begin
  SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, @Result, 0);
end;

procedure TForm1.DBGridMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  Direction: Shortint;
begin
  Direction := 1;
  if WheelDelta = 0 then
    Exit
  else if WheelDelta > 0 then
    Direction := -1;

  with TDBGrid(Sender) do
  begin
    if Assigned(DataSource) and Assigned(DataSource.DataSet) then
      DataSource.DataSet.MoveBy(Direction * GetNumScrollLines);
    Invalidate;
  end;
end;

Membuat splash program / Form / Windows / Delphi 7 - XE
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {FSplash};

{$R *.res}

begin
  Application.Initialize;

  // tambahkan perintah dibawah ini
  Fsplash := TFSplash.Create(Application);
  Fsplash.Show;
  Fsplash.Update;
  // Akhir tambahkan perintah dibawah ini

  Application.CreateForm(TForm1, Form1);

  // Tambahkan pula dibawah ini
  Fsplash.Close;

  Application.Run;
end.

// pada project option pilih nama FSplash dipindah ke available form


Membuat style warna dialog / Form / Windows / Delphi 7 - XE
var 
  f: TForm;
begin
    f := Dialogs.CreateMessageDialog('HELLOWORLD', dialogs.mtInformation, dialogs.mbOKCancel);
    f.Color := clBlue;
    f.Font.Color := clLime;
    if f.ShowModal = mrOk then
       ShowMessage('OK ditekan')
    else
       ShowMessage('Cancel ditekan.')
end;

Mengganti caption dialog / Form / Windows / Delphi 7 - XE
function MyMessageDialog(const Msg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; Captions: array of string): Integer;
var
  aMsgDlg: TForm;
  i: Integer;
  dlgButton: TButton;
  CaptionIndex: Integer;
begin
    aMsgDlg := CreateMessageDialog(Msg, DlgType, Buttons);
    captionIndex := 0;
    for i := 0 to aMsgDlg.ComponentCount - 1 do
    begin
      if (aMsgDlg.Components[i] is TButton) then
      begin
        dlgButton := TButton(aMsgDlg.Components[i]);
        if CaptionIndex > High(Captions) then Break;
        dlgButton.Caption := Captions[CaptionIndex];
        Inc(CaptionIndex);
      end;
    end;
    Result := aMsgDlg.ShowModal;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     if MyMessageDialog('Berapa banyak', mtConfirmation, mbOKCancel, ['1', '2']) = mrOk then
        ShowMessage('"1" Diklik')
     else
        ShowMessage('"2" Diklik');
end;

Memberi warna button pada toolbar / Form / Windows / Delphi 7 - XE
procedure TForm1.ToolBar1CustomDrawButton(Sender: TToolBar;
  Button: TToolButton; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
     Sender.Canvas.Brush.Color := clAqua;
     Sender.Canvas.Rectangle(Button.BoundsRect);
end;

Keterangan : tambahkan toolbar dan klik kanan pada toolbar tersebut pilih new button, buat sebanyak 5 tombol


Memberi event pada create komponen / Form / Windows / Delphi 7 - XE
Var
    Tombol : TButton;

procedure TForm1.Buttonme(Sender: TObject);
begin
     ShowMessage(Inttostr(i));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     Tombol := TButton.Create(form1);
     Tombol.Parent := form1;
     Tombol.Left := 100;
     Tombol.Top := 100;
     Tombol.OnClick := Buttonme;
End;


Mengetahui posisi Top Left mouse / Form / Windows / Delphi 7 - XE
procedure TForm1.Button1Click(Sender: TObject);
var
  MausPos: TPoint;
begin
  GetCursorPos(MausPos);
  label1.Caption := IntToStr(MausPos.x);
  label2.Caption := IntToStr(MausPos.y);
end;

// Set mouse position to (x,y)

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetCursorPos(600, 600);
end;