全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:4761
推到 Plurk!
推到 Facebook!

請問如何改變GroupBox 中邊框的顏色呢?

尚未結案
clericc
一般會員


發表:4
回覆:1
積分:1
註冊:2007-05-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2011-08-10 16:27:07 IP:60.249.xxx.xxx 訂閱
查了Google, 好像得要去繼承 TGroupBox , 然後自己畫,

但還沒用過繼承元件的作法

請問有完整的實作程式碼或例子嗎? 謝謝!~
Victor4022
中階會員


發表:0
回覆:76
積分:90
註冊:2011-02-20

發送簡訊給我
#2 引用回覆 回覆 發表時間:2011-08-15 22:32:52 IP:122.126.xxx.xxx 訂閱
小弟參考了 TGroupBox 所引用的 StdCtrls.pas 內容, 繼承 TGroupBox  之後, 改寫一行即可達到您想要的功能:


[code delphi]
type
TColorGroupBox = class(TGroupBox)
protected
procedure Paint; override;
end;
implementation

{ TColorGroupBox }
procedure TColorGroupBox.Paint;
var
H: Integer;
R: TRect;
Flags: Longint;
CaptionRect,
OuterRect: TRect;
Size: TSize;
Box: TThemedButton; // uses Themes.pas
Details: TThemedElementDetails;
begin
with Canvas do
begin
Font := Self.Font;
if ThemeServices.ThemesEnabled then
begin
if Text <> '' then
begin
GetTextExtentPoint32(Handle, PChar(Text), Length(Text), Size);
CaptionRect := Rect(0, 0, Size.cx, Size.cy);
if not UseRightToLeftAlignment then
OffsetRect(CaptionRect, 8, 0)
else
OffsetRect(CaptionRect, Width - 8 - CaptionRect.Right, 0);
end
else
CaptionRect := Rect(0, 0, 0, 0);
OuterRect := ClientRect;
OuterRect.Top := (CaptionRect.Bottom - CaptionRect.Top) div 2;
with CaptionRect do
ExcludeClipRect(Handle, Left, Top, Right, Bottom);
if Enabled then
Box := tbGroupBoxNormal
else
Box := tbGroupBoxDisabled;
Details := ThemeServices.GetElementDetails(Box);
ThemeServices.DrawElement(Handle, Details, OuterRect);
SelectClipRgn(Handle, 0);
if Text <> '' then
ThemeServices.DrawText(Handle, Details, Text, CaptionRect, DT_LEFT, 0);
end
else
begin
H := TextHeight('0');
R := Rect(0, H div 2 - 1, Width, Height);
if Ctl3D then
begin
Inc(R.Left);
Inc(R.Top);
// Brush.Color := clBtnHighlight;
Brush.Color := clBlue; // 改寫此行
FrameRect(R);
OffsetRect(R, -1, -1);
Brush.Color := clBtnShadow;
end else
Brush.Color := clWindowFrame;
FrameRect(R);
if Text <> '' then
begin
if not UseRightToLeftAlignment then
R := Rect(8, 0, 0, H)
else
R := Rect(R.Right - Canvas.TextWidth(Text) - 8, 0, 0, H);
Flags := DrawTextBiDiModeFlags(DT_SINGLELINE);
DrawText(Handle, PChar(Text), Length(Text), R, Flags or DT_CALCRECT);
Brush.Color := Color;
DrawText(Handle, PChar(Text), Length(Text), R, Flags);
end;
end;
end;
end;
[/code]

當然, 您也可以將它封裝得更完美, 將 BorderColor 提升至 published property, 再包裝成 Component
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2011-12-05 10:25:54 IP:118.160.xxx.xxx 未訂閱
使用 DelphiXE2 加 FireMonkey 很容易就可以做到你要的功能
===================引 用 clericc 文 章===================
查了Google, 好像得要去繼承 TGroupBox , 然後自己畫,

但還沒用過繼承元件的作法

請問有完整的實作程式碼或例子嗎? 謝謝!~
系統時間:2024-04-27 5:58:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!