Autor Beitrag
Shasta2103
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25

Win7
Delphi XE2
BeitragVerfasst: Do 18.01.07 23:41 
Hi,

ich habe neulich gedacht, dass die PageControl Komponente eigentlich gar nicht so toll ausschaut und habe sie daraufhin optisch verändert. Das hat auch alles wunderbar funktioniert.
Nur leider ergab sich nun das Problem, dass besonders der Inhalt der Tabsheets flackert, d.h. ich habe in den Tabsheets
eigene Komponenten (TGraphicControl) abgelegt. Diese würden normalerweise bei doublebuffered:=true nicht mehr flackern, aber wie es nun mal so ist, tun sie das dennoch :( , trotz dass ich für die Tabsheets und das PageControl eingestellt habe.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
unit EBPageControl;

interface

uses Windows, Controls, Classes, Graphics, ComCtrls, Messages;

type
  TTabSheet = class(ComCtrls.TTabSheet)
  private
    FColor: TColor;
    procedure SetColor(Value: TColor);
    procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd);
      message WM_ERASEBKGND;
  protected
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Color: TColor read FColor write SetColor;    
  end;

  TEBPageControl = class(ComCtrls.TPageControl)
    private
    protected
      procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
      procedure DoOnChange(Sender: TObject);
    public
      constructor Create(AOwner: TComponent); override;
    published
      property OnClick;
      property OnDblClick;
      property OnMouseMove;
      property OnMouseUp;
      property OnMouseDown;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TEBPageControl]);
end;


procedure DrawGradient(const Canvas: TCanvas; Color1, Color2: TColor; Rect: TRect);
var
 Y, R, G, B: Integer;
 RColor1, GColor1, BColor1: Integer;
 RColor2, GColor2, BColor2: Integer;
begin
 Color1:=ColorToRGB(Color1);
 Color2:=ColorToRGB(Color2);

 RColor1:=GetRValue(Color1);
 GColor1:=GetGValue(Color1);
 BColor1:=GetBValue(Color1);

 RColor2:=GetRValue(Color2);
 GColor2:=GetGValue(Color2);
 BColor2:=GetBValue(Color2);

 Canvas.Pen.Width:=1;
 Canvas.Pen.Style:=psInsideFrame;

 for Y:=Rect.Top to Rect.Bottom-1 do
 begin
   R:=Round(RColor1 + ((RColor2 - RColor1) * Y / (Rect.Bottom - Rect.Top)));
   G:=Round(GColor1 + ((GColor2 - GColor1) * Y / (Rect.Bottom - Rect.Top)));
   B:=Round(BColor1 + ((BColor2 - BColor1) * Y / (Rect.Bottom - Rect.Top)));

   Canvas.Brush.Color:=RGB(R, G, B);
   Canvas.FillRect(Classes.Rect(Rect.Left, Y, Rect.Right, Y+1));
 end;
end;





//******************************************************************************
constructor TTabSheet.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);

  Color:=clSilver;
  Doublebuffered:=true;
end;

destructor TTabSheet.Destroy;
begin
  inherited Destroy;
end;

procedure TTabSheet.SetColor(Value: TColor);
begin
  if FColor <> Value then
  begin
    FColor := Value;
    Invalidate;
  end;
end;

procedure TTabSheet.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
begin
  if FColor = clBtnFace then
    inherited
  else
  begin
    Brush.Color := FColor;
    Windows.FillRect(Msg.dc, ClientRect, Brush.Handle);
    Msg.Result := 1;
  end;
end;




constructor TEBPageControl.Create(AOwner: TComponent);
begin
  inherited;
  Self.OwnerDraw:=true;
  Self.DoubleBuffered:=true;
  OnChange:=self.DoOnChange;
end;

procedure TEBPageControl.DoOnChange(Sender: TObject);
begin
  Invalidate;
end;

procedure TEBPageControl.WMPaint(var Message: TWMPaint);
var R: TRect;
    a, x,y: integer;
    AText: String;
begin
  inherited;

  R.Left:=0;
  R.Top:=0;
  R.Right:=Width;
  R.Bottom:=Height;
  DrawGradient(Canvas, $e0e0e0, $9f9f9f, R);
  Canvas.Pen.Style:=psSolid;
  Canvas.Pen.Color:=clBlack;
  Canvas.FrameRect(R);

  Canvas.Font.Assign(Self.Font);

  for a:=0 to (Self.PageCount-1) do
  begin
    R:=TabRect(a);
    R.Left:=R.Left + 2;
    R.Top:=R.Top - 1;
    R.Right:=R.Right-1;
    R.Bottom:=R.Bottom + 4;

    if (Self.ActivePageIndex = a) then
    begin
      DrawGradient(Canvas, $8f8f8f, $505050, R);
    end
    else
    begin
      DrawGradient(Canvas, $e0e0e0, $9f9f9f, R);
    end;

    AText:=Self.Pages[a].Caption;
    x:=(R.Right - R.Left) div 2 - Canvas.TextWidth(AText) div 2;
    y:=(R.Bottom - R.Top) div 2 - Canvas.TextHeight(AText) div 2;

    if (Self.ActivePageIndex = a) then Canvas.Font.Color:=clWhite
    else Canvas.Font.Color:=clBlack;

    Canvas.Brush.Style:=bsClear;
    Canvas.TextRect(R, R.Left + x, R.Top + y, AText);

    Canvas.Pen.Style:=psSolid;
    Canvas.Pen.Color:=clGray;
    Canvas.MoveTo(R.Right, R.Top);
    Canvas.LineTo(R.Right, R.Bottom);

    Canvas.Pen.Color:=clDkGray;
    Canvas.MoveTo(R.Left, R.Bottom-1);
    Canvas.LineTo(R.Right+1, R.Bottom-1);
  end;
end;

end.


Ich würde mich über einen weisen Rat freuen.