Fix for TRibbon and VCL styles

by Radek Červinka 1. December 2011 00:06

If you use VCL styles from Delphi XE2 and TRibbon control (or maybe Glass window on Vista +) you can see some drawing problems. But there is a way to modify how can VCL draw element and you can use this.

For me there is a problem with drawing on NonClient (NC) area of form. When style is used, NC area is painted by VCL and not by Windows (maybe this can apply for glass windows too).

TRibbon and style

Basic of style painting is class TStyleHook from unit Vcl.Themes and it's descendant TFormStyleHook. In base class there is a property OverridePaintNC, or PaintNC(Canvas: TCanvas) method for painting on NC area.

So little class:

unit uPatch;

interface

uses
  VCL.Graphics, VCl.Controls, Vcl.Forms;

type
  TMyStyleHookClass= class(TFormStyleHook)
  protected
    procedure PaintNC(Canvas: TCanvas); override;
  end;

implementation
procedure TMyStyleHookClass.PaintNC(Canvas: TCanvas);
begin
  OverridePaintNC := False;
end;

end.

In PaintNC set OverridePaintNC to false (tried in constructor, but this is better).

Remark: in VCL is called in this way:

procedure TStyleHook.WMNCPaint(var Message: TMessage);
var
  Canvas: TCanvas;
begin
  if FOverridePaintNC then
  begin
    Canvas := TCanvas.Create;
    try
      Canvas.Handle := GetWindowDC(Control.Handle);
      PaintNC(Canvas);
    finally
      ReleaseDC(Handle, Canvas.Handle);
      Canvas.Handle := 0;
      Canvas.Free;
    end;
    Handled := True;
  end;
end;

And now VCL will use our class instead of original:

 TStyleManager.Engine.UnRegisterStyleHook(TCustomForm, TFormStyleHook);//unregister the original style hook
 TStyleManager.Engine.RegisterStyleHook(TCustomForm, TMyStyleHookClass); //register the new style hook

Fix: TCustomForm not TForm

So TRibbon and VCL styles can work together.

fixed TRibbon and style

Tags: ,

How to

Comments

12/17/2011 5:28:09 AM #

pingback

Pingback from theroadtodelphi.wordpress.com

Exploring Delphi XE2 – VCL Styles Part II « The Road to Delphi – a Blog about programming

theroadtodelphi.wordpress.com |

1/7/2012 8:58:25 PM #

john

great site

john |

Comments are closed