Delphi XE2 VCL styles, How to disable VCL styles on TBitBtn? - delphi

I am using the new VCL styles system in Delphi XE2 and its work fine but on one Form I want exception. This Form contains number of TBitBtn control and each TBitBtn control has its own Font colour (clRed, clBlue, clLime etc) different from other.
Due to Style implementation all TBitBtn control’s Caption is display in black colour instead of set colour.
Is there any TStyleHook, which can be register on TBitBtn control, which disabled the Style on TBitBtn Control on that form?

The TBitBtn component doesn't use a vcl style hook, this control use the TButtonGlyph class (which is defined and implemented in the implementation part of the Vcl.Buttons unit) to draw the button using the Windows theme or the current vcl style, this class (TButtonGlyph) is not accessible outside of this unit , so you are out of luck here.
The only option which comes to my mind is create a interposer class and intercept the CN_DRAWITEM message for the TBitBtn control and then execute your own code to draw the button.
TBitBtn = class(Vcl.Buttons.TBitBtn)
private
procedure MyDrawItem(const DrawItemStruct: TDrawItemStruct);
public
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
end;
procedure TBitBtn.CNDrawItem(var Message: TWMDrawItem);
begin
MyDrawItem(Message.DrawItemStruct^);
end;
procedure TBitBtn.MyDrawItem(const DrawItemStruct: TDrawItemStruct);
begin
//the new code goes here.
end;

Related

MDI-Form ignores StyleElements seClient

i have a problem with the VCL-Styles and MDI-Form. I want to use the VCL Styles, but i also want to draw the background (image) of my MainForm (MDI) by myself. This worked fine without VCL Styles, but when a Style is active the background image of the MainForm isn't shown.
I checked out the StyleElements for the MainForm, but exclude the seClient is ignoerd and the background image isn't shown.
When i exclude the seClient and seBoarder the image is shown again. Obviously the Form Boarder lost the Style, which is also not that what i want.
The image is drawn at the Canvas in the ClientWndProc by the messages WM_ERASEBKGND, WM_VSCROLL and WM_HSCROLL. With the Styles, it looks like this events didn't raise. Is there any way the get the image at the form background with VCL Styles active?
The main point to realize here is that form styled fsMDIForm is a very special TWinControl that manages two window handles instead of one - TWinControl.Handle and TForm.ClientHandle. While the first handle is the form window itself the second is MDI client window (container-like for MDI child windows inside MDI parent).
TFormStyleHook hooks both window procedures and introduces new method TFormStyleHook.MDIClientWndProc, which processes messages sent to MDI client. This method luckily virtual. It does some pre-processing of messages and then calls the original hooked procedure. The sad part is that it prevents calling the old procedure for WM_NCACTIVATE, WM_NCCALCSIZE, WM_NCPAINT and WM_ERASEBKGND. Even worse is that on WM_ERASEBKGND it paints the client area background directly using StyleServices.
Thanks to the above the subclassing of TFormStyleHook for MDI forms a PITA. I see multiple design flaws here:
Missing virtual TFormStyleHook.PaintMDIClientBackground similar to TFormStyleHook.PaintBackground.
No way to control/access over the original MDI client proc without hacking (hidden in private field FMDIPrevClientProc).
Disability to control styling of MDI client window via TForm.StyleElements (as noted by OP).
So what is the workaround? The easiest I can see is creating a custom style hook:
type
TMainFormStyleHook = class(TFormStyleHook)
public
procedure MDIClientWndProc(var Message: TMessage); override;
end;
{ TMainFormStyleHook }
procedure TMainFormStyleHook.MDIClientWndProc(var Message: TMessage);
begin
if Message.Msg = WM_ERASEBKGND then
begin
{ TODO: Paint background to TWMEraseBkgnd(Message).DC }
Message.Result := 1;
end
else
inherited;
end;
and applying it to your MDI parent:
type
TMainForm = class(TForm)
private
class constructor Create;
class destructor Destroy;
{ ... }
end;
{ TMainForm }
class constructor TMainForm.Create;
begin
TCustomStyleEngine.RegisterStyleHook(TMainForm, TMainFormStyleHook);
end;
class destructor TMainForm.Destroy;
begin
TCustomStyleEngine.UnRegisterStyleHook(TMainForm, TMainFormStyleHook);
end;
Note that you still need to keep painting background in MDI parent form in case the VCL styles are disabled, so it's worth creating method TMainForm.PaintMDICLientBackground(DC: HDC) and call it from both places.
I would argue that this is a bug in VCL. How about you guys?

Delphi VCL styles problem with TListview EditCaption and HideSelection

I'm using Delphi 10.3 in a VCL app with a Dark VCL style (default Windows10 Dark for example).
I have two problems with VCL styling in TListview
When editing an item in TListview, the listview's edit is not styled: it has a white background with black text. It there any way to apply the VCL style to that edit although it not a VCL control?
Also, with HideSelection = False and the listview not having focus, the selected item's background color is the default light gray color instead of a much darker color that would better fit in the VCL style. Is there any way to modify that other than custom drawing?
I checked this with several dark VCL styles, they all have these problems with TListView (and TTreeview too).
I was able to fix the listview's edit background and text color by overriding message handler WM_CTLCOLOREDIT in a TListView descendant (TListViewEx in my case) like below. If you check the VCL source for TCustomListView.WMCtlColorEdit you'll see this fix does not cover the case glassPaint case correct - but I'm not using that. It seems to me this is actually a VCL bug and the below code should have been included in TCustomListView.WMCtlColorEdit.
Note: the same type of fix also works for TTreeview (using a TTreeview descendant obviously)
I have not found a workaround for the unfocused selection color yet.
procedure TListViewEx.WMCtlColorEdit(var Message: TMessage);
var
DC: HDC;
begin
if StyleServices.IsSystemStyle then
inherited
else
begin
DC := Message.WParam;
SetTextColor(DC, StyleServices.GetSystemColor(clWindowText));
SetBkColor(DC, StyleServices.GetSystemColor(clWindow));
Message.Result := 1;
end;
end;

Form background color with VCL Styles?

I am experimenting with VCL Styles. This might be a silly question, but is it possible to have forms with different backgrounds when using a style? It seems that the form background (client area) is specified in the VCL style designer and it overrides the form's Color property.
How can I achieve forms with different background color? For example, I want my modal dialogs have a different background color than the main form.
Yes it is possible :
if you are using Delphi XE3,XE4,XE5 :
you only need to remove seClient from the StyleElements property of your form :
Form3.StyleElements := [seFont, seBorder];
if you are using delphi xe2:
you should override the TFormStyleHook class ,and catch the WM_ERASEBKGND message , and return without processing the default message :
type
TFormStyleHookEx = class(TFormStyleHook)
procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
end;
{ TFormStyleHookEx }
procedure TFormStyleHookEx.WMEraseBkgnd(var Message: TMessage);
begin
Message.Result := 1;
end;
initialization
TStyleEngine.RegisterStyleHook(TForm3, TFormStyleHookEx);

Is there a way to make TRadioButton be Transparent?

I'm using delphi 2010
I agree with Andreas and Serg in that the control is transparent when themes are enabled.
I, once, had tried to make the CheckBox transparent for when runtime themes are not enabled in project options, or a classic theme is selected with the OS; the result was not perfect. The below is the same code applied to the RadioButton.
Problems easily noticable are, as you would guess from the code, it's a bit flickery and it is not transparent when DoubleBuffered. A problem not easily noticable can (sometimes) be duplicated by bringing a different window in front of the form containing the controls, and then slowly moving it aside, sometimes this leaves some artifacts.
Well, anyway, here it is;
type
TMyRadioButton = class(TRadioButton)
private
procedure CnCtlColorStatic(var Msg: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
procedure WmEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WmPaint(var Msg: TWMNCPaint); message WM_PAINT;
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
implementation
uses
themes;
procedure TMyRadioButton.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
end;
procedure TMyRadioButton.WmPaint(var Msg: TWMNCPaint);
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered) then
InvalidateRect(Handle, nil, True);
inherited;
end;
procedure TMyRadioButton.WmEraseBkgnd(var Msg: TWMEraseBkgnd);
var
R: TRect;
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered)
and (Parent <> nil) then begin
R := Rect(Left, Top, Left + Width, Height + Top);
InvalidateRect(Parent.Handle, #R, True);
UpdateWindow(Parent.Handle);
Msg.Result := 1;
end else
inherited;
end;
procedure TMyRadioButton.CnCtlColorStatic(var Msg: TWMCtlColorStatic);
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered) then begin
SetBKMode(Msg.ChildDC, TRANSPARENT);
Msg.Result := GetStockObject(NULL_BRUSH);
end else
inherited;
end;
Quote Remy Lebeau (TeamB):
TLabel is a TGraphicControl
descendant, and thus has to do all of
its own drawing manually, so it can
implement transparency as needed.
TCheckBox and TRadioButton, on the
other hand, are TWinControl
descendants that wrap standard Win32
API controls, and thus are subject to
whatever capabilities the OS supports
for them (transparency is not one of
them).
https://forums.codegear.com/thread.jspa?threadID=24027&tstart=375
You either need to do some heavy overriding, or else you will need to use a third party component...
A simple trick: make the button color white, shrink it to the minimum size, only the button; and put a transparent label behind it.
Otherwise, to make a button really transparent you need to owner draw it. You may find some examples in the web.
I found some information on responding to the WM_CTLCOLOR message. But I gave a quick try but couldn't quite get it to work.
I experimented with the standard VCL TRadioButton control in Delphi 2009 (I suppose Delphi 2010 is the same).
If you compile the project with runtime themes enabled (Project->Options->Application->Enable Runtime Themes), the TRadioButton control is transparent and its 'Color' property ignored. If the runtime themes disabled, the TRadioButton control is not transparent and its background is defined by its 'Color' property.
So I assume that the standard VCL TRadioButton (and the underlying windows control) is made transparent by the Windows theme, not by the control itself. You can switch off the theme support on application level, and in that case you get a non-transparent radio button. If you need a transparent radiobutton with runtime themes disabled, use 3rd party custom radiobutton (TCustomControl descendant, not a standard Windows radiobutton wrapper)
The easiest way is to buy a component set like Raize Components which will do this for you and lots more besides. Raize in particular allows you to customize lots of aspects of the UI.
http://www.torry.net/quicksearchd.php?String=transparent+radiobutton&Title=No might help. None of those are D2010 or D2009, but I believe porting would be possible.

Making a TPageControl flat in Delphi 7

I don't know whether this question can be answered here, but I hope it will.
I wrote a simple text editor in Delphi 7 that serves as my primary IDE for writing C code under Windows. I run Windows in a VM and I needed something light.
In any case, it uses a TpageControl that gets a new tab whenever you open or create a new file. Pretty standard.
Now, the TPageControl under Delphi has no flat property.
NO I don't mean setting the tab style to tsButtons or tsFlatButtons
the borders cannot be set to "none" and it looks pretty bad when you add a text editor into the tab control.
Is there any way to make a TpageControl flat?
EDIT:
On an open source PageControl that supports flat here's what I found:
procedure TCustomTabExtControl.WndProc(var Message: TMessage);
begin
if(Message.Msg=TCM_ADJUSTRECT) and (FFlat) then
begin
Inherited WndProc(Message);
Case TAbPosition of
tpTop : begin
PRect(Message.LParam)^.Left:=0;
PRect(Message.LParam)^.Right:=ClientWidth;
PRect(Message.LParam)^.Top:=PRect(Message.LParam)^.Top-4;
PRect(Message.LParam)^.Bottom:=ClientHeight;
end;
tpLeft : begin
PRect(Message.LParam)^.Top:=0;
PRect(Message.LParam)^.Right:=ClientWidth;
PRect(Message.LParam)^.Left:=PRect(Message.LParam)^.Left-4;
PRect(Message.LParam)^.Bottom:=ClientHeight;
end;
tpBottom : begin
PRect(Message.LParam)^.Left:=0;
PRect(Message.LParam)^.Right:=ClientWidth;
PRect(Message.LParam)^.Bottom:=PRect(Message.LParam)^.Bottom-4;
PRect(Message.LParam)^.Top:=0;
end;
tpRight : begin
PRect(Message.LParam)^.Top:=0;
PRect(Message.LParam)^.Left:=0;
PRect(Message.LParam)^.Right:=PRect(Message.LParam)^.Right-4;
PRect(Message.LParam)^.Bottom:=ClientHeight;
end;
end;
end else Inherited WndProc(Message);
end;
The thing is when I tried something similar on the main application it won't work. It won't even compile.
When the tabs are drawn as buttons, no border is drawn around the display area, so set the Style property to tsButtons or tsFlatButtons. (For non-VCL programmers, this is equivalent to including the tcs_Buttons window style on the tab control.)
An alternative is to use a TNotebook. It holds pages, but it doesn't do any painting at all. You'd have to provide the tabs yourself, such as by setting the tab control's height equal to the height of the tabs, or by using a TTabSet. (TTabSet is available in Delphi 2005; I'm not sure about Delphi 7.)
Regarding the code you found, it would be helpful if you indicated why it doesn't compile, or if you gave a link to where you found it, since I suppose the compilation error was because it refers to fields or properties of the custom class rather than the stock one. Here's what you can try to put it in your own code, without having to write a custom control.
Make two new declarations in your form like this:
FOldTabProc: TWndMethod;
procedure TabWndProc(var Msg: TMessage);
In the form's OnCreate event handler, assign that method to the page control's WindowProc property:
FOldTabProc := PageControl1.WindowProc;
PageControl1.WindowProc := TabWndProc;
Now implement that method and handle the tcm_AdjustRect messsage:
procedure TForm1.TabWndProc(var Msg: TMessage);
begin
FOldTabProc(Msg);
if Msg.Msg = tcm_AdjustRect then begin
case PageControl1.TabPosition of
tpTop: begin
PRect(Msg.LParam)^.Left := 0;
PRect(Msg.LParam)^.Right := PageControl1.ClientWidth;
Dec(PRect(Msg.LParam)^.Top, 4);
PRect(Msg.LParam)^.Bottom := PageControl1.ClientHeight;
end;
end;
end;
end;
You can fill in the other three cases if you need them. Tcm_AdjustRect is a message identifier declared in the CommCtrl unit. If you don't have that message in that unit, declare it yourself; its value is 4904.
I suspect this doesn't stop the control from drawing its borders. Rather, it causes the contained TTabSheet to grow a little bigger and cover up the borders.
I'm using Delphi XE8 and the following seems to do the trick:
ATabControl.Tabs.Clear;
ATabControl.Style := TTabStyle.tsFlatButtons;
ATabControl.Brush.Color := clWhite;
You could always use a commercial solution. I would strongly recommend Raize components, which support flat TPageControls with tabs. The component set is very easy to work with, and supports numerous visual enhancements which in my opinion give a better feel to any application.
(source: raize.com)
Drop two TPageControls, one with tabs as Tabs, with a global height equal to the tabs, and one with flatbuttons and Tabvisible properties set to false, which would be aligned under the first one. Then make sure the tab change on the first TPagecontrol makes the tabs also change in the second one.

Resources