ItemRect (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a list box on a form. When you click one of the items in the list box, a beep occurs if the mouse is in the first half of the item. A similar technique can be used to detect "hot" regions in an owner-draw list box.

Code

procedure TForm1.FormCreate(Sender: TObject);
begin
  with ListBox1 do
  begin
    Items.Add('Hello');
    Items.Add('New');
    Items.Add('World');
  end;
end;

procedure TForm1.ListBox1MouseUp (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ListBoxItem: TRect;
begin
  if Button = mbLeft then
  begin
    ListBoxItem := ListBox1.ItemRect(ListBox1.ItemIndex);
    if (Y > ListBoxItem.Top) and 
       (Y < ListBoxItem.Bottom) and 
       (X > ListBoxItem.Left) and 
       (X < (ListBoxItem.Left + ListBoxItem.Right) div 2) then
      Beep;
  end;
 end;

Uses

Personal tools
Assistance