Entwickler-Ecke

Visual Component Library (VCL) - ...wie kann ich ein BitBtn in Farbe darstellen?


FriFra - Mo 14.10.02 01:09
Titel: ...wie kann ich ein BitBtn in Farbe darstellen?
wie kann ich ein BitBtn in Farbe darstellen?

Ich hab mir mal was gebastelt um BitBtn's bunt zu bekommen.
Folgende Einschränkungen sind zu beachten:

Der Code kann vielleicht noch etwas aufgeräumt werden, aber es funktioniert:

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:
procedure TForm1.SetBitBtnColors(Button: TBitBtn; BtnColor, FontColor: TColor);
var
  Bmp, Bmp2: TBitmap;
  Margin, n, m, H, W, T, Q, UT, US, UE: integer;
  RPC: TColor;
  R1, R2: TRect;
  function ApplyDark(Color: TColor; HowMuch: Byte): TColor;
  var
    r, g, b: Byte;
  begin
    Color := ColorToRGB(Color);
    r := GetRValue(Color);
    g := GetGValue(Color);
    b := GetBValue(Color);
    if r > HowMuch then
      r := r - HowMuch
    else
      r := 0;
    if g > HowMuch then
      g := g - HowMuch
    else
      g := 0;
    if b > HowMuch then
      b := b - HowMuch
    else
      b := 0;
    result := RGB(r, g, b);
  end;
  function RemQuot(Text: string): string;
  var
    n: integer;
  begin
    Result := Text;
    if pos('&', Result) > 0 then
      Result := copy(Result, 1, pos('&', Result) - 1) + copy(Result, pos('&',
        Result) + 1, Length(Result));
  end;
begin
  try
    //Marin berechnen
    Margin := BitBtn1.Margin;
    if Margin = -1 then
      Margin := trunc(Button.Width / 2) - trunc((Button.Glyph.Width +
        Button.Glyph.Canvas.TextWidth(RemQuot(Button.Caption))) / 2) + 6;

    Q := pos('&', Button.Caption);
    US := -1;
    if Q = 1 then
    begin
      US := 0;
      UE := Button.Glyph.Canvas.TextWidth(copy(RemQuot(Button.Caption), 11)) -
        1;
      UT := Button.Glyph.Canvas.TextHeight(copy(RemQuot(Button.Caption), 11))
        - 1;
    end
    else if Q > 1 then
    begin
      US := Button.Glyph.Canvas.TextWidth(copy(RemQuot(Button.Caption), 1, Q -
        1));
      UE := Button.Glyph.Canvas.TextWidth(copy(RemQuot(Button.Caption), Q,
        1)) - 1;
      UT := Button.Glyph.Canvas.TextHeight(copy(RemQuot(Button.Caption), 11))
        - 1;
    end;

    Bmp := TBitmap.Create;
    Bmp2 := TBitmap.Create;
    Bmp2.Width := Button.Glyph.Width;
    Bmp2.Height := Button.Glyph.Height;
    Bmp2.Assign(Button.Glyph);
    RPC := Bmp2.Canvas.Pixels[0, Bmp2.Height - 1];

    for n := 0 to Bmp2.Width - 1 do
    begin
      for m := 0 to Bmp2.Height - 1 do
      begin
        if Bmp2.Canvas.Pixels[n, m] = RPC then
          Bmp2.Canvas.Pixels[n, m] := BtnColor;
      end;
    end;

    Bmp.Width := Button.Width * 2 - 6;
    Bmp.Height := Button.Height - 4;
    Bmp.Canvas.Brush.Color := BtnColor;
    Bmp.Canvas.FillRect(BMP.Canvas.ClipRect);
    //streifen links ändern...
    for n := 0 to Bmp.Height - 1 do
    begin
      Bmp.Canvas.Pixels[0, n] := ApplyDark(Color, 1);
      Bmp.Canvas.Pixels[Trunc(Bmp.Width / 2), n] := ApplyDark(Color, 1);
    end;

    H := Bmp.Canvas.TextHeight(Button.Caption);
    W := Bmp.Canvas.TextWidth(RemQuot(Button.Caption));
    T := Trunc((Button.Height / 2) - (H / 2)) - 2;

    Bmp.Canvas.Font.Color := FontColor;
    Bmp.Canvas.Brush.Color := BtnColor;

    //Glyph1
    R1.Left := Margin;
    R1.Top := 2;
    R1.Bottom := R1.Top + Bmp2.Height;
    R1.Right := R1.Left + Trunc(Bmp2.Width / 2);
    R2 := Bmp2.Canvas.ClipRect;
    R2.Right := Trunc(R2.Right / 2);
    BMP.Canvas.CopyRect(R1, Bmp2.Canvas, R2);
    Bmp.Canvas.TextOut(R1.Right + 4, T, RemQuot(Button.Caption));
    //Underline
    if US > -1 then
      for n := 0 to UE do
        BMP.Canvas.Pixels[R1.Right + 4 + n + US, T + UT] := FontColor;
    //Glyph2
    R1.Left := Trunc(bmp.Width / 2) + Margin;
    R1.Top := 2;
    R1.Bottom := R1.Top + Bmp2.Height;
    R1.Right := R1.Left + Trunc(Bmp2.Width / 2);
    R2 := Bmp2.Canvas.ClipRect;
    R2.Left := Trunc(Bmp2.Width / 2);
    BMP.Canvas.CopyRect(R1, Bmp2.Canvas, R2);
    Bmp.Canvas.TextOut(R1.Right + 4, T, RemQuot(Button.Caption));
    //Underline
    if US > -1 then
      for n := 0 to UE do
        BMP.Canvas.Pixels[R1.Right + 4 + n + US, T + UT] := FontColor;

    Button.Margin := 0;
    Button.Glyph.Assign(Bmp);
  finally
    FreeAndNil(Bmp);
    FreeAndNil(Bmp2);
  end;
end;