PDA

Ver la Versión Completa : No se puede cambiar color a TPanel creado en tiempo de ejecución


osmeg
30-07-2015, 18:56:43
Buenos días amigos de ClubDelphi,

Les escribo porque quiero plantearles un problema que tengo y no he podido resolver: Estoy creando un TPanel en tiempo de ejecución y quiero que al momento de que pase el mouse sobre él, el TPanel cambie de color. Para capturar el evento mencionado he utilizado la propiedad OnMouseEnter y lo hace correctamente, pero al momento intentar cambiar el color del panel, no ocurre nada.

A continuación detallo el código que estoy utilizando:


unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure panelMouseEnter(Sender:TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
panelprueba: TPanel;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
panelprueba:= TPanel.Create(Self);
panelprueba.Parent:= Self;
panelprueba.Width:= 100;
panelprueba.Height:= 100;
panelprueba.Top:=0;
panelprueba.Left:=0;
panelprueba.OnMouseEnter:= panelMouseEnter;
end;

procedure TForm1.panelMouseEnter(Sender:TObject);
begin
if Sender is TPanel then
begin
ShowMessage('Evento capturado');
TPanel(Sender).Color:= clBlue;
end;
end;

end.



Agradezco la colaboración que me puedan brindar para solucionar este problema.

ZayDun
30-07-2015, 19:54:12
Hola osmeg, solo tienes que agregar la propiedad ParentBackground a false. Espero que te sea de ayuda.

panelprueba.ParentBackground:=false;

nlsgarcia
30-07-2015, 20:45:18
osmeg,


...No se puede cambiar color a TPanel creado en tiempo de ejecución...

:rolleyes:

Revisa este código:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
procedure FormCreate(Sender: TObject);
procedure PanelMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseLeave(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Panel1.ParentBackground := False;
Panel2.ParentBackground := False;
Panel3.ParentBackground := False;
Panel4.ParentBackground := False;
end;

procedure TForm1.PanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin

if TPanel(Sender).Name = 'Panel1' then
Panel1.Color := clSkyBlue;

if TPanel(Sender).Name = 'Panel2' then
Panel2.Color := clGreen;

if TPanel(Sender).Name = 'Panel3' then
Panel3.Color := clYellow;

if TPanel(Sender).Name = 'Panel4' then
Panel4.Color := clRed;

end;

procedure TForm1.FormMouseLeave(Sender: TObject);
begin

if TPanel(Sender).Name = 'Panel1' then
Panel1.Color := clBtnFace;

if TPanel(Sender).Name = 'Panel2' then
Panel2.Color := clBtnFace;

if TPanel(Sender).Name = 'Panel3' then
Panel3.Color := clBtnFace;

if TPanel(Sender).Name = 'Panel4' then
Panel4.Color := clBtnFace;

end;

end.

El código anterior en Delphi XE7 sobre Windows 7 Professional x32, Permite cambiar el color de un TPanel en tiempo de ejecución, como se muestra en la siguiente imagen:

http://i.imgur.com/njjferA.gif

Espero sea útil :)

Nelson.

osmeg
30-07-2015, 21:49:35
Funciona perfectamente. Gracias a ZayDun y nlsgarcia por su colaboración.