• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

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.
0
pr2501
Asked:
pr2501
  • 3
  • 2
1 Solution
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
by online you mean run-time ?
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
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

0
 
pr2501Author Commented:
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

0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
Add the unit "Buttons" to the uses list
0
 
pr2501Author Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now