求制作透明radiobutton,checkbox,button的方法(100分)

  • 主题发起人 主题发起人 白乐天
  • 开始时间 开始时间

白乐天

Unregistered / Unconfirmed
GUEST, unregistred user!
不要第三方控件,我想自己写。
但是遇到一些问题,radiobutton,checkbox,button都是TButtonControl下继承的,都是调用CreateParams生成的,即使重载WMPaint过程,也没办法控制它怎么画,比如radiobutton左边的圈和右边的字,如何生成它们透明呢?

最好有例子代码,实在不行说大致方法也可以。请诸位高手赐教!
 
参考 Label 的 Transparent属性
 
无法参考,label是TGraphicControl下继承的,这个我是会的。

我要的是TWinControl下的不从TCustomControl继承的控件,radiobutton,checkbox,button这些都是,它们的生成都是由CreateParams。我就卡在这里。我记得有人提到过透明radiobutton,只是居然在大富翁没找到。
 
我这里有一个继承Tedit的透明Edit,你可以参考下
unit TransparentEdit;

{$R-}
interface

uses
Windows, Messages, Controls, StdCtrls, Classes;

const
TMWM__SpecialInvalidate = WM_USER + 1111;

type
TTransparentEdit = class(TEdit)
private
FAlignment: TAlignment; //增加对齐属性

procedure SpecialInvalidate(var Message:TMessage); message TMWM__SpecialInvalidate;
procedure CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT;
procedure WMSetText(var Message:TWMSetText); message WM_SETTEXT;
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
protected
function GetAlignment: TAlignment; virtual;
procedure SetAlignment(newValue: TAlignment); virtual;
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override;
published
property Alignment: TAlignment read GetAlignment write SetAlignment;
end;

procedure Register;

implementation

{ TTransparentEdit }

procedure TTransparentEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
begin
with Message do
begin
SetBkMode(ChildDC,TRANSPARENT);
Result:=GetStockObject(HOLLOW_BRUSH)
end
end;

procedure TTransparentEdit.WMSetText(var Message:TWMSetText);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0)
end;

procedure TTransparentEdit.SpecialInvalidate(var Message:TMessage);
var r:TRect;
begin
if Parent<>nil then
begin
r:=ClientRect;
r.TopLeft:=Parent.ScreenToClient(ClientToScreen(r.TopLeft));
r.BottomRight:=Parent.ScreenToClient(ClientToScreen(r.BottomRight));
InvalidateRect(Parent.Handle,@r,true);
RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE)
end;
end;


procedure TTransparentEdit.WMKeyDown(var Message: TWMKeyDown);
begin
SendMessage(Handle,TMWM__SpecialInvalidate,0,0);
inherited;
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;

procedure TTransparentEdit.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1;
end;

constructor TTransparentEdit.Create(AOwner: TComponent);
begin
inherited;
FAlignment := taLeftJustify;
ControlStyle := [csDesignInteractive, csClickEvents, csCaptureMouse,
csOpaque, csReplicatable, csNoStdEvents, csSetCaption];// csDoubleClicks,
end;

procedure TTransparentEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
with Params do
begin
ExStyle:=ExStyle or WS_EX_TRANSPARENT and not WS_EX_WINDOWEDGE
and not WS_EX_STATICEDGE and not WS_EX_DLGMODALFRAME and not WS_EX_CLIENTEDGE;

Style := Style or Alignments[FAlignment];//增加对齐方式
end;

end;

function TTransparentEdit.GetAlignment: TAlignment;
{ Returns the value of data member FAlignment. }
begin
result := FAlignment;
end; { GetAlignment }

procedure TTransparentEdit.SetAlignment(newValue: TAlignment);
{ Sets data member FAlignment to newValue. }
begin
if FAlignment <> newValue then
begin
FAlignment := newValue;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end; { SetAlignment }


procedure Register;
begin
RegisterComponents('My Component', [TTransparentEdit]);
end;

end.
 
ddddddddddddddddd
 
http://www.delphibbs.com/keylife/iblog_show.asp?xid=19635
 
后退
顶部