title: Delphi: Changing the Button Color using a Class Helper
body: <p>I need to change the visual style of my delphi form controls inorder to show them from a .Net environment. To do this, I need to change the colors of delphi controls to blue ($00FCF5EE). I have used "TButton" controls widely which doesn't have a  "Color" property.So, instead of changing the buttons to speed buttons, I have tried a different approach by introducing a parent form and inheriting all the other forms from this parent form. In the parent form, I have a class helper to change the color of buttons. Here is the code: (I am using Delphi 2007)  </p>
  
  <pre><code>TButtonHelper=class helper for TButton
  private
    procedure doChangeColor;
  public
    procedure DrawChangeColor;
  end;
  
  TParentForm = class(TForm)
  public
    procedure AfterConstruction; override;
  end;
  </code></pre>
  
  <p>And in the implementation section, I have</p>
  
  <pre><code>    procedure TButtonHelper.doChangeColor;
      var
        SaveIndex: Integer;
        FCanvas:TCanvas;
        rect:TRect;
      begin
        if csDestroying in ComponentState then exit;
        FCanvas:=TCanvas.Create;
  
          SaveIndex := SaveDC(Self.Handle);
          FCanvas.Lock;
          try
            FCanvas.Handle := Handle;
            FCanvas.Font := Font;
            FCanvas.Brush := self.Brush;
            FCanvas.Brush.Color:=$00FCF5EE;
            FCanvas.FillRect(BoundsRect);//Omitting the code to draw the text
          finally
            FCanvas.Handle := 0;
            FCanvas.Unlock;
            RestoreDC(handle, SaveIndex);
            FCanvas.Free;
          end;
      end;
  
  
      procedure TButtonHelper.DrawChangeColor;
      begin
          doChangeColor;
          self.Repaint;
      end;
  
  procedure TParentForm.AfterConstruction;
      var
       i : Integer;
       wc: TControl;
      begin
        inherited;
        for i := 0 to self.ControlCount - 1 do begin
           wc:=Controls[i];
           if wc is  TButton then
              TButton(wc).DrawChangeColor;
        end;
      end;
  </code></pre>
  
  <p>But this doesn't work. Although the doChangeColor method is executed, it is not changing the color of the button.Please let me know what I am missing here.</p>
  
  <p>Thanking you all,</p>
  
  <p>Pradeep</p>