title: How to correctly respond to focus messages in a custom control?
body: <p>I need to create my own panel which will be derived from <code>TCustomPanel</code> - Here I will be doing my own custom painting on the panel such as drawing a header at the top.</p>
  
  <p>One of the requirements I need is that I need to be able to draw two different colors depending on whether my panel has focus or not, I also need a way of knowing whether or not a child control inside my panel has focus too - of course though there is no knowing of what the child contents could be.</p>
  
  <p>So for example, if the panel (or any child controls inside the panel) has focus, then the color could be <code>clSkyBlue</code>. If the panel (or none of the child controls inside the panel) does not have focus, then the color could be <code>clWhite</code>.</p>
  
  <p>Here is a quick layout of the custom panel: (to keep it simple for the example there is no header drawing, just basic color changing)</p>
  
  <pre><code>type
    TMyPanel = class(TCustomPanel)
    private
      //
    protected
      // procedure Paint; override;
  
      procedure WMMouseDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;        
      procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
      procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
    public
      constructor Create(AOwner: TComponent); override;
      destructor Destroy; override;
    end;
  
  constructor TMyPanel.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
  
    Self.ParentBackground := False;
    Self.ParentColor := False;
  end;
  
  destructor TMyPanel.Destroy;
  begin
    inherited Destroy;
  end;
  
  procedure TMyPanel.WMMouseDown(var Message: TWMLButtonDown);
  begin
    Self.SetFocus;
  end;
  
  procedure TMyPanel.WMKillFocus(var Message: TWMKillFocus);
  begin
    Self.Color := clWhite;
    Invalidate;
  end;
  
  procedure TMyPanel.WMSetFocus(var Message: TWMSetFocus);
  begin
    Self.Color := clSkyBlue;
    Invalidate;
  end;
  </code></pre>
  
  <p>The first problem I ran into is making the panel focusable, I'm sure there will be a proper solution that I have overlooked but in this case I used a dirty trick by intercepting the <code>WM_LBUTTONDOWN</code> message and making the panel focused. This of course also denies the other requirement I mentioned before where child controls also decide what color the panel should be depending on whether or not the panel or it's child controls are focused.</p>
  
  <p>This is what happens when I click on my panel:</p>
  
  <p><img src="https://i.stack.imgur.com/8j9Jn.png" alt="enter image description here"></p>
  
  <p>This is what happens when clicking on the child control:</p>
  
  <p><img src="https://i.stack.imgur.com/ncWxF.png" alt="enter image description here"></p>
  
  <p>As you can see the panel turned white, but I need it to turn to <code>clSkyBlue</code> to indicate that the panel or it's child contents has focus, which should look like:</p>
  
  <p><img src="https://i.stack.imgur.com/Nau5n.png" alt="enter image description here"> </p>
  
  <p>So, what is the solution to correctly identify whether or not my custom control or it's child controls has the focus or not?</p>
  
  <p>I had thought about iterating through each child control inside the panel to determine if it had focus or not but I am not sure how I would fire such an event in the first place as clicking directly on a child control would surely ignore any messages that the custom panel is waiting to intercept.</p>