各位谈谈制作透明控件。 我的要求是是从TCustomControl继承,来实现透明效果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我们看看下面的这一段代码:TCustomLabel.h TTextLayout = (tlTop, tlCenter, tlBottom);TCustomLabel = class(TGraphicControl)private FFocusControl: TWinControl; FAlignment: TAlignment; FAutoSize: Boolean; FLayout: TTextLayout; FWordWrap: Boolean; FShowAccelChar: Boolean; function GetTransparent: Boolean; procedure SetAlignment(Value: TAlignment); procedure SetFocusControl(Value: TWinControl); procedure SetShowAccelChar(Value: Boolean); procedure SetTransparent(Value: Boolean); procedure SetLayout(Value: TTextLayout); procedure SetWordWrap(Value: Boolean); procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED; procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED; procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;protected procedure AdjustBounds; dynamic; procedure DoDrawText(var Rect: TRect; Flags: Longint); dynamic; function GetLabelText: string; virtual; procedure Loaded; override; procedure Notification(AComponent: TComponent; Operation: TOperation); override; procedure Paint; override; procedure SetAutoSize(Value: Boolean); virtual; property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify; property AutoSize: Boolean read FAutoSize write SetAutoSize default True; property FocusControl: TWinControl read FFocusControl write SetFocusControl; property ShowAccelChar: Boolean read FShowAccelChar write SetShowAccelChar default True; property Transparent: Boolean read GetTransparent write SetTransparent default False; property Layout: TTextLayout read FLayout write SetLayout default tlTop; property WordWrap: Boolean read FWordWrap write SetWordWrap default False;public constructor Create(AOwner: TComponent); override; property Canvas;end;[TCustomLabel.cpp]constructor TCustomLabel.Create(AOwner: TComponent);begin inherited Create(AOwner); ControlStyle := ControlStyle + [csOpaque, csReplicatable]; Width := 65; Height := 17; FAutoSize := True; FShowAccelChar := True;end;function TCustomLabel.GetLabelText: string;begin Result := Caption;end;procedure TCustomLabel.DoDrawText(var Rect: TRect; Flags: Longint);var Text: string;begin Text := GetLabelText; if (Flags and DT_CALCRECT <> 0) and ((Text = '') or FShowAccelChar and (Text[1] = '&') and (Text[2] = #0)) then Text := Text + ' '; if not FShowAccelChar then Flags := Flags or DT_NOPREFIX; Flags := DrawTextBiDiModeFlags(Flags); Canvas.Font := Font; if not Enabled then begin OffsetRect(Rect, 1, 1); Canvas.Font.Color := clBtnHighlight; DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags); OffsetRect(Rect, -1, -1); Canvas.Font.Color := clBtnShadow; DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags); end else DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);end;procedure TCustomLabel.Paint;const Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER); WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);var Rect, CalcRect: TRect; DrawStyle: Longint;begin with Canvas do begin if not Transparent then begin Brush.Color := Self.Color; Brush.Style := bsSolid; FillRect(ClientRect); end; Brush.Style := bsClear; Rect := ClientRect; { DoDrawText takes care of BiDi alignments } DrawStyle := DT_EXPANDTABS or WordWraps[FWordWrap] or Alignments[FAlignment]; { Calculate vertical layout } if FLayout <> tlTop then begin CalcRect := Rect; DoDrawText(CalcRect, DrawStyle or DT_CALCRECT); if FLayout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom) else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2); end; DoDrawText(Rect, DrawStyle); end;end;procedure TCustomLabel.Loaded;begin inherited Loaded; AdjustBounds;end;procedure TCustomLabel.AdjustBounds;const WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);var DC: HDC; X: Integer; Rect: TRect; AAlignment: TAlignment;begin if not (csReading in ComponentState) and FAutoSize then begin Rect := ClientRect; DC := GetDC(0); Canvas.Handle := DC; DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[FWordWrap]); Canvas.Handle := 0; ReleaseDC(0, DC); X := Left; AAlignment := FAlignment; if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment); if AAlignment = taRightJustify then Inc(X, Width - Rect.Right); SetBounds(X, Top, Rect.Right, Rect.Bottom); end;end;procedure TCustomLabel.SetAlignment(Value: TAlignment);begin if FAlignment <> Value then begin FAlignment := Value; Invalidate; end;end;procedure TCustomLabel.SetAutoSize(Value: Boolean);begin if FAutoSize <> Value then begin FAutoSize := Value; AdjustBounds; end;end;function TCustomLabel.GetTransparent: Boolean;begin Result := not (csOpaque in ControlStyle);end;procedure TCustomLabel.SetFocusControl(Value: TWinControl);begin FFocusControl := Value; if Value <> nil then Value.FreeNotification(Self);end;procedure TCustomLabel.SetShowAccelChar(Value: Boolean);begin if FShowAccelChar <> Value then begin FShowAccelChar := Value; Invalidate; end;end;procedure TCustomLabel.SetTransparent(Value: Boolean);begin if Transparent <> Value then begin if Value then ControlStyle := ControlStyle - [csOpaque] else ControlStyle := ControlStyle + [csOpaque]; Invalidate; end;end;procedure TCustomLabel.SetLayout(Value: TTextLayout);begin if FLayout <> Value then begin FLayout := Value; Invalidate; end;end;procedure TCustomLabel.SetWordWrap(Value: Boolean);begin if FWordWrap <> Value then begin FWordWrap := Value; AdjustBounds; Invalidate; end;end;procedure TCustomLabel.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation); if (Operation = opRemove) and (AComponent = FFocusControl) then FFocusControl := nil;end;procedure TCustomLabel.CMTextChanged(var Message: TMessage);begin Invalidate; AdjustBounds;end;procedure TCustomLabel.CMFontChanged(var Message: TMessage);begin inherited; AdjustBounds;end;procedure TCustomLabel.CMDialogChar(var Message: TCMDialogChar);begin if (FFocusControl <> nil) and Enabled and ShowAccelChar and IsAccel(Message.CharCode, Caption) then with FFocusControl do if CanFocus then begin SetFocus; Message.Result := 1; end;end;TGraphicControl中没有Transparent属性,Transparent属性是在TCustomLabel中实现,但是在我的代码我按照这种方法却不行。 http://www.snowdeer.net/BBS/dispbbs.asp?boardID=4&RootID=31370&ID=31370 http://www.snowdeer.net/BBS/dispbbs.asp?boardID=4&RootID=31370&ID=31370 http://www.snowdeer.net/BBS/dispbbs.asp?boardID=4&RootID=31370&ID=31370 如果这个compounent属于纯粹VCL的组件,应该没有问题啊,你有机会控制他的一切啊。首先是CreateParam方法,然后消息的截获和处理,在适当的时候处理WM_ERASEBKGND这样的消息,以及重载Paint方法,还有SetParent(),Invalidate()等等最终总是可以达到目的 的 To cker:你可能理解错了我的意思,我现在关键问题是要知道透明效果是如何实现的。 直接的办法我不知道,不过投机取巧的办法我倒是有一个:那就是直接把父控件的画面作为一个Bitmap画到子控件的背景上,这样子控件“看起来就象透明了”。 现在主要问题是要实现透明,我的代码:#ifndef FocusLabelH#define FocusLabelH#include <SysUtils.hpp>#include <Controls.hpp>#include <Classes.hpp>#include <Forms.hpp>class PACKAGE TFocusLabel : public TCustomControl{private: TLabel *FLabel; bool FTransParent; bool FAutoSize; AnsiString FCaption;protected: virtual void __fastcall CreateWnd(); virtual void __fastcall Paint();public: void __fastcall SetCaption(AnsiString S); void __fastcall SetTransParent(bool TransParent); bool _fastcall GetTransParent(); void __fastcall SetSize(); void __fastcall SetAutoSize(bool AutoSize); __fastcall TFocusLabel(TComponent* Owner); virtual __fastcall ~TFocusLabel();__published: __property bool TransParent={read=GetTransParent,write=SetTransParent}; __property bool AutoSize={read=FAutoSize,write=SetAutoSize}; __property AnsiString Caption={read=FCaption,write=SetCaption}; __property Color;};#endif.cpp File//---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "FocusLabel.h"#pragma package(smart_init)//---------------------------------------------------------------------------// ValidCtrCheck is used to assure that the components created do not have// any pure virtual functions.//static inline void ValidCtrCheck(TFocusLabel *){ new TFocusLabel(NULL);}//---------------------------------------------------------------------------__fastcall TFocusLabel::TFocusLabel(TComponent* Owner) : TCustomControl(Owner){ FLabel=new TLabel(this); FLabel->Caption="Label1"; Caption="Label1"; Width=40; Height=20; ControlStyle<<csOpaque<<csReplicatable;}//GetTransParentbool __fastcall TFocusLabel::GetTransParent(){ if(ControlStyle.Contains(csOpaque)) { return false; } else { return true; }}//SetTransParentvoid __fastcall TFocusLabel::SetTransParent(bool TransParent){ FTransParent=TransParent; //这些实现透明效果的代码没有起到作用 FLabel->Transparent=TransParent; if(TransParent) { ControlStyle>>csOpaque; } else { ControlStyle<<csOpaque; } Invalidate();}//CreateWnd Methodvoid __fastcall TFocusLabel::CreateWnd(){ TCustomControl::CreateWnd();}//Paint Methodvoid __fastcall TFocusLabel::Paint(){ SetSize(); FLabel->Parent=this; FLabel->Left=0; FLabel->Top=0; FLabel->Transparent=TransParent; if(TransParent) //whether the controls is transparent or not. { long DrawStyle; Canvas->Brush->Color=Color; Canvas->Brush->Style=bsClear; } else { Canvas->Brush->Color=Color; Canvas->Brush->Style=bsSolid; Canvas->FillRect(ClientRect); }}//SetCaptionvoid __fastcall TFocusLabel::SetCaption(AnsiString S){ FCaption=S; FLabel->Caption=S;}//SetSizevoid __fastcall TFocusLabel::SetSize(){ if(FAutoSize) { Canvas->Font=Font; Width=Canvas->TextWidth(Caption)+20; Height=Canvas->TextHeight(Caption)+30; FLabel->Width=Canvas->TextWidth(Caption)+20; FLabel->Height=Canvas->TextHeight(Caption)+30; }}//SetAutoSizevoid __fastcall TFocusLabel::SetAutoSize(bool AutoSize){ FAutoSize=AutoSize; FLabel->AutoSize=AutoSize; if(FAutoSize) { SetSize(); Repaint(); }}//Deconstructor__fastcall TFocusLabel::~TFocusLabel(){ if(FLabel) { delete FLabel; }}namespace Focuslabel{ void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TFocusLabel)}; RegisterComponents("AKing", classes, 0); }}如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗? 现在主要问题是要实现透明,我的代码:#ifndef FocusLabelH#define FocusLabelH#include <SysUtils.hpp>#include <Controls.hpp>#include <Classes.hpp>#include <Forms.hpp>class PACKAGE TFocusLabel : public TCustomControl{private: TLabel *FLabel; bool FTransParent; bool FAutoSize; AnsiString FCaption;protected: virtual void __fastcall CreateWnd(); virtual void __fastcall Paint();public: void __fastcall SetCaption(AnsiString S); void __fastcall SetTransParent(bool TransParent); bool _fastcall GetTransParent(); void __fastcall SetSize(); void __fastcall SetAutoSize(bool AutoSize); __fastcall TFocusLabel(TComponent* Owner); virtual __fastcall ~TFocusLabel();__published: __property bool TransParent={read=GetTransParent,write=SetTransParent}; __property bool AutoSize={read=FAutoSize,write=SetAutoSize}; __property AnsiString Caption={read=FCaption,write=SetCaption}; __property Color;};#endif.cpp File//---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "FocusLabel.h"#pragma package(smart_init)//---------------------------------------------------------------------------// ValidCtrCheck is used to assure that the components created do not have// any pure virtual functions.//static inline void ValidCtrCheck(TFocusLabel *){ new TFocusLabel(NULL);}//---------------------------------------------------------------------------__fastcall TFocusLabel::TFocusLabel(TComponent* Owner) : TCustomControl(Owner){ FLabel=new TLabel(this); FLabel->Caption="Label1"; Caption="Label1"; Width=40; Height=20; ControlStyle<<csOpaque<<csReplicatable;}//GetTransParentbool __fastcall TFocusLabel::GetTransParent(){ if(ControlStyle.Contains(csOpaque)) { return false; } else { return true; }}//SetTransParentvoid __fastcall TFocusLabel::SetTransParent(bool TransParent){ FTransParent=TransParent; //这些实现透明效果的代码没有起到作用 FLabel->Transparent=TransParent; if(TransParent) { ControlStyle>>csOpaque; } else { ControlStyle<<csOpaque; } Invalidate();}//CreateWnd Methodvoid __fastcall TFocusLabel::CreateWnd(){ TCustomControl::CreateWnd();}//Paint Methodvoid __fastcall TFocusLabel::Paint(){ SetSize(); FLabel->Parent=this; FLabel->Left=0; FLabel->Top=0; FLabel->Transparent=TransParent; if(TransParent) //whether the controls is transparent or not. { long DrawStyle; Canvas->Brush->Color=Color; Canvas->Brush->Style=bsClear; } else { Canvas->Brush->Color=Color; Canvas->Brush->Style=bsSolid; Canvas->FillRect(ClientRect); }}//SetCaptionvoid __fastcall TFocusLabel::SetCaption(AnsiString S){ FCaption=S; FLabel->Caption=S;}//SetSizevoid __fastcall TFocusLabel::SetSize(){ if(FAutoSize) { Canvas->Font=Font; Width=Canvas->TextWidth(Caption)+20; Height=Canvas->TextHeight(Caption)+30; FLabel->Width=Canvas->TextWidth(Caption)+20; FLabel->Height=Canvas->TextHeight(Caption)+30; }}//SetAutoSizevoid __fastcall TFocusLabel::SetAutoSize(bool AutoSize){ FAutoSize=AutoSize; FLabel->AutoSize=AutoSize; if(FAutoSize) { SetSize(); Repaint(); }}//Deconstructor__fastcall TFocusLabel::~TFocusLabel(){ if(FLabel) { delete FLabel; }}namespace Focuslabel{ void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TFocusLabel)}; RegisterComponents("AKing", classes, 0); }}如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗? 现在主要问题是要实现透明,我的代码:#ifndef FocusLabelH#define FocusLabelH#include <SysUtils.hpp>#include <Controls.hpp>#include <Classes.hpp>#include <Forms.hpp>class PACKAGE TFocusLabel : public TCustomControl{private: TLabel *FLabel; bool FTransParent; bool FAutoSize; AnsiString FCaption;protected: virtual void __fastcall CreateWnd(); virtual void __fastcall Paint();public: void __fastcall SetCaption(AnsiString S); void __fastcall SetTransParent(bool TransParent); bool _fastcall GetTransParent(); void __fastcall SetSize(); void __fastcall SetAutoSize(bool AutoSize); __fastcall TFocusLabel(TComponent* Owner); virtual __fastcall ~TFocusLabel();__published: __property bool TransParent={read=GetTransParent,write=SetTransParent}; __property bool AutoSize={read=FAutoSize,write=SetAutoSize}; __property AnsiString Caption={read=FCaption,write=SetCaption}; __property Color;};#endif.cpp File//---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "FocusLabel.h"#pragma package(smart_init)//---------------------------------------------------------------------------// ValidCtrCheck is used to assure that the components created do not have// any pure virtual functions.//static inline void ValidCtrCheck(TFocusLabel *){ new TFocusLabel(NULL);}//---------------------------------------------------------------------------__fastcall TFocusLabel::TFocusLabel(TComponent* Owner) : TCustomControl(Owner){ FLabel=new TLabel(this); FLabel->Caption="Label1"; Caption="Label1"; Width=40; Height=20; ControlStyle<<csOpaque<<csReplicatable;}//GetTransParentbool __fastcall TFocusLabel::GetTransParent(){ if(ControlStyle.Contains(csOpaque)) { return false; } else { return true; }}//SetTransParentvoid __fastcall TFocusLabel::SetTransParent(bool TransParent){ FTransParent=TransParent; //这些实现透明效果的代码没有起到作用 FLabel->Transparent=TransParent; if(TransParent) { ControlStyle>>csOpaque; } else { ControlStyle<<csOpaque; } Invalidate();}//CreateWnd Methodvoid __fastcall TFocusLabel::CreateWnd(){ TCustomControl::CreateWnd();}//Paint Methodvoid __fastcall TFocusLabel::Paint(){ SetSize(); FLabel->Parent=this; FLabel->Left=0; FLabel->Top=0; FLabel->Transparent=TransParent; if(TransParent) //whether the controls is transparent or not. { long DrawStyle; Canvas->Brush->Color=Color; Canvas->Brush->Style=bsClear; } else { Canvas->Brush->Color=Color; Canvas->Brush->Style=bsSolid; Canvas->FillRect(ClientRect); }}//SetCaptionvoid __fastcall TFocusLabel::SetCaption(AnsiString S){ FCaption=S; FLabel->Caption=S;}//SetSizevoid __fastcall TFocusLabel::SetSize(){ if(FAutoSize) { Canvas->Font=Font; Width=Canvas->TextWidth(Caption)+20; Height=Canvas->TextHeight(Caption)+30; FLabel->Width=Canvas->TextWidth(Caption)+20; FLabel->Height=Canvas->TextHeight(Caption)+30; }}//SetAutoSizevoid __fastcall TFocusLabel::SetAutoSize(bool AutoSize){ FAutoSize=AutoSize; FLabel->AutoSize=AutoSize; if(FAutoSize) { SetSize(); Repaint(); }}//Deconstructor__fastcall TFocusLabel::~TFocusLabel(){ if(FLabel) { delete FLabel; }}namespace Focuslabel{ void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TFocusLabel)}; RegisterComponents("AKing", classes, 0); }}如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗? http://www.snowdeer.net/BBS/dispbbs.asp?boardID=4&RootID=31370&ID=31370 1.重载 CreateParamsProcedure TYourWinControl.CreateParams( Var params: TCreateParams );begin inherited CreateParams( params ); params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;end;2.重载 WMEraseBkGndProcedure WMEraseBkGnd( Var msg: TWMEraseBkGnd );message WM_ERASEBKGND;Procedure TYourWinControl.WMEraseBkGnd( Var msg: TWMEraseBkGnd );begin SetBkMode( msg.DC, TRANSPARENT ); msg.result := 1;end; unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls, Buttons;type TForm1 = class(TForm) Label1: TLabel; Shape1: TShape; Shape2: TShape; Shape3: TShape; Shape4: TShape; Image1: TImage; SpeedButton1: TSpeedButton; procedure FormCreate(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); private { Private declarations } //截获背景图像 function GetBackgroundBmp:TBitmap; //对背景图像进行滤镜处理procedure TranslucentBmp(Bmp:TBitmap;AColor:TColor;ATransparent:Longint); public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}//以下截获背景图像function TForm1.GetBackgroundBmp:TBitmap;var Scn:TCanvas; h,w:Integer;begin Scn:=TCanvas.Create; //建立整个屏幕的画布 h:=ClientHeight; //窗口的高 w:=ClientWidth; //窗口的宽 Result.Height:=h; //设返回位图的高就是窗口的高 Result.Width:=w; //设返回位图的宽就是窗口的宽 tryScn.Handle:=GetDC(0);//取得整个屏幕的DC//以下一行将窗口的背景部分复制到指定的//画布中,也就是本函数的返回值 Result.Canvas.CopyRect(Rect(0,0,w,h), Scn,Rect(Left,Top,Left+w,Top+h)); ReleaseDC(0, Scn.handle); finally Scn.Free; end;end;//以下函数对背景图像进行滤镜处理,//Bmp是要处理的位图;ATransparent是透明度procedure TForm1.TranslucentBmp(Bmp:TBitmap;AColor:TColor;ATransparent:Longint);var BkColor:COLORREF; ForeColor:Longint; R,G,B:Int64; i,j:Integer;begin ForeColor:=ColorToRGB(AColor); with Bmp.Canvas do for i:=ClientHeight-1 downto 0 do for j:=ClientWidth-1 downto 0 do begin BkColor:=GetPixel(Handle,j,i); //取得每一像素 R:=Byte(ForeColor)+(Byte(BkColor) -Byte(ForeColor))*ATransparent; G:=Byte(ForeColor shr 8)+(Byte (BkColor shr 8)-Byte(ForeColor shr 8)) *ATransparent; B:=Byte(ForeColor shr 16)+(Byte (BkColor shr 16) -Byte(ForeColor shr 16))*ATransparent; SetPixelV(Handle,j,i,RGB(R,G,B));//合成像素 end;end;procedure TForm1.FormCreate(Sender: TObject);var BackgroundBmp:TBitmap;begin try BackgroundBmp:=Tbitmap.Create; //建立窗口背景图 BackgroundBmp.PixelFormat:=pf24bit; //指定该图是24位真彩色BackgroundBmp:=GetBackgroundBmp; //取得窗口背景图 TranslucentBmp(BackgroundBmp,clBlack,50); //对该图像进行滤镜处理Image1.Picture.Bitmap:=BackgroundBmp; //将处理过的图像显示出来 finally BackgroundBmp.Free; end;end;procedure TForm1.SpeedButton1Click(Sender: TObject);begin Close;end;end.最好能调用MMX进行优化! 干宝宝生日,两周岁了 问题求助 delphi2007升级到delphi2010后,外部动态库运行结果不对了。 dbExpress问题 怎么判断用户按下的是 [确定]? 还是 [取消]?分不够可以再加!! 如何将这些函数减缩成一个函数?(高手请救) office xp 的access操作的问题? 怎样让delphi程序通过socket和其它程序建立通讯阿? Delphi中如何判断Edit.text为合法的日期? 一个非常奇怪的问题,希望大家有兴趣赐教! 出现下面的情况是什么原因?“Error Creating Cursor Handle”在线等 怎样更改按钮的颜色?
TCustomLabel.h
TTextLayout = (tlTop, tlCenter, tlBottom);
TCustomLabel = class(TGraphicControl)
private
FFocusControl: TWinControl;
FAlignment: TAlignment;
FAutoSize: Boolean;
FLayout: TTextLayout;
FWordWrap: Boolean;
FShowAccelChar: Boolean;
function GetTransparent: Boolean;
procedure SetAlignment(Value: TAlignment);
procedure SetFocusControl(Value: TWinControl);
procedure SetShowAccelChar(Value: Boolean);
procedure SetTransparent(Value: Boolean);
procedure SetLayout(Value: TTextLayout);
procedure SetWordWrap(Value: Boolean);
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
protected
procedure AdjustBounds; dynamic;
procedure DoDrawText(var Rect: TRect; Flags: Longint); dynamic;
function GetLabelText: string; virtual;
procedure Loaded; override;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint; override;
procedure SetAutoSize(Value: Boolean); virtual;
property Alignment: TAlignment read FAlignment write SetAlignment
default taLeftJustify;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property FocusControl: TWinControl read FFocusControl write SetFocusControl;
property ShowAccelChar: Boolean read FShowAccelChar write SetShowAccelChar default True;
property Transparent: Boolean read GetTransparent write SetTransparent default False;
property Layout: TTextLayout read FLayout write SetLayout default tlTop;
property WordWrap: Boolean read FWordWrap write SetWordWrap default False;
public
constructor Create(AOwner: TComponent); override;
property Canvas;
end;
[TCustomLabel.cpp]
constructor TCustomLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csOpaque, csReplicatable];
Width := 65;
Height := 17;
FAutoSize := True;
FShowAccelChar := True;
end;function TCustomLabel.GetLabelText: string;
begin
Result := Caption;
end;procedure TCustomLabel.DoDrawText(var Rect: TRect; Flags: Longint);
var
Text: string;
begin
Text := GetLabelText;
if (Flags and DT_CALCRECT <> 0) and ((Text = '') or FShowAccelChar and
(Text[1] = '&') and (Text[2] = #0)) then Text := Text + ' ';
if not FShowAccelChar then Flags := Flags or DT_NOPREFIX;
Flags := DrawTextBiDiModeFlags(Flags);
Canvas.Font := Font;
if not Enabled then
begin
OffsetRect(Rect, 1, 1);
Canvas.Font.Color := clBtnHighlight;
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
OffsetRect(Rect, -1, -1);
Canvas.Font.Color := clBtnShadow;
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
end
else
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
end;procedure TCustomLabel.Paint;
const
Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
Rect, CalcRect: TRect;
DrawStyle: Longint;
begin
with Canvas do
begin
if not Transparent then
begin
Brush.Color := Self.Color;
Brush.Style := bsSolid;
FillRect(ClientRect);
end;
Brush.Style := bsClear;
Rect := ClientRect;
{ DoDrawText takes care of BiDi alignments }
DrawStyle := DT_EXPANDTABS or WordWraps[FWordWrap] or Alignments[FAlignment];
{ Calculate vertical layout }
if FLayout <> tlTop then
begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if FLayout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
end;
DoDrawText(Rect, DrawStyle);
end;
end;procedure TCustomLabel.Loaded;
begin
inherited Loaded;
AdjustBounds;
end;procedure TCustomLabel.AdjustBounds;
const
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
DC: HDC;
X: Integer;
Rect: TRect;
AAlignment: TAlignment;
begin
if not (csReading in ComponentState) and FAutoSize then
begin
Rect := ClientRect;
DC := GetDC(0);
Canvas.Handle := DC;
DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[FWordWrap]);
Canvas.Handle := 0;
ReleaseDC(0, DC);
X := Left;
AAlignment := FAlignment;
if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
if AAlignment = taRightJustify then Inc(X, Width - Rect.Right);
SetBounds(X, Top, Rect.Right, Rect.Bottom);
end;
end;procedure TCustomLabel.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
Invalidate;
end;
end;procedure TCustomLabel.SetAutoSize(Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
AdjustBounds;
end;
end;function TCustomLabel.GetTransparent: Boolean;
begin
Result := not (csOpaque in ControlStyle);
end;procedure TCustomLabel.SetFocusControl(Value: TWinControl);
begin
FFocusControl := Value;
if Value <> nil then Value.FreeNotification(Self);
end;procedure TCustomLabel.SetShowAccelChar(Value: Boolean);
begin
if FShowAccelChar <> Value then
begin
FShowAccelChar := Value;
Invalidate;
end;
end;procedure TCustomLabel.SetTransparent(Value: Boolean);
begin
if Transparent <> Value then
begin
if Value then
ControlStyle := ControlStyle - [csOpaque] else
ControlStyle := ControlStyle + [csOpaque];
Invalidate;
end;
end;procedure TCustomLabel.SetLayout(Value: TTextLayout);
begin
if FLayout <> Value then
begin
FLayout := Value;
Invalidate;
end;
end;procedure TCustomLabel.SetWordWrap(Value: Boolean);
begin
if FWordWrap <> Value then
begin
FWordWrap := Value;
AdjustBounds;
Invalidate;
end;
end;procedure TCustomLabel.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFocusControl) then
FFocusControl := nil;
end;procedure TCustomLabel.CMTextChanged(var Message: TMessage);
begin
Invalidate;
AdjustBounds;
end;procedure TCustomLabel.CMFontChanged(var Message: TMessage);
begin
inherited;
AdjustBounds;
end;procedure TCustomLabel.CMDialogChar(var Message: TCMDialogChar);
begin
if (FFocusControl <> nil) and Enabled and ShowAccelChar and
IsAccel(Message.CharCode, Caption) then
with FFocusControl do
if CanFocus then
begin
SetFocus;
Message.Result := 1;
end;
end;
TGraphicControl中没有Transparent属性,Transparent属性是在TCustomLabel中实现,但是在我的代码我按照这种方法却不行。
你可能理解错了我的意思,我现在关键问题是要知道透明效果是如何实现的。
#ifndef FocusLabelH
#define FocusLabelH
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
class PACKAGE TFocusLabel : public TCustomControl
{
private:
TLabel *FLabel;
bool FTransParent;
bool FAutoSize;
AnsiString FCaption;protected:
virtual void __fastcall CreateWnd();
virtual void __fastcall Paint();public:
void __fastcall SetCaption(AnsiString S);
void __fastcall SetTransParent(bool TransParent);
bool _fastcall GetTransParent();
void __fastcall SetSize();
void __fastcall SetAutoSize(bool AutoSize);
__fastcall TFocusLabel(TComponent* Owner);
virtual __fastcall ~TFocusLabel();
__published:
__property bool TransParent={read=GetTransParent,write=SetTransParent};
__property bool AutoSize={read=FAutoSize,write=SetAutoSize};
__property AnsiString Caption={read=FCaption,write=SetCaption};
__property Color;
};
#endif
.cpp File
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "FocusLabel.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//static inline void ValidCtrCheck(TFocusLabel *)
{
new TFocusLabel(NULL);
}
//---------------------------------------------------------------------------
__fastcall TFocusLabel::TFocusLabel(TComponent* Owner)
: TCustomControl(Owner)
{
FLabel=new TLabel(this);
FLabel->Caption="Label1";
Caption="Label1";
Width=40;
Height=20;
ControlStyle<<csOpaque<<csReplicatable;}
//GetTransParent
bool __fastcall TFocusLabel::GetTransParent()
{
if(ControlStyle.Contains(csOpaque))
{
return false;
}
else
{
return true;
}}
//SetTransParent
void __fastcall TFocusLabel::SetTransParent(bool TransParent)
{
FTransParent=TransParent; //这些实现透明效果的代码没有起到作用
FLabel->Transparent=TransParent;
if(TransParent)
{
ControlStyle>>csOpaque;
}
else
{
ControlStyle<<csOpaque;
}
Invalidate();
}
//CreateWnd Method
void __fastcall TFocusLabel::CreateWnd()
{
TCustomControl::CreateWnd();
}
//Paint Method
void __fastcall TFocusLabel::Paint()
{
SetSize();
FLabel->Parent=this;
FLabel->Left=0;
FLabel->Top=0;
FLabel->Transparent=TransParent;
if(TransParent) //whether the controls is transparent or not.
{
long DrawStyle;
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsClear;
}
else
{
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsSolid;
Canvas->FillRect(ClientRect);
}
}
//SetCaption
void __fastcall TFocusLabel::SetCaption(AnsiString S)
{
FCaption=S;
FLabel->Caption=S;
}
//SetSize
void __fastcall TFocusLabel::SetSize()
{
if(FAutoSize)
{
Canvas->Font=Font;
Width=Canvas->TextWidth(Caption)+20;
Height=Canvas->TextHeight(Caption)+30;
FLabel->Width=Canvas->TextWidth(Caption)+20;
FLabel->Height=Canvas->TextHeight(Caption)+30; }
}
//SetAutoSize
void __fastcall TFocusLabel::SetAutoSize(bool AutoSize)
{
FAutoSize=AutoSize;
FLabel->AutoSize=AutoSize;
if(FAutoSize)
{
SetSize();
Repaint();
}
}//Deconstructor
__fastcall TFocusLabel::~TFocusLabel()
{
if(FLabel)
{
delete FLabel;
}
}namespace Focuslabel
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TFocusLabel)};
RegisterComponents("AKing", classes, 0);
}
}
如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗?
#ifndef FocusLabelH
#define FocusLabelH
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
class PACKAGE TFocusLabel : public TCustomControl
{
private:
TLabel *FLabel;
bool FTransParent;
bool FAutoSize;
AnsiString FCaption;protected:
virtual void __fastcall CreateWnd();
virtual void __fastcall Paint();public:
void __fastcall SetCaption(AnsiString S);
void __fastcall SetTransParent(bool TransParent);
bool _fastcall GetTransParent();
void __fastcall SetSize();
void __fastcall SetAutoSize(bool AutoSize);
__fastcall TFocusLabel(TComponent* Owner);
virtual __fastcall ~TFocusLabel();
__published:
__property bool TransParent={read=GetTransParent,write=SetTransParent};
__property bool AutoSize={read=FAutoSize,write=SetAutoSize};
__property AnsiString Caption={read=FCaption,write=SetCaption};
__property Color;
};
#endif
.cpp File
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "FocusLabel.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//static inline void ValidCtrCheck(TFocusLabel *)
{
new TFocusLabel(NULL);
}
//---------------------------------------------------------------------------
__fastcall TFocusLabel::TFocusLabel(TComponent* Owner)
: TCustomControl(Owner)
{
FLabel=new TLabel(this);
FLabel->Caption="Label1";
Caption="Label1";
Width=40;
Height=20;
ControlStyle<<csOpaque<<csReplicatable;}
//GetTransParent
bool __fastcall TFocusLabel::GetTransParent()
{
if(ControlStyle.Contains(csOpaque))
{
return false;
}
else
{
return true;
}}
//SetTransParent
void __fastcall TFocusLabel::SetTransParent(bool TransParent)
{
FTransParent=TransParent; //这些实现透明效果的代码没有起到作用
FLabel->Transparent=TransParent;
if(TransParent)
{
ControlStyle>>csOpaque;
}
else
{
ControlStyle<<csOpaque;
}
Invalidate();
}
//CreateWnd Method
void __fastcall TFocusLabel::CreateWnd()
{
TCustomControl::CreateWnd();
}
//Paint Method
void __fastcall TFocusLabel::Paint()
{
SetSize();
FLabel->Parent=this;
FLabel->Left=0;
FLabel->Top=0;
FLabel->Transparent=TransParent;
if(TransParent) //whether the controls is transparent or not.
{
long DrawStyle;
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsClear;
}
else
{
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsSolid;
Canvas->FillRect(ClientRect);
}
}
//SetCaption
void __fastcall TFocusLabel::SetCaption(AnsiString S)
{
FCaption=S;
FLabel->Caption=S;
}
//SetSize
void __fastcall TFocusLabel::SetSize()
{
if(FAutoSize)
{
Canvas->Font=Font;
Width=Canvas->TextWidth(Caption)+20;
Height=Canvas->TextHeight(Caption)+30;
FLabel->Width=Canvas->TextWidth(Caption)+20;
FLabel->Height=Canvas->TextHeight(Caption)+30; }
}
//SetAutoSize
void __fastcall TFocusLabel::SetAutoSize(bool AutoSize)
{
FAutoSize=AutoSize;
FLabel->AutoSize=AutoSize;
if(FAutoSize)
{
SetSize();
Repaint();
}
}//Deconstructor
__fastcall TFocusLabel::~TFocusLabel()
{
if(FLabel)
{
delete FLabel;
}
}namespace Focuslabel
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TFocusLabel)};
RegisterComponents("AKing", classes, 0);
}
}
如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗?
#ifndef FocusLabelH
#define FocusLabelH
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
class PACKAGE TFocusLabel : public TCustomControl
{
private:
TLabel *FLabel;
bool FTransParent;
bool FAutoSize;
AnsiString FCaption;protected:
virtual void __fastcall CreateWnd();
virtual void __fastcall Paint();public:
void __fastcall SetCaption(AnsiString S);
void __fastcall SetTransParent(bool TransParent);
bool _fastcall GetTransParent();
void __fastcall SetSize();
void __fastcall SetAutoSize(bool AutoSize);
__fastcall TFocusLabel(TComponent* Owner);
virtual __fastcall ~TFocusLabel();
__published:
__property bool TransParent={read=GetTransParent,write=SetTransParent};
__property bool AutoSize={read=FAutoSize,write=SetAutoSize};
__property AnsiString Caption={read=FCaption,write=SetCaption};
__property Color;
};
#endif
.cpp File
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "FocusLabel.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//static inline void ValidCtrCheck(TFocusLabel *)
{
new TFocusLabel(NULL);
}
//---------------------------------------------------------------------------
__fastcall TFocusLabel::TFocusLabel(TComponent* Owner)
: TCustomControl(Owner)
{
FLabel=new TLabel(this);
FLabel->Caption="Label1";
Caption="Label1";
Width=40;
Height=20;
ControlStyle<<csOpaque<<csReplicatable;}
//GetTransParent
bool __fastcall TFocusLabel::GetTransParent()
{
if(ControlStyle.Contains(csOpaque))
{
return false;
}
else
{
return true;
}}
//SetTransParent
void __fastcall TFocusLabel::SetTransParent(bool TransParent)
{
FTransParent=TransParent; //这些实现透明效果的代码没有起到作用
FLabel->Transparent=TransParent;
if(TransParent)
{
ControlStyle>>csOpaque;
}
else
{
ControlStyle<<csOpaque;
}
Invalidate();
}
//CreateWnd Method
void __fastcall TFocusLabel::CreateWnd()
{
TCustomControl::CreateWnd();
}
//Paint Method
void __fastcall TFocusLabel::Paint()
{
SetSize();
FLabel->Parent=this;
FLabel->Left=0;
FLabel->Top=0;
FLabel->Transparent=TransParent;
if(TransParent) //whether the controls is transparent or not.
{
long DrawStyle;
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsClear;
}
else
{
Canvas->Brush->Color=Color;
Canvas->Brush->Style=bsSolid;
Canvas->FillRect(ClientRect);
}
}
//SetCaption
void __fastcall TFocusLabel::SetCaption(AnsiString S)
{
FCaption=S;
FLabel->Caption=S;
}
//SetSize
void __fastcall TFocusLabel::SetSize()
{
if(FAutoSize)
{
Canvas->Font=Font;
Width=Canvas->TextWidth(Caption)+20;
Height=Canvas->TextHeight(Caption)+30;
FLabel->Width=Canvas->TextWidth(Caption)+20;
FLabel->Height=Canvas->TextHeight(Caption)+30; }
}
//SetAutoSize
void __fastcall TFocusLabel::SetAutoSize(bool AutoSize)
{
FAutoSize=AutoSize;
FLabel->AutoSize=AutoSize;
if(FAutoSize)
{
SetSize();
Repaint();
}
}//Deconstructor
__fastcall TFocusLabel::~TFocusLabel()
{
if(FLabel)
{
delete FLabel;
}
}namespace Focuslabel
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TFocusLabel)};
RegisterComponents("AKing", classes, 0);
}
}
如果从TLabel的继承层次来看,首先查看TGraphicControl,它并没有Transparent属性,TrasnParent属性是在TCustomLabel中实现的,但我在代码中按照它的实现方法,并不能成功,有人能解释一下原因吗?
Procedure TYourWinControl.CreateParams( Var params: TCreateParams );
begin
inherited CreateParams( params );
params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;
end;2.重载 WMEraseBkGnd
Procedure WMEraseBkGnd( Var msg: TWMEraseBkGnd );message WM_ERASEBKGND;Procedure TYourWinControl.WMEraseBkGnd( Var msg: TWMEraseBkGnd );
begin
SetBkMode( msg.DC, TRANSPARENT );
msg.result := 1;
end;
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,StdCtrls, ExtCtrls, Buttons;type
TForm1 = class(TForm)
Label1: TLabel;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Image1: TImage;
SpeedButton1: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
//截获背景图像
function GetBackgroundBmp:TBitmap;
//对背景图像进行滤镜处理
procedure TranslucentBmp(Bmp:TBitmap;
AColor:TColor;ATransparent:Longint);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
//以下截获背景图像
function TForm1.GetBackgroundBmp:TBitmap;
var Scn:TCanvas;
h,w:Integer;
begin
Scn:=TCanvas.Create; //建立整个屏幕的画布
h:=ClientHeight; //窗口的高
w:=ClientWidth; //窗口的宽
Result.Height:=h; //设返回位图的高就是窗口的高
Result.Width:=w; //设返回位图的宽就是窗口的宽
try
Scn.Handle:=GetDC(0);//取得整个屏幕的DC
//以下一行将窗口的背景部分复制到指定的
//画布中,也就是本函数的返回值
Result.Canvas.CopyRect(Rect(0,0,w,h),
Scn,Rect(Left,Top,Left+w,Top+h));
ReleaseDC(0, Scn.handle);
finally
Scn.Free;
end;
end;//以下函数对背景图像进行滤镜处理,
//Bmp是要处理的位图;ATransparent是透明度
procedure TForm1.TranslucentBmp
(Bmp:TBitmap;AColor:TColor;ATransparent:Longint);
var BkColor:COLORREF;
ForeColor:Longint;
R,G,B:Int64;
i,j:Integer;
begin
ForeColor:=ColorToRGB(AColor);
with Bmp.Canvas do
for i:=ClientHeight-1 downto 0 do
for j:=ClientWidth-1 downto 0 do
begin
BkColor:=GetPixel(Handle,j,i); //取得每一像素
R:=Byte(ForeColor)+(Byte(BkColor)
-Byte(ForeColor))*ATransparent;
G:=Byte(ForeColor shr 8)+(Byte
(BkColor shr 8)-Byte(ForeColor shr 8))
*ATransparent;
B:=Byte(ForeColor shr 16)+(Byte
(BkColor shr 16)
-Byte(ForeColor shr 16))*ATransparent;
SetPixelV(Handle,j,i,RGB(R,G,B));//合成像素
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var BackgroundBmp:TBitmap;
begin
try
BackgroundBmp:=Tbitmap.Create;
//建立窗口背景图
BackgroundBmp.PixelFormat:=pf24bit;
//指定该图是24位真彩色
BackgroundBmp:=GetBackgroundBmp;
//取得窗口背景图
TranslucentBmp(BackgroundBmp,clBlack,50);
//对该图像进行滤镜处理
Image1.Picture.Bitmap:=BackgroundBmp;
//将处理过的图像显示出来
finally
BackgroundBmp.Free;
end;
end;procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Close;
end;end.
最好能调用MMX进行优化!