Components

Items of ListBox with drag and drop

Posted in Components

Use Move method of the ListBox1.Items for the changing position of the item in a list. var Form1: TForm1; NumX, NumY: Integer; implementation

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  Num1, Num2: Integer;
  Point1, Point2: TPoint;
begin
  Point1.X:=NumX;
  Point1.Y:=NumY;
  Point2.X:=X;
  Point2.Y:=Y;
  with Source as TListBox do
  begin
    Num2:=ListBox1.ItemAtPos(Point1,True);
    Num1:=ListBox1.ItemAtPos(Point2,True);
    ListBox1.Items.Move(Num2, Num1);
  end;
end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if Source=ListBox1 then Accept:=True;
end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  NumY:=Y;
  NumX:=X;
end;

Highlight componen

Posted in Components

Use CM_MOUSEENTER and CM_MOUSELEAVE messages:

  TYourObject = class(TAnyControl)
  ...
  private
    procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
  ...
  end;

implementation

procedure TYourObject.CMMouseEnter(var AMsg: TMessage);
begin
  ParentColor:=False;
  Color:=clHighlightText;
end;

procedure TYourObject.CMMouseLeave(var AMsg: TMessage);
begin
  ParentColor:=True;
end;

Get image on the button

Posted in Components

Use GetDC function (this function returns handle of a display device context) and Handle method of Bitmap.Canvas for assigning result of GetDC function with it.

procedure TForm1.Button1Click(Sender: TObject);
var
  Bitmap, Bitmap2: TBitmap;
  ButHandle: THandle;
  Rec: TRect;
begin
  Bitmap:=TBitmap.Create;
  Bitmap2:=TBitmap.Create;
  Bitmap2.LoadFromFile('factory.bmp');
  Rec:=Rect(2, 2, Button1.Width-2, Button1.Height-2);
  ButHandle:=GetDC(Button1.Handle);
  Bitmap.Canvas.Handle:=ButHandle;
  Bitmap.Canvas.StretchDraw(Rec, Bitmap2);
end;

Get events of component

Posted in Components

Use for solving of this question GetPropList function. Don't forget to add TypInfo in the uses chapter.

uses TypInfo;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  ListProp: PPropList;
  TD: PTypeData;
  Num, i: Integer;
begin
  GetMem(ListProp, SizeOf(PPropInfo)*TD.PropCount);
  Num:=GetPropList(
    Sender.ClassInfo, 
    [tkMethod], 
    ListProp);
  for i:=0 to Num-1 do
    Memo1.Lines.Add(ListProp[i]^.Name);
end;

Get component index at runtime

Posted in Components

Use IndexInParent function:

procedure TForm1.Button3Click(Sender: TObject);
begin
  MessageBox(
    Handle, 
    PChar('Index = '+IntToStr(IndexInParent(Button3))),
    'Information',  
    MB_OK);
end;

function TForm1.IndexInParent(VControl: TControl): Integer;
var
  ParentControl: TWinControl;
begin
  ParentControl:=TForm(VControl.Parent);
  if (ParentControl<>nil) then
    for Result:=0 to ParentControl.ControlCount-1 do
      if (ParentControl.Controls[Result]=VControl) then
        Exit;
  Result:=-1;
end;