Change a TSTATUSPANEL color after the initial program is loaded in Delphi?

I have a program with a status bar containing three elements at the bottom. I want to redraw the second element with a different font color to indicate that the user is valid. My question is The first part is:

a) How to redraw the text as the default text in a different color?

And b) How to draw text in different colors after triggering an event (for example, pressing a button)?

My current code is as follows. I am trying to check if the conditions for redrawing the panel in a different color are correct (CurrentUser.Valid, which is a boolean), and then try to recolor the text. This currently does not work .

procedure TChatFormMain.sbarMainDrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
var
RectForText: TRect ;
begin
if (Panel = StatusBar.Panels[1]) and (CurrentUser.Valid) then
begin
sbarMain.Canvas.Font.Color := clGreen;
RectForText := Rect;
StatusBar.Canvas.FillRect(RectForText);
DrawText(sbarMain.Canvas.Handle, PChar(Panel.Text), -1, RectForText,
DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

In another part of the code, I am calling StatusBar.repaint; try to redraw the bar graph with the new text color, this is Is it correct?

Update MCVE:

unit colourStatusU;

interface

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

type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure StatusBar1DrawPanel (StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect);
private
{Private declarations }
public
{Public declarations }
end ;

var
Form1: TForm1;
valid: boolean;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
valid := true;
StatusBar1.Repaint;
end;

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRec t);
var
RectForText: TRect;
begin
if (Panel = StatusBar.Panels[1]) and (Valid) then
begin
Canvas.Font.Color := clGreen;
RectForText := Rect;
Canvas.FillRect(RectForText);
DrawText(Canvas.Handle, PChar(Panel.Text), -1, RectForText ,
DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

end.

The OnDrawPanel event is only called for panels whose Style is set to psOwnerDraw, so please ensure that the Style is set correctly in the Object Inspector or the code.

The canvas to be drawn must be TStatusBar.Canvas, but you are using TForm.Canvas. Canvas using the StatusBar provided by the OnDrawPanel event:

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect);
var
RectForText: TRect;
begin
if (Panel = StatusBar.Panels[1]) and (Valid) then
begin
StatusBar.Canvas.Font.Color := clGreen;
RectForText := Rect;
StatusBar.Canvas.FillRect(RectForTex t);
DrawText(StatusBar.Canvas.Handle, PChar(Panel.Text), -1, RectForText, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

Calling Repaint() or Invalidate() to force the status bar to redraw its drawing is the method to be used.

I have a program, and there is a program at the bottom that contains three The status bar of two elements. I want to redraw the second element with a different font color to indicate that the user is effective. The first part of my question is:

a) How to use a different color Redraw the text as the default text?

And b) How to draw text in different colors after triggering an event (for example, pressing a button)?

My current code is as follows. I am trying to check if the conditions for redrawing the panel in a different color are correct (CurrentUser.Valid, which is a boolean), and then try to recolor the text. This currently does not work .

procedure TChatFormMain.sbarMainDrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
var
RectForText: TRect ;
begin
if (Panel = StatusBar.Panels[1]) and (CurrentUser.Valid) then
begin
sbarMain.Canvas.Font.Color := clGreen;
RectForText := Rect;
StatusBar.Canvas.FillRect(RectForText);
DrawText(sbarMain.Canvas.Handle, PChar(Panel.Text), -1, RectForText,
DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

In another part of the code, I am calling StatusBar.repaint; try to redraw the bar graph with the new text color, this is Is it correct?

Update MCVE:

unit colourStatusU;

interface

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

type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure StatusBar1DrawPanel (StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect);
private
{Private declarations }
public
{Public declarations }
end ;

var
Form1: TForm1;
valid: boolean;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
valid := true;
StatusBar1.Repaint;
end;

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect);
var
RectForText: TRect;
begin
if (Panel = StatusBar.Panels[1]) and (Valid) then
begin
Canvas.Font .Color := clGreen;
RectForText := Rect;
Canvas.FillRect(RectForText);
DrawText(Canvas.Handle, PChar(Panel.Text), -1, RectForText,
DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

end.

OnDrawPanel The event is only called for the panel whose Style is set to psOwnerDraw, so please ensure that the Style is set correctly in the Object Inspector or the code.

The canvas to be drawn must be TStatusBar.Canvas, but you are using It is TForm.Canvas. Canvas using StatusBar provided by OnDrawPanel event:

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect) ;
var
RectForText: TRect;
begin
if (Panel = StatusBar.Panels[1]) and (Valid) then
begin
StatusBar. Canvas.Font.Color := clGreen;
RectForText := Rect;
StatusBar.Canvas.FillRect(RectForText);
DrawText(StatusBar.Canvas.Handle, PChar(Pane l.Text), -1, RectForText, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
end;

Call Repaint() or Invalidate() to force the status bar to redraw The drawing is the method to be used.

Leave a Comment

Your email address will not be published.