Mirror

How to change the client area of a TListBox (Views: 100)


Problem/Question/Abstract:

I have created my own listbox control as a descendant of TListBox. What I want to be able to do is to change the client area of the listbox so that I can draw a label above the list box area. I can change the client rect by overriding the CreateWnd method like this:

procedure TMyListBox.CreateWnd;
begin
  inherited CreateWnd;
  ClientHeight := Height - 20;
end;

But I can't move the client rect down (ie change the origin). Using ClientOrigin.X := 20 does not work as the ClientOrigin property is read only. I'm thinking maybe I need to override the CreateParams method to do this. Any ideas?

Answer:

You have to respond to the WM_NCCALCSIZE message:

procedure TREDCustomListBox.WMNCCalcSize(var Msg: TWMNCCALCSIZE);
begin
  inherited;
  Inc(MSG.CalcSize_Params^.rgrc[0].Top, FHeader.Height);
end;

That is a the method within my own listbox which does exactly what you want to do. Notice how I'm incrementing the client area by the height of the header (which I implemented as a separate class so I can do the same thing in other controls). It was a fun exercise.

Another clue: You have to paint the header in WM_NCPAINT

<< Back to main page