Solved

Capturing and modifying the WM_CTLCOLORBTN

Posted on 2003-11-03
14
507 Views
Last Modified: 2008-03-10
I would like to change the default color of the buttons in my Delphi app.  I know that I can call the SetSysColor and GetSysColor, however, I only want to modify my app.  I was thinking that I could capture the WM_CTLCOLORBTN or the COLOR_BTNFACE somehow and modify only the window and child windows of the app.  I would like to do this for both buttons and scroll bars. I am using Delphi 7 Pro.

Thanks,
Derrick
0
Comment
Question by:dbetts
  • 8
  • 4
14 Comments
 
LVL 33

Expert Comment

by:Slick812
ID: 9691309
hello dbetts, , the WM_CTLCOLORBTN has NO effect on a standard TButton, but it may have an effect on an Owner Draw button, like a TBitBtn,  It may change the text color, but it does not change the background color, you may be better off if you go to a component web site like Torry and just  download a "Color Button" component, thses many many of them, but you can also do it in code, scroll bars are a different story, and I do not see how you group them together
0
 

Author Comment

by:dbetts
ID: 9691352
Good thought, however, I am using a component from TMS (TMSGridPack) which is pretty good, however, it doesn't allow me to change the background clolor of the buttons, nor does it allow me to change the color of the scroll bars.  The needs I have basically require me to continue using the TMS components, so I was hoping for a way to change the way my app drew the buttons and scrollbars in the TMS components by trapping a windows message and returning the components with my desired color.  Currently, the windows system colors are being used and they often clash with the other colors and etc. I am using in the Application.

Thanks for your input.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9692038
I know Nothing about the TMSGridPack, so I can only generalize about some TButton stuff, However this TMSGridPack may not be derived from a TButton? ? Any way, The windows system is Very reluctant to change ANY drawing aspect of a BUTTON Class, you can change the Font on it but not much else, however these is an Owner Draw option for a BUTTON class, which allows you to draw absolutely ANYTHING you want on the button. . . If you are stuck using this TMSGridPack, then you should contack the makers of this component and ask them about it. . . but it is unlikely that it can be done, if it is Not in it's methods already. . .
as to scroll bars, this is a different thing, unrelated to buttons, I do not think that there are Owner draw Scroll bars, but there is a
WM_CTLCOLORSCROLLBAR message that can change the Shaft color, but NOT the scroll and up and down buttons, besides in win XP these are colored by the XP theme anyway. . . You may not get much help for doing what you say you want to do. . . I seem to be the only one to comment here
0
 

Author Comment

by:dbetts
ID: 9692108
I appreciate your time.  This is what I have so far.  I AM capturing the message that is sent when the button is drawn:


protected
    procedure CatchtIt(var Msg: TMessage); message WM_CTLCOLORBTN;
...
...

procedure TForm1.CatchtIt(var Msg: TMessage);
begin
  SetBkColor(Msg.WParam, RGB(0,0,220));   //These parts aren't doing anything for me.  NO CHANGES
  SetTextColor(Msg.WParam, RGB(0,0,220));//These parts aren't doing anything for me.  NO CHANGES
end;

However, in the Win32 Developer's Reference in Delphi it states:

Windows provides default color values for buttons. The system sends a WM_CTLCOLORBTN message to a button's parent window before the button is drawn. This message contains a handle of the button's device context and a handle of the child window. The parent window can use these handles to change the button's text and background colors. The following table shows the default button-color values.

Value                           Element colored
COLOR_BTNFACE           Button faces.
COLOR_BTNHIGHLIGHT    Highlight area (the top and left edges) of a button.
COLOR_BTNSHADOW           Shadow area (the bottom and right edges) of a button.
COLOR_BTNTEXT           Regular (nongrayed) text in buttons.
COLOR_GRAYTEXT           Disabled (gray) text in buttons. This color is set to 0 if the current display driver does not support a solid gray color.
COLOR_WINDOW           Window backgrounds.
COLOR_WINDOWFRAME  Window frames.
COLOR_WINDOWTEXT    Text in windows.

So according to this it should be possible...I just don't know how.
Thanks for looking into it for me Slick812!
0
 

Author Comment

by:dbetts
ID: 9692122
Perhaps this is my problem.  This came from the Win32 help in Delphi for the WM_CTLCOLORBTN:

Return Values:
If an application processes this message, it must return the handle of a brush. Windows uses the brush to paint the background of the button.

How do I return the handle of a brush based on the code I have in my previous note?  What is a brush handle?  (P.S. I have increased the point value).

Thanks.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9692232
Look, I have already explained, and I have read that API for that a hundred times,

 IT WILL NOT CHANGE ANY THING ON A STANDARD BUTTON, I do not care what it says. . . It will do all of that if the button is OWNER DRAWN, and ONLY if the button is owner drawn
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 33

Accepted Solution

by:
Slick812 earned 100 total points
ID: 9692249
procedure TForm1.CatchtIt(var Msg: TMessage);
begin
  SetBkColor(Msg.WParam, RGB(0,0,220));  
  SetTextColor(Msg.WParam, RGB(0,0,220)); // this will change the text color on a OWNER DRAW BUTTON

Result := GetStockObject(WHITE_BRUSH);
//The Result can be a Brush for an OWNER DRAWN BUTTON
end;
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9692257
should be
Msg.Result := GetStockObject(WHITE_BRUSH);
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9692279
What is a brush handle?

the windows system creates Brushes, to fill in colors in areas of display pixels, and to keep track of which brush is which, it gives them and Identifier called a Handle, When ever you create a brush
hBrush1 := CreateSolidBrush($FFFFA0);
you will Identify this brush to the system with
hBrush1
the handle of that brush

in delphi graphics you can use the TBrush  Handle property
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9696563
Oh, and I have also tried to convert a TButton to Owner Draw, and I could do this, However, the guys at Delphi have several code checks for a TButton, so if it's Style is changed it will change it back, so as soon as you click a converted TButton, it will go Back to being NON-owner Draw
0
 

Author Comment

by:dbetts
ID: 9696662
Okay.  Thanks for your help.  How should we do the points on this one?
0
 
LVL 33

Expert Comment

by:Slick812
ID: 9699107
you can do what is convient for you, I did not help you to get the results that you wanted, (I only did this question because no one else did), but if my comments are helpful you can do a point adjustment. . .  OR if you is one of those whos got NO points, then go to community support at

http://www.experts-exchange.com/Community_Support/

and ask for a refund, delete, close question or you can read some of the stuff there to get an idea of how to

as I said, there are a ton of Color Button components, but if you are stuck with your component, then what you got is what you get
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now