Google
 

Thursday, November 22, 2007

Add a Check Box to a standard dialog box

Suppose you have a confirmation dialog of some kind, where the user can select a checkbox displayinfg "Don't show this message again"). When the user closes the dialog - program can store the state of the check box (checked or not checked) in a global variable - the next time you need to display this dialog - you simply don't show it. The idea of realization is:
1. Create a dialog using CreateMessageDialog
2.This function will return a form object with dialog
3. In this object we can add a checkbox
4. Show dialog using ShowModal
5. Check a result and process a state of the checkbox
6. Destroy the created checkbox and the dialog


procedure TForm1.Button1Click(Sender: TObject) ;
var AMsgDialog: TForm;
ACheckBox: TCheckBox;
begin
___AMsgDialog := CreateMessageDialog('This is a test message.', mtWarning, [mbYes, mbNo]) ;
___ACheckBox := TCheckBox.Create(AMsgDialog) ;
___with AMsgDialog do
___try
______Caption := 'Dialog Title' ;
______Height := 169;
______with ACheckBox do
______begin
_________Parent := AMsgDialog;
_________Caption := 'Don''t show me again.';
_________Top := 121;
_________Left := 8;
______end;
______if (ShowModal = ID_YES) then
______begin
______if ACheckBox.Checked then
_________//do if checked
______else
_________//do if NOT checked
______end;
___finally
______Free;
___end;
end;

Tuesday, November 20, 2007

Get Default Printer Name

Here's how to get the name of the default printer on a computer:

~~~~~~~~~~~~~~~~~~~~~~~~~
uses Printers;
function GetDefaultPrinterName : string;
begin
___if (Printer.PrinterIndex > 0)then
___begin
______Result := Printer.Printers[Printer.PrinterIndex];
___end else
___begin
______Result := '';
___end;
end;

Saturday, November 17, 2007

Incremental Search for the TListBox Delphi Control



The TListBox Delphi control displays a collection of items in a scrollable list. Items can be selected by clicking on an item, the ItemIndex property gets or sets the index of the selected item. Incremental Searching for a ListBoxImagine a list box with a huge number of (unsorted) items. Finding the one user wants to select might turn into a nightmare. Let's provide the user with an option to immediately locate the item in the list box by adding incremental search functionality. Drop a TEdit and a TListBox on a form. Leave the default names: "Edit1" and "ListBox1".


Handle the Edit1's OnChange event as:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

procedure TListBoxSearchForm.Edit1Change(Sender: TObject) ;

const indexStart = -1;

var search : array[0..128] of Char;

begin

___//make sureLength(Edit1.Text) <= 128

___StrPCopy(search, Edit1.Text) ;

___ListBox1.ItemIndex := ListBox1.Perform(LB_SELECTSTRING, indexStart, LongInt(@search)) ;

end;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The StrPCopy
RTL function copies Edit1.Test string value into a null-terminated string variable "search".
The Perform method sends the specific LB_SELECTSTRING message directly to ListBox1.

Wednesday, November 14, 2007

Add Row Number In DBGrid

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. create new blank field in dbgrid
2. rename the title with 'No'
3. put this code in OnDrawColumncell
4. Now your Grid has a row number
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if
DataSource1.DataSet.RecNo > 0 then
begin
if
Column.Title.Caption = 'No' then
DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(DataSource1.DataSet.RecNo));
end;
end;

Sunday, November 11, 2007

Drag'n'Drop nodes inside TreeView

The following code uses GetNodeAt to add a dragged node as a child of the node under then mouse when it is dropped.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer) ;
var
___AnItem: TTreeNode;
___AttachMode: TNodeAttachMode;
___HT: THitTests;
begin
___if TreeView1.Selected = nilthen Exit;
___HT := TreeView1.GetHitTestInfoAt(X, Y) ;
___AnItem := TreeView1.GetNodeAt(X, Y) ;
___if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent]<> HT) then
___begin
______if (htOnItem in HT) or (htOnIcon in HT) then
_________AttachMode := naAddChild
______else if htNowhere in HT then
_________AttachMode := naAdd
______else if htOnIndent in HT then
_________AttachMode := naInsert;
______TreeView1.Selected.
______MoveTo(AnItem, AttachMode) ;
___end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Thursday, November 8, 2007

Mengetahui Jumlah Mapping Drive dalam Jaringan Komputer


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function
GetNetworkDriveMappings (SList: TStrings): integer;
var
____c: Char;
____ThePath: string;
____MaxNetPathLen: DWord;
begin
____SList.Clear;
____MaxNetPathLen := MAX_PATH;
____SetLength(ThePath, MAX_PATH) ;
____for c := 'A' to 'Z' do
____if WNetGetConnection(PChar('' + c + ':'), PChar(ThePath),MaxNetPathLen) = NO_ERROR then ____________sList.Add(c + ': ' + ThePath) ;
____Result := SList.Count;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tuesday, November 6, 2007

Change glyphs of TDBNavigator Buttons

Form1 has a DBNavigator1. In the OnCreate event for the form the custom bitmap ('GoFirst') for the First button is loaded from the resource.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.FormCreate(Sender: TObject) ;
var
___i : Integer;
___tempGlyph : tBitmap;
begin
___tempGlyph :=TBitmap.Create;
___try
______tempGlyph.LoadFromResourceName(HInstance,'GoFirst') ;
______with DBNavigator1 do
______begin
_________for i := 0 to ControlCount - 1 do
____________if Controls[c] is TNavigateBtn then
____________with TNavigateBtn(Controls[c])
____________do begin
_______________case Index of
_______________nbFirst: Glyph := tempGlyph;
____________end;
_________end;
______end;
___finally
______tempGlyph.Free;
___end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Monday, November 5, 2007

Implementing OnMouseOver for Items in a TComboBox, with custom hints.



Here's how to get the caption of an item in a TComboBox as mouse hovers over an item when the ComboBox is in drop down state. Use this "trick" to get the object associated with the "pre-selected" item in a combo box, or to display a custom hint for each item, for example.

When a TComboBox is in drop down state, and you move around the items with mouse, the item the mouse is over is "pre-selected" (colored in "clHighLight" color). If you ever wanted to get the text of that "pre-selected" item (to show a hint for this item, for example), you probably noticed that such a property does not exist.

When a combo box is dropped down, its Items are displayed in a list box type of control. The VCL TListBox component does actually provide an OnMouseOver event, yet, this event will not fire for TComboBox's list box since the list box part of the combobox is not a VCL control.

If you were lucky and the TComboBox's list box was TListBox, than you will have no problems to "get ListBox items as mouse hovers over them".
However, if you need to know what TComboBox item is "pre-selected", you'll need a little trick...

TComboBox.Items OnMouseMove
TComboBox item textIn order to get the text of the combo box item "under" the mouse, when the combo is in drop down state, you need to send some specific messages to the actual list box displayed. Your best bet is to place the code in the Application.OnIdle event handler since this event is fired when it is waiting for input from the user (not processing application's code).

The code provided below is an example of how to get the name of the combo box and the text of the item the mouse hovers over. Firstly, we get the handle of the window under the mouse (WindowFromPoint), we check if the window is a combo box (GetClassName). Secondly, we get the index of the "pre-selected" item (by sending the LB_ITEMFROMPOINT message to the underlying list box). Next, if we have a valid item index, we get the text of the item by sending the LB_GETTEXT message. Finally, the combo box name and the item text are displayed in a TLabel component.

procedure TForm1.ApplicationIdle(
sender: TObject; var Done: boolean);
var
pt : TPoint;
w : Hwnd;
ItemBuffer : array[0..256] of Char;
idx : Integer;
s : string;
begin
pt := Mouse.CursorPos;
w := WindowFromPoint(pt);
if w = 0 then Exit;

GetClassName(w, ItemBuffer, SizeOf(ItemBuffer));
if StrIComp(ItemBuffer, 'ComboLBox') = 0 then
begin
Windows.ScreenToClient(w, pt);
idx := SendMessage(w,
LB_ITEMFROMPOINT,
0,
LParam(PointToSmallPoint(pt)));
if idx >= 0 then
begin
if LB_ERR <> SendMessage(w,
LB_GETTEXT,
idx,
LParam(@ItemBuffer)) then
begin
s:= 'Mouse over item: ' + #13#10 +
Format('Combo.Name: %s,%sItem.Text: %s',
[ActiveControl.Name,#13#10,ItemBuffer]);

ComboItemLabel.Caption := s;

//explained later
hw.DoActivateHint(ActiveControl.Name + ItemBuffer,
'Hint for: ' + ItemBuffer);
end;
end;
end;
end; (*ApplicationIdle*)

That's it. Tricky yet simple.

In the sample application provided for download, there's also code that "activates" the ApplicationIdle procedure when the combo box fires its OnDropDown event; similary the Application.OnIdle is set to nil when the combo box fires the OnCloseUp event.

TComboBox.Item custom hint messages
As you can see from the screen shot, you can attach custom hint text for each combo box item. In general, a class TComboItemHint that derives from THintWindow is used to display custom hint messages for each item. The DoActivateHint procedure calls the ActivateHint method of the THintWindow. ActivateHint displays the hint window at the specified coordinates (where the mouse is).

type
TComboItemHint = class(THintWindow)
private
DoHint : boolean;

FControlName: string;
procedure SetControlName(const Value: string);
private
property ControlName : string
read FControlName write SetControlName;
public
procedure DoActivateHint(
ControlName : string; Text : string);
end;

...
procedure TComboItemHint.DoActivateHint(
ControlName : string; Text: string);
var
pt : TPoint;
r : TRect;
begin
self.ControlName := ControlName;
if DoHint then
begin
pt := Mouse.CursorPos;

//some hard-coding
r:= Rect(pt.X + 16,
pt.Y + 16,
pt.X + 100,
pt.Y + 32);

ActivateHint(r,Text);
DoHint := false;
end;
end;

procedure TComboItemHint.SetControlName(
const Value: string);
begin
if FControlName <> Value then
begin
ReleaseHandle;
//remove flicker
DoHint := True;
FControlName := Value;
end;
end;

Show any graphics format as Glyph on a SpeedButton

TBitBtn dan TSpeedButton hanya bisa menerima picture berformat BMp , jika kita hanya memiliki format ICO atau JPG dan ingin menampilkan sebagai Glyph, kita butuh konversi ke Bitmap, berikut Langkahnya :

~~~~~~~~~~~~~~~~~~~~~~~~~
var
___bmp: TBitmap;
begin
___bmp:=TBitmap.Create;
___try
______bmp.Width := Image.Picture.Graphic.Width;
______bmp.Height := Image.Picture.Graphic.Height;
______bmp.Canvas.Draw(0, 0, Image.Picture.Graphic) ;
______BitBtn.Glyph:=bmp;
___finally
______bmp.Free;
___end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Thursday, November 1, 2007

How to set system wide Hot Key for a Delphi application

Bila sebuah aplikasi berada pada posisi minimze di tray icon dan kita ingin restore aplikasi dengan short cut "Alt-Shift-F9" agar berada pada posisi aktif form. berikut contohnya :

~~~~~~~~~~~~~~~~~~~~~~~~~
//In the main forms OnCreate
//handler assign the hotkey:

If not RegisterHotkey
(Handle, 1, MOD_ALT or MOD_SHIFT, VK_F9) Then
ShowMessage('Unable to assign Alt-Shift-F9 as hotkey.') ;

//In the main forms
//OnClose event remove the handler:

UnRegisterHotkey( Handle, 1 ) ;

//Add a handler for the
//WM_HOTKEY message to the form:

private // form declaration
Procedure WMHotkey( Var msg: TWMHotkey ) ;
message WM_HOTKEY;

Procedure TForm1.WMHotkey( Var msg: TWMHotkey ) ;
Begin
__If msg.hotkey = 1 Then
__Begin
____ If IsIconic( Application.Handle ) Then
______Application.Restore;
____BringToFront;
__End;
End;
~~~~~~~~~~~~~~~~~~~~~~~~~