自行实现透明的控件如Panel GroupBox
CSDN的Blog开通了。我想这里的Blog作为今后自己回答别人问题的时候,收藏答案的地方很不错呢。因为社区的贴子早晚都会沉下去,查找起来很不方便,甚至再也找不到呢。Q: http://community.csdn.net/Expert/TopicView.asp?id=3106090言归正传,要实现一些标准的容器类控件的透明效果,也许是个经常会想到的问题。事实上在2000以上的系统下
CSDN的Blog开通了。我想这里的Blog作为今后自己回答别人问题的时候,收藏答案的地方很不错呢。
因为社区的贴子早晚都会沉下去,查找起来很不方便,甚至再也找不到呢。
Q: http://community.csdn.net/Expert/TopicView.asp?id=3106090
言归正传,要实现一些标准的容器类控件的透明效果,也许是个经常会想到的问题。事实上在2000以上的系统下实现起来相当容易。
你不需要重绘父控件的效果,一切都因为窗口有了WS_EX_TRANSPARENT的属性可以选择。
下面我们就以TPanel和TGroupBox控件来说明。我们从它们继承两个新的控件,TTransPanel和TTransGroupBox。
class TTransGroupBox :public TGroupBox
{
void __fastcall CreateParams(Controls::TCreateParams &Params)
{
TGroupBox::CreateParams(Params);
Params.ExStyle += WS_EX_TRANSPARENT;
}
void __fastcall AdjustColors(TPanelBevel Bevel,TColor& TopColor,TColor& BottomColor)
{
TopColor = clBtnHighlight;
if (Bevel == bvLowered) TopColor = clBtnShadow;
BottomColor = clBtnShadow;
if (Bevel == bvLowered) BottomColor = clBtnHighlight;
}
void __fastcall Paint()
{
int H;
TRect R;
Longint Flags;
Canvas->Font = this->Font;
H = Canvas->TextHeight('0');
R = Rect(0, H / 2 - 1, Width, Height);
if (Ctl3D)
{
R.Left ++;
R.Top ++;
Canvas->Brush->Color = clBtnHighlight;
Canvas->FrameRect(R);
OffsetRect(R, -1, -1);
Canvas->Brush->Color = clBtnShadow;
}
else
Canvas->Brush->Color = clWindowFrame;
Canvas->FrameRect(R);
if (Text != "")
{
if (!UseRightToLeftAlignment())
R = Rect(8, 0, 0, H);
else
R = Rect(R.Right - Canvas->TextWidth(Text) - 8, 0, 0, H);
Flags = DrawTextBiDiModeFlags(DT_SINGLELINE);
DrawText(Canvas->Handle, Text.c_str(), Text.Length(), &R, Flags | DT_CALCRECT);
Canvas->Brush->Color = Color;
Canvas->Brush->Style = bsClear;
DrawText(Canvas->Handle, Text.c_str(), Text.Length(), &R, Flags);
}
}
public:
__fastcall virtual TTransGroupBox(TComponent* AOwner):TGroupBox(AOwner)
{
ControlStyle >> csOpaque;
Width = 185;
Height = 105;
}
};
class TTransPanel :public TPanel
{
void __fastcall CreateParams(Controls::TCreateParams &Params)
{
TPanel::CreateParams(Params);
Params.ExStyle += WS_EX_TRANSPARENT;
}
void __fastcall AdjustColors(TPanelBevel Bevel,TColor& TopColor,TColor& BottomColor)
{
TopColor = clBtnHighlight;
if (Bevel == bvLowered) TopColor = clBtnShadow;
BottomColor = clBtnShadow;
if (Bevel == bvLowered) BottomColor = clBtnHighlight;
}
void __fastcall Paint()
{
DynamicArray<int>
Alignments;
Alignments.set_length(
3);
Alignments[taLeftJustify] = DT_LEFT;
Alignments[taCenter] = DT_CENTER;
Alignments[taRightJustify] = DT_RIGHT;
TRect Rect;
TColor TopColor, BottomColor;
int FontHeight;
Longint Flags;
Rect = GetClientRect();
if (BevelOuter != bvNone)
{
AdjustColors(BevelOuter,TopColor, BottomColor);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
}
Frame3D(Canvas, Rect, Color, Color, BorderWidth);
if (BevelInner != bvNone)
{
AdjustColors(BevelInner,TopColor, BottomColor);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
}
Canvas->Brush->Color = Color;
// Canvas->FillRect(Rect); 注释掉这一句,就透明了。下面的bsClear保证写Caption的不使用底色。
Canvas->Brush->Style = bsClear;
Canvas->Font =
this->Font;
FontHeight = Canvas->TextHeight(
'W');
Rect.Top = ((Rect.Bottom + Rect.Top) - FontHeight) /
2;
Rect.Bottom = Rect.Top + FontHeight;
Flags = DT_EXPANDTABS | DT_VCENTER | Alignments[Alignment];
Flags = DrawTextBiDiModeFlags(Flags);
DrawText(Canvas->Handle, Caption.c_str(), -
1, &Rect, Flags);
}
public:
__fastcall virtual TTransPanel(TComponent* AOwner):TPanel(AOwner)
{
ControlStyle >> csOpaque;
Width =
185;
Height =
41;
}
};
更多推荐
所有评论(0)