1

I found some sample code like below, for trying to customize a scrollbar color:

HBRUSH CMainFrame::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor); if(nCtlColor==CTLCOLOR_SCROLLBAR) return m_brColor; return hbr; } 

I found that the following code does not work:

procedure TForm1.WMCTLColor(var msg: TWMCTLCOLOR); message WM_CTLCOLOR; 

How can I do it in Delphi?

2 Answers 2

2

There's no WM_CTLCOLOR message in the native api. Instead you can use CN_CTLCOLORSCROLLBAR control notification, which is send to child controls by the VCL in response to the API's WM_CTLCOLORSCROLLBAR.

type TScrollBar = class(TScrollBar) protected procedure WMCtlColor(var Message: TWMCtlColorScrollbar); message CN_CTLCOLORSCROLLBAR; end; procedure TScrollBar.WMCtlColor(var Message: TWMCtlColor); begin Message.Result := CreateSolidBrush(RGB(255, 255, 0)); end; 


Or, if you don't want to derive a new control, provided the scrollbar is placed on the form:

 TForm1 = class(TForm) ... protected procedure WMCtlColorScrollbar(var Message: TWMCtlColorScrollbar); message WM_CTLCOLORSCROLLBAR; ... end; procedure TForm1.WMCtlColorScrollbar(var Message: TWMCtlColorScrollbar); begin if Message.ChildWnd = ScrollBar1.Handle then Message.Result := CreateSolidBrush(RGB(255, 255, 0)); end; 
Sign up to request clarification or add additional context in comments.

7 Comments

I've try, but it doesn't work. Does it only work when my form has the TScrollBar? My demo code only has a TMemo with ScrollBar set to ssBoth
Clearly you need to attach the code to the right window! I can't see any mention of TMemo in the question. How did you expect us to know about that?
@Leo - David is right, the code you give in the question is for a scrollbar control, which you can find in the standard component tool palette of the IDE. A memo with scrollbars is a window with WS_VSCROLL and WS_HSCROLL styles, there're no separate scrollbar controls in the memo. Neither the code you gave in the question, or the code in this answer will work for such a case.
Sorry about the misleading, and i though the case should be simple enough to state my question, and i just wanna know if the WM_CTLCOLOR work for normal controls in Delphi, such as TMemo or TStringGrid or any other one with built-in scrollbars. And seems that the Windows Message only work for TScrollBar in Delphi, and then thank you two guys
@David, i think you're understanding me that i'm trying to find a way to custom the color of the scrollbars of a generic controls in Delphi, such as TMemo, TStringGrid, TListView or TListBox, etc. I don't want to disable the built-in scrollbar and replace with mine, but just want to custom the built-in one, but so far, i got nothing
|
1

This improvement avoids the memory leak by the repeated calling of CreateSolidBrush()

{ TMyScrollBar } //****************************************************************************** constructor TMyScrollBar.Create(AOwner: TComponent); begin inherited; FHBrush := CreateSolidBrush(ColorToRGB(FBackColor)); end; //****************************************************************************** destructor TMyScrollBar.Destroy; begin DeleteObject(FHBrush); inherited; end; //****************************************************************************** procedure TMyScrollBar.SetBackColor(const Value: Tcolor); begin FBackColor := Value; DeleteObject(FHBrush); FHBrush := CreateSolidBrush(ColorToRGB(FBackColor)); end; //****************************************************************************** procedure TMyScrollBar.WMCtlColor(var Message: TWMCtlColorScrollbar); begin Message.Result := FHBrush; end; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.