Tuesday, January 4, 2011

Membuat aplikasi shutdown komputer di delphi

uses ShellApi;
...
ShellExecute(Handle, 'open', 'c:\Windows\system32\shutdown.exe', nil, nil, SW_SHOWNORMAL) ;

Reading and writing INI files

Reading and writing INI files

Here is an overloaded group of commands to read from or write to an INI file. The routines are overloaded based on the d parameter. s is the [GROUP], k is Left side (key), d is the default or new parameter and f is the filename to use.
function GetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
if ini.ReadString(s,k,'') = '' then
begin
ini.WriteBool(s,k,d);
ini.UpdateFile;
end;
Result := ini.ReadBool(s,k,d);
FreeAndNil(ini);
end;

function SetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
ini.WriteBool(s,k,d);
ini.UpdateFile;
Result := ini.ReadBool(s,k,d);
FreeAndNil(ini);
end;

function GetIni(s,k: string; d: integer; f: string=''): integer; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
if ini.ReadString(s,k,'') = '' then
begin
ini.WriteInteger(s,k,d);
ini.UpdateFile;
end;
Result := ini.ReadInteger(s,k,d);
FreeAndNil(ini);
end;

function SetIni(s,k: string; d:integer; f: string=''): integer; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
ini.WriteInteger(s,k,d);
ini.UpdateFile;
Result := ini.ReadInteger(s,k,d);
FreeAndNil(ini);
end;

function GetIni(s,k: string; d: string; f: string=''): string; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
if ini.ReadString(s,k,'') = '' then
begin
ini.WriteString(s,k,d);
ini.UpdateFile;
end;
Result := ini.ReadString(s,k,d);
FreeAndNil(ini);
end;

function SetIni(s,k: string; d: string; f: string=''): string; overload;
var
ini: TMemIniFile;
begin
Result := d;
if f = '' then
begin
ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
end
else
begin
if not FileExists(f) then Exit;
ini := TMemIniFile.Create(f);
end;
ini.WriteString(s,k,d);
ini.UpdateFile;
Result := ini.ReadString(s,k,d);
FreeAndNil(ini);
end;
 
 
 
Source : http://www.delphidabbler.com/tips/59 

Programatically log users off

Programatically log users off

You may be thinking how can I use this procedure. Well I used it in a program where I could send commands to a remote machine.
procedure LogOff;
begin
ExitWindowsEx(EWX_LOGOFF, 0);
end;

Prevent system sleep

Prevent system sleep

Windows detects activities such as keyboard or mouse input – after a long idle time the system might go to sleep mode and/or power off the display device.
Here's a unit you can use to prevent the system going to sleep during critical operations.
unit SystemCriticalU;

interface

uses
Windows;

type
TSystemCritical = class
private
FIsCritical: Boolean;
procedure SetIsCritical(const Value: Boolean) ;
protected
procedure UpdateCritical(Value: Boolean) ; virtual;
public
constructor Create;
property IsCritical: Boolean read FIsCritical write SetIsCritical;
end;

var
SystemCritical: TSystemCritical;

implementation

{ TSystemCritical }
// REF: http://msdn.microsoft.com/en-us/library/aa373208.aspx
type
EXECUTION_STATE = DWORD;

const
ES_SYSTEM_REQUIRED = $00000001;
ES_DISPLAY_REQUIRED = $00000002;
ES_USER_PRESENT = $00000004;
ES_AWAYMODE_REQUIRED = $00000040;
ES_CONTINUOUS = $80000000;

KernelDLL = 'kernel32.dll';

{
SetThreadExecutionState Function
Enables an application to inform the system that it is in use,
thereby preventing the system from entering sleep or turning off the
display while the application is running.
}
procedure SetThreadExecutionState(ESFlags: EXECUTION_STATE);
stdcall; external kernel32 name 'SetThreadExecutionState';

constructor TSystemCritical.Create;
begin
inherited;
FIsCritical := False;
end;

procedure TSystemCritical.SetIsCritical(const Value: Boolean) ;
begin
if FIsCritical = Value then
Exit;
FIsCritical := Value;
UpdateCritical(FIsCritical);
end;

procedure TSystemCritical.UpdateCritical(Value: Boolean) ;
begin
if Value then
// Prevent the sleep idle time-out and Power off.
SetThreadExecutionState(ES_SYSTEM_REQUIRED or ES_CONTINUOUS)
else
// Clear EXECUTION_STATE flags to disable away mode and allow the
// system to idle to sleep normally.
SetThreadExecutionState(ES_CONTINUOUS);
end;

initialization

SystemCritical := TSystemCritical.Create;

finalization

SystemCritical.IsCritical := False;
SystemCritical.Free;

end.
Here's one usage example:
SystemCritical.IsCritical = true;
try
// do critical operation here
// without going in to sleep or turning off the display
finally
SystemCritical.IsCritical = false;
end;
 
Source : http://www.delphidabbler.com/tips/127 

How to turn numlock on by code

This function may come in especially handy when creating billing/financial applications where the keypad is a must. What it does is when called turns the NumLock status to "on".
uses
Windows;
...
procedure SetNumLockOn;
var
KeyState: TKeyBoardState;
begin
GetKeyboardState(KeyState);
if GetKeyState(VK_NUMLOCK) = 0 then begin
KeyState[VK_NUMLOCK] := 1;
SetKeyboardState(KeyState);
end;
end;

How to register a global hotkey

How to register a global hotkey

Global hotkeys are very handy when your application should be accessible at all time. For instance, you are browsing the Web and suddenly want to e-mail your friend. The most convenient way to do so would be to press a shortcut and the e-mail client would popup with a "new message" window no matter what program you are using at the time. This can be done with global shortcuts. They are system-wide no matter if the program you are using uses the same shortcut. A global shortcut is more important.
Here's the code to register and unregister a global shortcut.
uses
Messages, Windows;
...
// Your main form's class
protected
procedure WMHotKey(var Message: TMessage); message WM_HOTKEY;
...
implementation
...
procedure TMainForm.WMHotKey(var Message: TMessage);
begin
// This example brings the application up front. Put your code here
Application.BringToFront;
end;
Example of use:
RegisterHotKey(Handle, 100000 { Any unused number}, MOD_CONTROL, VK_F7);
// When closing program
UnregisterHotKey(Handle, 100000);
 
 
Source : http://www.delphidabbler.com/tips/16 

Auto-resizing and centring forms on screen

Auto-resizing and centring forms on screen

Calling this procedure from the form's OnActivate event will resize the form in proportion to the screen resolution – I do my programming on resolution 1280×768 hence the constants – once the form height and width has been calculated it can then be centred.
Note – form should be set to autoresize and AutoScroll := False at least while being re-calculated. Also the font size for the form should be (probably) 12 for the font to be resized downwards for 800×600 – all controls should be set to ParentFont := True so they will resize.
While this works exactly for the old screen resolutions which were all more or less in proportion there is nothing which will resize screens properly for the newer stretched screens, eg. for 1280×960 and 1280×1024. The min ratio calculated is scrx / screenx = 1 so the form stays with original measurements.
uses
math,windows;

const
screenx = 1280;
screeny = 768;

procedure tform1.autosizeall ;
var
scrx,scry, k:integer ;
ratio:double;
begin
scrx:= GetSystemMetrics(SM_CXSCREEN); {finds screen resolution x value}
scry:= (GetSystemMetrics(SM_CYSCREEN)); {finds screen resolution y value}

ratio:=min(scrx/screenx,scry/screeny);
{takes the smaller ratio and makes sure you dont make the window
too big for the screen}


scaleby(trunc(ratio*100),100);
{scales all controls and attempts to place them in the correct position}

{to centre the form on the screen}
form1.Left:=centreleft(form1.width);
form1.Top:=centretop(form1.Height) ;

end;
This code refers to two functions: centreleft and centretop. These are defined as follows:
function centreleft(fw:integer):integer;
{calculates the form.left}
var
smcx:integer;
begin
smcx:=GetSystemMetrics(SM_CXSCREEN);
centreleft:=(smcx-fw) div 2;
end;

function centretop(fh:integer):integer;
{calculates the form.top}
var
smcy:integer;
begin
smcy:=GetSystemMetrics(SM_CYSCREEN);
centtop:=(smcy-fh) div 2;
end;

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum