Ferry Setiawan

Saturday, June 28, 2008

Delphi Code - Close Others Application

procedure TForm1.Button1Click(Sender: TObject);
var
MyHandle: THandle;
begin
MyHandle:=FindWindow(nil, 'Delphi Help');
SendMessage(MyHandle, WM_CLOSE, 0, 0);

end;

Delphi Code - Create DBGrid that shows images

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
var
Bmp: TBitmap;

begin
if Field is TGraphicField then
begin
try
Bmp:=TBitmap.Create;
Bmp.Assign(Field);
DBGrid1.Canvas.StretchDraw(Rect, Bmp);
finally

Bmp.Free;
end
end
else
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

Delphi Code - Load Dynamic Dll

function LoadAndRunDLLProcedure(sDLL,sFunc : string):boolean;
type
TFunc_Start = procedure;
var
Func_Start : TFunc_Start;
hDll : THandle;
FuncPtr : TFarProc;
sMsg : string;
begin
Result := False;
hDll := LoadLibrary(PChar(sDLL));
if(hDll > 32)then
begin
FuncPtr := GetProcAddress(hDll,PChar(sFunc));
@Func_Start := FuncPtr;
if(nil <> @Func_Start)then
begin
Func_Start;
Result := True;
end
else
begin
sMsg := 'DLL entry point ' + sFunc + ' not found';
MessageBox(0, PChar( sMsg ), 'Error',MB_OK );
end;
FreeLibrary( hDll );
end
else
begin
sMsg := 'File ' + sDLL + ' not found';
MessageBox(0, PChar( sMsg ), 'Error',MB_OK );
end;
end;

example :
LoadAndRunDLLProcedure('MyStuff.DLL','HelloWorld');

Delphi Code - Calling Control Panel Applets

function RunControlPanelApplet(sAppletFileName : string): integer;
begin
Result := WinExec(PChar('rundll32.exe shell32.dll,'+
'Control_RunDLL '+sAppletFileName),SW_SHOWNORMAL);
end;

access.cpl: Accessibility Properties
appwiz.cpl: Add/Remove Programs Properties
desk.cpl: Display Properties
intl.cpl: Regional Settings Properties
joy.cpl: Joystick Properties
main.cpl: Mouse Properties
mmsys.cpl: Multimedia Properties
modem.cpl: Modems Properties
sysdm.cpl: System Properties
timedate.cpl: Time/Date Properties

Delphi Code - Fungsi Terbilang

unit Konversi;

interface

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

function Convert(vString : String):String;
function ReadNumber(vString : String):string;
function GetNumber(vNum : Integer):String;

var
FValue : String;
FResult : String;
FCurrency : String;

implementation

function GetNumber(vNum : Integer):String;
begin
case vNum of
1 : Result := 'satu ';
2 : Result := 'dua ';
3 : Result := 'tiga ';
4 : Result := 'empat ';
5 : Result := 'lima ';
6 : Result := 'enam ';
7 : Result := 'tujuh ';
8 : Result := 'delapan ';
9 : Result := 'sembilan ';
0 : Result := '';
end;
end;

function ReadNumber(vString : String):string;
var level,len,j,i,vawal,awal,akhir,dgt : Integer;
str,bil,digit,lvl,temp2 : String;
begin
len := length(vString);
str := vString;
//cari level
level := len div 3;
if len mod 3 <> 0 then
level := level + 1;
//baca per 3 digit
for i := 1 to level do
begin
vawal := 0;
akhir := len - ((i-1)*3);
awal := akhir - 3 + 1;
if awal <= 0 then
awal := 1;
vString := copy(str,awal,akhir-awal+1);
if length(vString) < 3 then
akhir := length(vString)
else
akhir := 3;
awal := 1;
while akhir >= awal do
begin
dgt := akhir - awal + 1;
bil := GetNumber(StrToInt(vString[awal]));
case dgt of
1 : digit := '';
2 : digit := 'puluh ';
3 : digit := 'ratus ';
end;
//pengujian angka 10 dan 11
if dgt in [1,2] then
begin
if (StrToInt(vString[awal]+vString[awal+1]) >= 10) and
(StrToInt(vString[awal]+vString[awal+1]) < 20) then
begin
if (akhir - awal + 1 > 1) then
begin
if (StrToInt(vString[awal]) = 1) and
(StrToInt(vString[awal+1]) in [0,1]) then
bil := 'se'
else if (StrToInt(vString[awal]) = 1) and
(StrToInt(vString[awal+1]) > 1) then
bil := GetNumber(StrToInt(vString[awal+1]));
end;

if StrToInt(vString[akhir]) <> 0 then
begin
bil := bil + 'belas ';
digit := '';
vawal := awal + 1;
end
else
begin
vawal := awal + 1;
end;
end;
end;
if (((i = 2) and (awal=1) and (akhir = 1)) or
((akhir > 1) and (StrToInt(vString[1]) = 1) and
(dgt = 3))) and (StrToInt(vString[awal]) = 1) then
bil := 'se';

if (StrToInt(vString[awal]) = 0) then
digit := '';

Temp2 := Temp2 + bil + digit;
awal := awal + 1 + vawal;
vawal := 0;
end;
case i of
1 : lvl := '';
2 : lvl := 'ribu ';
3 : lvl := 'juta ';
4 : lvl := 'milliar ';
5 : lvl := 'trilliun ';
6 : lvl := 'billiun ';
end;

if StrToInt(vString) <> 0 then
begin
if (StrToInt(vString) = 1) and (i = 2) then
Temp2 := 'seribu '
else
Temp2 := Temp2 + lvl;
end;
Result := Temp2 + Result;
Temp2 := '';
end;
if (length(vString) = 1) and (vString = '0') then
Result := 'nol ';
end;

function Convert(vString : String):String;
var i,koma : Integer;
temp : string;
begin
koma := pos('.',vString);
if koma <> 0 then
begin
for i := 1 to 2 do
begin
if i = 1 then
begin
temp := copy(vString,1,koma-1);
Result := ReadNumber(temp);
Result := FResult + 'koma ';
end
else
begin
temp := copy(vString,koma+1,length(vString));
FResult := FResult + ReadNumber(temp);
end;
end;
end
else
Result := ReadNumber(vString);
Result := Result + 'rupiah';
end;

end.

Delphi Code - Set Header Caption With IniFile

Uses
... IniFiles;


procedure SetHeader(Grid: TDBGrid; Table: String);
var vIniFile : TIniFile;
i : integer;
Str : String;
DSource : TDataSource;
begin
vIniFile := TIniFile.Create('C:\Coba.Ini');
Str := vIniFile.ReadString(UPPERCASE(Table)+
'-JUMLAH','JMLKOLOM','0');
DSource := Grid.DataSource;
for i := 0 to StrToInt(Str)-1 do
begin
Grid.Columns.Add;
Grid.Columns[i].FieldName :=
DSource.DataSet.Fields[i].FieldName;
Grid.Columns[i].Title.Caption :=
vIniFile.ReadString(UPPERCASE(Table)+
'-HEADER',Grid.Columns[i].FieldName,'');
end;
end;
Penggunaan
SetHeader(DBGrid1,'Biodata');

Contoh Isi IniFile

[BIODATA-JUMLAH]
JMLKOLOM=2

[BIODATA-HEADER]
NO_=No
___NIK____=NIK



Keterangan :
BIODATA adalah nama table
BIODATA-JUMLAH adalah jumlah kolom yang ingin ditampilkan
BIODATA-HEADER adalah nama kolom dan caption yang akan ditampilkan

Delphi Code - Bmp To Jpg and Jpg To Bmp

uses Jpeg;

procedure Bmp2Jpeg(const BmpFileName, JpgFileName: string);
var
Bmp: TBitmap;
Jpg: TJPEGImage;
begin
Bmp := TBitmap.Create;
Jpg := TJPEGImage.Create;
try
Bmp.LoadFromFile(BmpFileName);
Jpg.Assign(Bmp);
Jpg.SaveToFile(JpgFileName);
finally
Jpg.Free;
Bmp.Free;
end;
end;

procedure Jpeg2Bmp(const BmpFileName, JpgFileName: string);
var
Bmp: TBitmap;
Jpg: TJPEGImage;
begin
Bmp := TBitmap.Create;
Jpg := TJPEGImage.Create;
try
Jpg.LoadFromFile(JpgFileName);
Bmp.Assign(Jpg);
Bmp.SaveToFile(BmpFileName);
finally
Jpg.Free;
Bmp.Free;
end;
end;

Delphi Code - Clear "Documents" menu

uses
Shlobj;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
SHAddToRecentDocs(SHARD_PATH, nil);
end;

Sunday, December 03, 2006

Tips for more efficient SQL

Tips for more efficient SQL

Space doesn’t permit me to discuss every detail of Oracle tuning, but I can share some general rules for writing efficient SQL in Oracle regardless of the optimizer that is chosen. These rules may seem simplistic but following them in a diligent manner will generally relieve more than half of the SQL tuning problems that are experienced:


Never do a calculation on an indexed column (e.g., WHERE salary*5 > :myvalue).

Whenever possible, use the UNION statement instead of OR conditions.

Avoid the use of NOT IN or HAVING in the WHERE clause. Instead, use the NOT EXISTS clause.

Always specify numeric values in numeric form and character values in character form (e.g., WHERE emp_number = 565, WHERE emp_name = ‘Jones’).

Avoid specifying NULL in an indexed column.

Avoid the LIKE parameter if = will suffice. Using any Oracle function will invalidate the index, causing a full-table scan.

Never mix data types in Oracle queries, as it will invalidate the index. If the column is numeric, remember not to use quotes (e.g., salary = 50000). For char index columns, always use single quotes (e.g., name = ‘BURLESON’).

Remember that Oracle’s rule-based optimizer looks at the order of table names in the FROM clause to determine the driving table. Always make sure that the last table specified in the FROM clause is the table that will return the smallest number of rows. In other words, specify multiple tables with the largest result set table specified first in the FROM clause.

Avoid using subqueries when a JOIN will do the job.

Use the Oracle “decode” function to minimize the number of times a table has to be selected.

To turn off an index you do not want to use (only with a cost-based optimizer), concatenate a null string to the index column name (e.g., name||') or add zero to a numeric column name (e.g., salary+0). With the rule-based optimizer, this allows you to manually choose the most selective index to service your query.

If your query will return more than 20 percent of the rows in the table, a full-table scan may be better than an index scan.

Always use table aliases when referencing columns.

Thursday, October 05, 2006

How To Get Serial Hard Disk

function GetHardDiskSerial(const DriveLetter: Char): string;
var
NotUsed: DWORD;
VolumeFlags: DWORD;
VolumeInfo: array[0..MAX_PATH] of Char;
VolumeSerialNumber: DWORD;
begin
GetVolumeInformation(PChar(DriveLetter + ':\'),
nil, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,
VolumeFlags, nil, 0);
Result := Format('Label = %s VolSer = %8.8X',
[VolumeInfo, VolumeSerialNumber])
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetHardDiskSerial('c'));
end;

Thursday, June 08, 2006

Set Header Caption With IniFile


Uses
... IniFiles;


procedure SetHeader(Grid: TDBGrid; Table: String);
var vIniFile : TIniFile;
i : integer;
Str : String;
DSource : TDataSource;
begin
vIniFile := TIniFile.Create('C:\Coba.Ini');
Str := vIniFile.ReadString(UPPERCASE(Table)+
'-JUMLAH','JMLKOLOM','0');
DSource := Grid.DataSource;
for i := 0 to StrToInt(Str)-1 do
begin
Grid.Columns.Add;
Grid.Columns[i].FieldName :=
DSource.DataSet.Fields[i].FieldName;
Grid.Columns[i].Title.Caption :=
vIniFile.ReadString(UPPERCASE(Table)+
'-HEADER',Grid.Columns[i].FieldName,'');
end;
end;




Penggunaan
SetHeader(DBGrid1,'Biodata');

Contoh Isi IniFile

[BIODATA-JUMLAH]
JMLKOLOM=2

[BIODATA-HEADER]
NO_=No
___NIK____=NIK



Keterangan :
BIODATA adalah nama table
BIODATA-JUMLAH adalah jumlah kolom yang ingin ditampilkan
BIODATA-HEADER adalah nama kolom dan caption yang akan ditampilkan

Thursday, June 01, 2006

Disable Keyboard Functions

Penggunaan
if Key = VK_RETURN then
--> statement...
Letakkan di Event KEYDOWN atau object lainnya, Mis TEdit,TComboBox, dll
untuk Form, Set Property KeyPreview menjadi True

Daftar Virtual Key yang lain
----------------------------------------------------------------------------------
VK_LBUTTON Left mouse button
VK_RBUTTON Right mouse button
VK_CANCEL Control+Break
VK_MBUTTON Middle mouse button
VK_BACK Backspace key
VK_TAB Tab key
VK_CLEAR Clear key
VK_RETURN Enter key
VK_SHIFT Shift key
VK_CONTROL Ctrl key
VK_MENU Alt key
VK_PAUSE Pause key
VK_CAPITAL Caps Lock key
VK_KANA Used with IME
VK_HANGUL Used with IME
VK_JUNJA Used with IME
VK_FINAL Used with IME
VK_HANJA Used with IME
VK_KANJI Used with IME
VK_CONVERT Used with IME
VK_NONCONVERT Used with IME
VK_ACCEPT Used with IME
VK_MODECHANGE Used with IME
VK_ESCAPE Esc key
VK_SPACE Space bar
VK_PRIOR Page Up key
VK_NEXT Page Down key
VK_END End key
VK_HOME Home key
VK_LEFT Left Arrow key
VK_UP Up Arrow key
VK_RIGHT Right Arrow key
VK_DOWN Down Arrow key
VK_SELECT Select key
VK_PRINT Print key (keyboard-specific)
VK_EXECUTE Execute key
VK_SNAPSHOT Print Screen key
VK_INSERT Insert key
VK_DELETE Delete key
VK_HELP Help key
VK_LWIN Left Windows key (Microsoft keyboard)
VK_RWIN Right Windows key (Microsoft keyboard)
VK_APPS Applications key (Microsoft keyboard)
VK_NUMPAD0 0 key (numeric keypad)
VK_NUMPAD1 1 key (numeric keypad)
VK_NUMPAD2 2 key (numeric keypad)
VK_NUMPAD3 3 key (numeric keypad)
VK_NUMPAD4 4 key (numeric keypad)
VK_NUMPAD5 5 key (numeric keypad)
VK_NUMPAD6 6 key (numeric keypad)
VK_NUMPAD7 7 key (numeric keypad)
VK_NUMPAD8 8 key (numeric keypad)
VK_NUMPAD9 9 key (numeric keypad)
VK_MULTIPLY Multiply key (numeric keypad)
VK_ADD Add key (numeric keypad)
VK_SEPARATOR Separator key (numeric keypad)
VK_SUBTRACT Subtract key (numeric keypad)
VK_DECIMAL Decimal key (numeric keypad)
VK_DIVIDE Divide key (numeric keypad)
VK_F1 F1 key
VK_F2 F2 key
VK_F3 F3 key
VK_F4 F4 key
VK_F5 F5 key
VK_F6 F6 key
VK_F7 F7 key
VK_F8 F8 key
VK_F9 F9 key
VK_F10 F10 key
VK_F11 F11 key
VK_F12 F12 key
VK_F13 F13 key
VK_F14 F14 key
VK_F15 F15 key
VK_F16 F16 key
VK_F17 F17 key
VK_F18 F18 key
VK_F19 F19 key
VK_F20 F20 key
VK_F21 F21 key
VK_F22 F22 key
VK_F23 F23 key
VK_F24 F24 key
VK_NUMLOCK Num Lock key
VK_SCROLL Scroll Lock key
VK_LSHIFT Left Shift key (only used with GetAsyncKeyState and GetKeyState)
VK_RSHIFT Right Shift key (only used with GetAsyncKeyState and GetKeyState)
VK_LCONTROL Left Ctrl key (only used with GetAsyncKeyState and GetKeyState)
VK_RCONTROL Right Ctrl key (only used with GetAsyncKeyState and GetKeyState)
VK_LMENU Left Alt key (only used with GetAsyncKeyState and GetKeyState)
VK_RMENU Right Alt key (only used with GetAsyncKeyState and GetKeyState)
VK_PROCESSKEY Process key
VK_ATTN Attn key
VK_CRSEL CrSel key
VK_EXSEL ExSel key
VK_EREOF Erase EOF key
VK_PLAY Play key
VK_ZOOM Zoom key
VK_NONAME Reserved for future use
VK_PA1 PA1 key
VK_OEM_CLEAR Clear key

Optimizing Your Delphi Application

1. Keep your code clean. This will make it easier to read and understand the code. Use meaningful variable and constant names. For example sMessage, sName for String variables, iCounter for integer variables. Constants could all be uppercase: iRATE for integer constant and sWARNING for a string constant. For components: don't ever use the standard naming: Button1, Edit1, Combo1. The more components you have the more time it'll take to find the one you want.


2. Leave the {$O+} (or {$Optimization On}) compiler directive on. With this directive, Delphi compiler produces more efficient code. Sometimes, debugging is more difficult with optimization enabled: you cannot set a breakpoint on a statement if the optimizer determines that the statement serves no purpose. All optimizations performed by Delphi's Object Pascal compiler are guaranteed not to alter the meaning of a program.


3. You may want to compile your programs with run-time packages. This can save a lot on size when you have a lot of programs that use the same libraries. Unfortunately, when distributing your application you will need to deploy dll's as well.


4. Don't create all the forms on program startup (they are created by default) because this will make your program load slower and require a lot more memory. Instead you should use dynamically created forms.


5. Use Windows API functions instead of writing your own functions to do the same task. They are usually faster and you will save size too.


6. When including images in your programs try to optimize them first. When including BMP files be sure that the file has the exact size you need else crop it and make sure that you don't have a 16 color image saved in 64K color mode. When including GIF images you may optimize their color palete with special tools. When including JPEG images you may want to use image compression. Convert JPEG image that contains 256 or less colors to a GIF image - they are much smaller.


7. When your are going to use only one or two functions or procedures from a unit and you have the unit's source code don't include the whole unit in the uses section but insert only the functions you are going to use into the code. This will prevent Delphi to add to executable all the routines from that unit.


8. Instead of using non-visible components it's usually better to include the code in your program. There's no need in using a component that displays Windows browse directory dialog when it's really just a few lines of code.


9. If you have a time-critical function (important loops) you should be very careful. What seems fast on your computer may not be as fast on other PC's. Here you should use as small code as possible and make it fast. You may want to use assembler (always comment it).


10. Nearly all programs have to do some rudimentary calculations of some kind. Simplifying your formulas to use faster functions is a very typical optimization opportunity. Knowing how data bandwidth and calculations perform relative to each other is also very important. For example, using tables to avoid certain recalculations is often a good idea (values for Sine and Cosine). Always try to use more efficient algorithms to replace an inefficient one are: a Binary Search to replace a Linear Search.

Wednesday, March 08, 2006

Oracle - Troubleshoting

Berisi Lebih dari 300 Topik dan bulletin (technical information, alerts, contoh scripts dan contoh file) yang diterbitkan oleh Oracle Worldwide Customer Support.

Kekurangannya : Navigasinya kurang bagus, sehingga pencarian artikel agak sulit

Tuesday, March 07, 2006

Oracle - Tutorial - Training


Tutorial dan Training Oracle Gratis
Content : Oracle 9 dan Oracle 10, Plus Java

Bisa di download, disediakan juga jika berminat untuk beli bukunya...tapi rasanya tidak ada yang beli...soalnya bisa di download :)

Oracle - Article

Akadias Publications

Article Oracle disertai beberapa file PDF
beberapa article berbahas jerman, mungkin ada yang mau sekalian belajar bahasa jerman :)