制作了一个Edit透明控件,但在WIN98和WIN200下可以透明,在WIN XP下却不能完全透明,有文字出现的地方不能透明,但是用同样方法使memo透明就没有这个问题存在,不知道什么原因,真纳闷啊! 哪位高手可以帮我!或者有其它更好的方法,谢谢帮忙! 我的控件代码如下:
unit EditPro;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics,Dialogs;
const
TWM_Invalidate = WM_USER+1;
type
TEditPro = class(TEdit)
private
FAlignment: TAlignment; //水平对齐
FTransparent: Boolean; //透明
procedure SetAlignment(Value: TAlignment);
procedure TInvalidate(var Message: TMessage); message TWM_Invalidate;
procedure CNCTLCOLOREDIT(var Message: TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WMMove(var Message: TMessage); message WM_MOVE;
{ Private declarations }
protected
procedure CreateWnd; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
procedure DoEnter; override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
procedure Invalidate; override;
{ Public declarations }
published
property Alignment: TAlignment Read FAlignment Write SetAlignment Default taLeftJustify; //水平对齐属性
{ Published declarations }
end;
procedure Register;
implementation
constructor TEditPro.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
Width := 70;
Height := 25;
FTransparent := True;
end;
procedure TEditPro.CreateWnd;
begin
inherited CreateWnd;
if FTransparent then
begin
SetWindowLong(Parent.Handle, GWL_STYLE,
GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
end;
end;
procedure TEditPro.SetAlignment(Value: TAlignment);
begin
if Value <> FAlignment then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure TEditPro.TInvalidate(var Message:TMessage);
var
r: TRect;
begin
if (Parent <> nil) and FTransparent then
begin
r := ClientRect;
r.TopLeft := Parent.ScreenToClient(ClientToScreen(r.TopLeft));
r.BottomRight := Parent.ScreenToClient(ClientToScreen(r.BottomRight));
RedrawWindow(Handle, nil, 0, RDW_FRAME + RDW_INVALIDATE);
end;
end;
procedure TEditPro.CNCTLCOLOREDIT(var Message: TWMCTLCOLOREDIT);
begin
if FTransparent then
with Message do
begin
SetBkMode(ChildDC, Windows.TRANSPARENT);
Result := GetStockObject(HOLLOW_BRUSH)
end
else
inherited;
end;
procedure TEditPro.WMEraseBkgnd(var Message: TWMERASEBKGND);
begin
if FTransparent then
PostMessage(Handle, TWM_Invalidate, 0, 0)
else
inherited;
end;
procedure TEditPro.WMMove(var message: TMessage);
begin
inherited;
if FTransparent then
SendMessage(Handle, TWM_Invalidate, 0, 0)
else
Invalidate;
end;
procedure TEditPro.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of word = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[FAlignment];
ExStyle := ExStyle or WS_EX_TRANSPARENT;
end;
end;
procedure TEditPro.DoExit;
begin
inherited;
FTransparent := True;
SetCursor(0);
RecreateWnd;
end;
procedure TEditPro.DoEnter;
var
exstyle, stdstyle: LongInt;
begin
inherited;
FTransparent := False;
StdStyle:= Windows.GetWindowLong(Parent.handle, GWL_EXSTYLE);
exStyle:= StdStyle and not WS_EX_TRANSPARENT;
Windows.SetWindowLong(Parent.handle, GWL_EXSTYLE, exStyle);
invalidate;
end;
procedure TEditPro.Invalidate;
begin
if FTransparent then
SendMessage(Handle, TWM_Invalidate, 0, 0)
else
inherited;
end;
procedure Register;
begin
RegisterComponents('Standard', [TEditPro]);
end;
end.
unit EditPro;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics,Dialogs;
const
TWM_Invalidate = WM_USER+1;
type
TEditPro = class(TEdit)
private
FAlignment: TAlignment; //水平对齐
FTransparent: Boolean; //透明
procedure SetAlignment(Value: TAlignment);
procedure TInvalidate(var Message: TMessage); message TWM_Invalidate;
procedure CNCTLCOLOREDIT(var Message: TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WMMove(var Message: TMessage); message WM_MOVE;
{ Private declarations }
protected
procedure CreateWnd; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
procedure DoEnter; override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
procedure Invalidate; override;
{ Public declarations }
published
property Alignment: TAlignment Read FAlignment Write SetAlignment Default taLeftJustify; //水平对齐属性
{ Published declarations }
end;
procedure Register;
implementation
constructor TEditPro.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
Width := 70;
Height := 25;
FTransparent := True;
end;
procedure TEditPro.CreateWnd;
begin
inherited CreateWnd;
if FTransparent then
begin
SetWindowLong(Parent.Handle, GWL_STYLE,
GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
end;
end;
procedure TEditPro.SetAlignment(Value: TAlignment);
begin
if Value <> FAlignment then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure TEditPro.TInvalidate(var Message:TMessage);
var
r: TRect;
begin
if (Parent <> nil) and FTransparent then
begin
r := ClientRect;
r.TopLeft := Parent.ScreenToClient(ClientToScreen(r.TopLeft));
r.BottomRight := Parent.ScreenToClient(ClientToScreen(r.BottomRight));
RedrawWindow(Handle, nil, 0, RDW_FRAME + RDW_INVALIDATE);
end;
end;
procedure TEditPro.CNCTLCOLOREDIT(var Message: TWMCTLCOLOREDIT);
begin
if FTransparent then
with Message do
begin
SetBkMode(ChildDC, Windows.TRANSPARENT);
Result := GetStockObject(HOLLOW_BRUSH)
end
else
inherited;
end;
procedure TEditPro.WMEraseBkgnd(var Message: TWMERASEBKGND);
begin
if FTransparent then
PostMessage(Handle, TWM_Invalidate, 0, 0)
else
inherited;
end;
procedure TEditPro.WMMove(var message: TMessage);
begin
inherited;
if FTransparent then
SendMessage(Handle, TWM_Invalidate, 0, 0)
else
Invalidate;
end;
procedure TEditPro.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of word = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[FAlignment];
ExStyle := ExStyle or WS_EX_TRANSPARENT;
end;
end;
procedure TEditPro.DoExit;
begin
inherited;
FTransparent := True;
SetCursor(0);
RecreateWnd;
end;
procedure TEditPro.DoEnter;
var
exstyle, stdstyle: LongInt;
begin
inherited;
FTransparent := False;
StdStyle:= Windows.GetWindowLong(Parent.handle, GWL_EXSTYLE);
exStyle:= StdStyle and not WS_EX_TRANSPARENT;
Windows.SetWindowLong(Parent.handle, GWL_EXSTYLE, exStyle);
invalidate;
end;
procedure TEditPro.Invalidate;
begin
if FTransparent then
SendMessage(Handle, TWM_Invalidate, 0, 0)
else
inherited;
end;
procedure Register;
begin
RegisterComponents('Standard', [TEditPro]);
end;
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货