Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

On line creating TSppidbutton

I need app where can i online create TSppedbutton (height 20,width 20) from main menu.
And to move (drag and drop) TSppedbutton with left mouse click on sppedbutton.
Then also possibility to open popup menu from right
click of the mouse on sppedbutton to define tag of TSppedbutton and the option to delete it.
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

by online you mean run-time ?
Here is the code to create dynamically a TSpeedButton, and to move it around when left-clickin. It will also display a popup menu with right click. You have to define its items. In this example I added only one item to delete the button first saved in the SpeedButtonMouseDown event.
unit DynamicButtons;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Menus, StdCtrls;

type
  TForm1 = class(TForm)

// don't paste these lines, 
// add the popup menu component and define its items with IDE
   popMenu:TPopupMenu;
   mnuDelete: TMenuItem;
   
   procedure SpeedButtonMouseDown(Sender: TObject; Button: TMouseButton;
               Shift: TShiftState; X, Y: Integer);
   procedure SpeedButtonMouseUp(Sender: TObject; Button: TMouseButton;
               Shift: TShiftState; X, Y: Integer);
   procedure SpeedButtonMouseMove(Sender: TObject; Shift: TShiftState; 
               X, Y: Integer);
   procedure mnuDeleteClick(Sender: TObject);
  private
   _Pos:TPoint;
   _btn:TSpeedButton;   
  public
   Function AddSpeedButton(L:Integer=10;T:Integer=10):TSpeedButton;
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
 AddSpeedButton(10,10);
 AddSpeedButton(40,10);
end;

Function TForm1.AddSpeedButton(L:Integer=10;T:Integer=10):TSpeedButton;
begin
 Result:=TSpeedButton.Create(Self);
 Result.Parent:=Self; // or a panel
 Result.Width:=20;
 Result.Height:=20;
 Result.Left:=L;
 Result.Top:=T;
 Result.onMouseDown:=SpeedButtonMouseDown;
 Result.onMouseUp:=SpeedButtonMouseUp;
 Result.onMouseMove:=SpeedButtonMouseMove;
end;

procedure TForm1.SpeedButtonMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 _btn:=TSpeedButton(Sender); // Might be usefull for other treatments
 if ssRight in Shift Then popMenu.Popup(Mouse.CursorPos.X,Mouse.CursorPos.Y);
 if ssLeft in Shift then
  begin
  // set the last clicked shape with right button
  // get its start position relative to position of mouse when the click occurred
  _Pos.X:=_btn.Left-Mouse.CursorPos.X;
  _Pos.Y:=_btn.Top-Mouse.CursorPos.Y;
  end;
end;

procedure TForm1.SpeedButtonMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 if ssLeft in Shift Then _btn:=nil;
end;

procedure TForm1.SpeedButtonMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
 if Not (ssLeft in Shift) then _btn:=nil;
 if Not Assigned(_btn) then Exit;
// Don't use X & Y directly as they are relative to the object, which is moving
 _btn.Left:=_Pos.X+Mouse.CursorPos.X;
 _btn.Top:=_Pos.Y+Mouse.CursorPos.Y;
end;

procedure TForm1.mnuDeleteClick(Sender: TObject);
begin
 if Assigned(_btn) then
  begin
   _btn.Free;
   _btn:=nil;
  end;
end;

Open in new window

Avatar of pr2501
pr2501

ASKER

With code below i have next error>
Why?
 
[Pascal Error] Unit1.pas(16): E2003 Undeclared identifier: 'TSpeedButton'

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Menus, StdCtrls;


type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    delate1: TMenuItem;
  private
   _Pos:TPoint;
   _btn:TSpeedButton;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

thank you