PDA

View Full Version : [Forms] how to pick colors (TColorPicker)



jhildy
08-13-2007, 02:21 AM
Since some people have been asking about this i made a tutorial. I am pretty sure this only works in Scar 3.10-3.11. For this tutorial I am assuming you have standard knowledge of how a form works.

First start out with a simple form
http://img483.imageshack.us/img483/7494/formuj9.png (http://imageshack.us)

program New;

var
// InitForm variables.
frmDesign : TForm;
Button1 : TButton;
colors : TEdit;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 300;
frmDesign.Height := 173;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 20;
Button1.Top := 70;
Button1.Width := 75;
Button1.Height := 25;
Button1.Caption := 'Pick color';
Button1.TabOrder := 8;
colors := TEdit.Create(frmDesign);
colors.Parent := frmDesign;
colors.Left := 100;
colors.Top := 73;
colors.Width := 121;
colors.Height := 21;
colors.TabOrder := 9;
colors.Text := 'color';
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.


Then we add a Tcolorpicker var at the top, and create the tcolorpicker in the initform procedure.

program New;

var
// InitForm variables.
frmDesign : TForm;
Button1 : TButton;
colors : TEdit;
colorpicker : TColorPicker;//Add this

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 300;
frmDesign.Height := 173;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 20;
Button1.Top := 70;
Button1.Width := 75;
Button1.Height := 25;
Button1.Caption := 'Pick color';
Button1.TabOrder := 8;
colors := TEdit.Create(frmDesign);
colors.Parent := frmDesign;
colors.Left := 100;
colors.Top := 73;
colors.Width := 121;
colors.Height := 21;
colors.TabOrder := 9;
colors.Text := 'color';
colorpicker := Tcolorpicker.Create(frmdesign);//add this too
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.


then we have to make two procedures to make the tcolorpicker do something so first under the Tbutton put this and add this procedure.
program New;

var
// InitForm variables.
frmDesign : TForm;
Button1 : TButton;
colors : TEdit;
colorpicker : TColorPicker;
//add this procedure
Procedure colorpick(sender: Tobject);
begin
colorpicker.Pick;
colorpicker.onpick := @colorpicked;
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 300;
frmDesign.Height := 173;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 20;
Button1.Top := 70;
Button1.Width := 75;
Button1.Height := 25;
Button1.Caption := 'Pick color';
Button1.TabOrder := 8;
Button1.Onclick := @colorpick;// add this
colors := TEdit.Create(frmDesign);
colors.Parent := frmDesign;
colors.Left := 100;
colors.Top := 73;
colors.Width := 121;
colors.Height := 21;
colors.TabOrder := 9;
colors.Text := 'color';
colorpicker := Tcolorpicker.Create(frmdesign);
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.

this makes it so when you click the button it goes to the colorpicker and when it picks it goes to this next procedure. so add this one last procedure and you are done.
program New;

var
// InitForm variables.
frmDesign : TForm;
Button1 : TButton;
colors : TEdit;
colorpicker : Tcolorpicker;
//add this
Procedure colorpicked(Sender: TObject; Color, X, Y: Integer);
begin
colors.text:= inttostr(color);
end;

Procedure colorpick(sender: Tobject);
begin
colorpicker.Pick;
colorpicker.onpick := @colorpicked;
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 300;
frmDesign.Height := 173;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 20;
Button1.Top := 70;
Button1.Width := 75;
Button1.Height := 25;
Button1.Caption := 'Pick color';
Button1.TabOrder := 8;
Button1.Onclick := @colorpick;
colors := TEdit.Create(frmDesign);
colors.Parent := frmDesign;
colors.Left := 100;
colors.Top := 73;
colors.Width := 121;
colors.Height := 21;
colors.TabOrder := 9;
colors.Text := 'color';
colorpicker := Tcolorpicker.Create(frmdesign);
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.

that just makes it so when it picks the color it takes the color and puts it in the color.text variable.

and your done now have fun. :f:

Macho Man67
08-13-2007, 02:54 AM
Wow....You know forms like inside and out:D

Great Tutorial, This is going to be very helpful with my new script( Macho Oak Owner)

jhildy
08-13-2007, 02:56 AM
Thanks glad i could help.

Santa_Clause
08-13-2007, 09:10 AM
Nice tutorial Jhildy...I always do it the other way Haha.

jhildy
08-13-2007, 02:31 PM
what way where u use the pickcolor procedure. yeh i just wanted to experiment with the new type tcolorpicker so i figured id write a tutorial.

yanix
08-14-2007, 10:17 AM
Hi jhildy i have done a script just like u did but something is wrong heres the script i did but i just cant get it to work i have 3 color pick's but it only works on color pick 2 so if u press pick1 it will go to color2 edit plz help if u run this script u might know what i meen this will be used in my scripts

program New;
var
frmDesign : TForm;
UserLabel : TLabel;
PassLabel : TLabel;
UserEdit : TEdit;
PassEdit : TEdit;
StartButton : TButton;
AutoBox : TCheckBox;
Color : TEdit;
Color3 : TEdit;
Color2 : TEdit;
Button1 : TButton;
Button2 : TButton;
Button3 : TButton;
Username, Password : String;
colorpicker : TColorPicker;

Procedure colorpicked(Sender: TObject; Color, X, Y: Integer);
begin
color2.text:= inttostr(color);
end;

Procedure colorpick(sender: Tobject);
begin
colorpicker.Pick;
colorpicker.onpick := @colorpicked;
end;

procedure StartClick(sender: TObject);
begin
Writeln('Form Is Done');
Username := UserEdit.Text;
Password := PassEdit.Text;
frmDesign.ModalResult:= mrOk; // Closes the form
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.Width := 300;
frmDesign.Height := 300;
frmDesign.Caption := 'Form.';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
UserLabel := TLabel.Create(frmDesign);
UserLabel.Parent := frmDesign;
UserLabel.Left := -1;
UserLabel.Top := 0;
UserLabel.Width := 48;
UserLabel.Height := 13;
UserLabel.Caption := 'Username';
PassLabel := TLabel.Create(frmDesign);
PassLabel.Parent := frmDesign;
PassLabel.Left := -1;
PassLabel.Top := 35;
PassLabel.Width := 46;
PassLabel.Height := 13;
PassLabel.Caption := 'Password';
UserEdit := TEdit.Create(frmDesign);
UserEdit.Parent := frmDesign;
UserEdit.Left := 60;
UserEdit.Top := 1;
UserEdit.Width := 100;
UserEdit.Height := 21;
UserEdit.Hint := 'Username';
UserEdit.ParentShowHint := False;
UserEdit.ShowHint := True;
UserEdit.TabOrder := 8;
PassEdit := TEdit.Create(frmDesign);
PassEdit.Parent := frmDesign;
PassEdit.Left := 60;
PassEdit.Top := 29;
PassEdit.Width := 100;
PassEdit.Height := 21;
PassEdit.Hint := 'Password';
PassEdit.ParentShowHint := False;
PassEdit.ShowHint := True;
PassEdit.TabOrder := 9;
StartButton := TButton.Create(frmDesign);
StartButton.Parent := frmDesign;
StartButton.Left := 83;
StartButton.Top := 52;
StartButton.Width := 50;
StartButton.Height := 20;
StartButton.Hint := 'Press Me To Start This Script.';
StartButton.Caption := 'Start';
StartButton.ParentShowHint := False;
StartButton.ShowHint := True;
StartButton.TabOrder := 10;
AutoBox := TCheckBox.Create(frmDesign);
AutoBox.Parent := frmDesign;
AutoBox.Left := 68;
AutoBox.Top := 71;
AutoBox.Width := 97;
AutoBox.Height := 17;
AutoBox.Hint := 'Use AutoColors?';
AutoBox.Caption := 'AutoColors?';
AutoBox.Checked := True;
AutoBox.ParentShowHint := False;
AutoBox.ShowHint := True;
AutoBox.State := cbChecked;
AutoBox.TabOrder := 11;
Color := TEdit.Create(frmDesign);
Color.Parent := frmDesign;
Color.Left := 0;
Color.Top := 117;
Color.Width := 100;
Color.Height := 21;
Color.Hint := 'Pick Color 1 Of Item';
Color.ParentShowHint := False;
Color.ShowHint := True;
Color.TabOrder := 12;
Color.Text := 'Color';
Color3 := TEdit.Create(frmDesign);
Color3.Parent := frmDesign;
Color3.Left := 0;
Color3.Top := 223;
Color3.Width := 100;
Color3.Height := 21;
Color3.Hint := 'Pick Color 3 Of Item';
Color3.TabOrder := 13;
Color3.Text := 'Color3';
Color2 := TEdit.Create(frmDesign);
Color2.Parent := frmDesign;
Color2.Left := 0;
Color2.Top := 168;
Color2.Width := 100;
Color2.Height := 21;
Color2.Hint := 'Pick Color 2 Of Item';
Color2.ParentShowHint := False;
Color2.ShowHint := True;
Color2.TabOrder := 14;
Color2.Text := 'Color2';
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 101;
Button1.Top := 116;
Button1.Width := 50;
Button1.Height := 20;
Button1.Hint := 'Pick Color';
Button1.Caption := 'Pick';
Button1.ParentShowHint := False;
Button1.ShowHint := True;
Button1.TabOrder := 15;
Button1.Onclick := @colorpick;
Button2 := TButton.Create(frmDesign);
Button2.Parent := frmDesign;
Button2.Left := 101;
Button2.Top := 168;
Button2.Width := 50;
Button2.Height := 20;
Button2.Hint := 'Pick Color';
Button2.Caption := 'Pick';
Button2.ParentShowHint := False;
Button2.ShowHint := True;
Button2.TabOrder := 16;
Button2.Onclick := @colorpick;
Button3 := TButton.Create(frmDesign);
Button3.Parent := frmDesign;
Button3.Left := 101;
Button3.Top := 223;
Button3.Width := 50;
Button3.Height := 20;
Button3.Hint := 'Pick Color';
Button3.Caption := 'Pick';
Button3.ParentShowHint := False;
Button3.ShowHint := True;
Button3.TabOrder := 17;
StartButton.OnClick := @StartClick;
Button3.Onclick := @colorpick;
colorpicker := Tcolorpicker.Create(frmdesign);
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;


end.

jhildy
08-15-2007, 02:29 AM
Thats because you only made it change color2.text you either have to make 3 seperate procedures or think of something to do with a case.