独
独孤剑
Unregistered / Unconfirmed
GUEST, unregistred user!
谁能帮我看看是什么原因啊:
下面的控件,放到TPanel上时,若改变Panel的大小或位置,该控件就会消失,
可能和该控件的透明有点关联,大家研究研究啊
《两个贴,一共200分,谁解决立马给分》
unit MyControls;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, math, Menus;
type
TMyImageControl = Class(TCustomControl)
Private
FText: String;
FBakBitmap: TBitmap;
FMovingBitMap: TBitmap;
FTransparentColor: TColor;
FMargin: Integer;
FModuleName: String;
Function GetText: String;
Procedure SetText(Value: String);
Procedure SetBitmap(Value: TBitmap);
Function GetFont: TFont;
Procedure SetFont(Value: TFont);
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd);message WM_EraseBkgnd;
Protected
Procedure MouseMove(Shift: TShiftState; X,Y: Integer); Override;
Procedure SetBounds(ALeft,ATop,AWidth,AHeight: Integer);Override;
procedure CreateParams(var Params: TCreateParams); Override;
procedure Click; override;
Public
Constructor Create(Aowner: TComponent); Override;
Destructor Destroy; Override;
Procedure Paint; override;
Published
Property Bitmap: TBitmap Read FBakBitmap Write SetBitmap;
Property Font: TFont Read GetFont Write SetFont;
Property Text:String Read GetText Write SetText;
Property OnClick;
Property OnMouseMove;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('PCP', [TMyImageControl]);
end;
Function TMyImageControl.GetFont: TFont;
begin
Result := Canvas.Font;
end;
Procedure TMyImageControl.SetFont(Value: TFont);
begin
if Value <> nil then
begin
Canvas.Font.Assign(Value);
SetBounds(Left,Top,Width,Height);
Paint;
end;
end;
Function TMyImageControl.GetText: String;
begin
Result := FText;
end;
Procedure TMyImageControl.SetText(Value: String);
begin
if Value <> FText then
begin
FText := Value;
SetBounds(Left, Top, Width, Height);
Paint;
end;
end;
Procedure TMyImageControl.SetBitmap(Value: TBitmap);
begin
if Value <> nil then
begin
FBakBitmap.Assign(Value);
SetBounds(Left,Top,Width,Height);
Paint;
end;
end;
procedure TMyImageControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1
end;
Procedure TMyImageControl.MouseMove(Shift: TShiftState; X,Y: Integer);
begin
if (GetCapture <> Handle) and PtInRect(ClientRect,Point(X,Y)) then
SetCapture(Handle);
if (GetCapture = Handle) and Not PtInRect(ClientRect,Point(X,Y)) then
ReleaseCapture;
if PtInRect(ClientRect,Point(X,Y)) Then
begin
Screen.Cursor:= crHandPoint;
end else
begin
Screen.Cursor:= crDefault;
end;
inherited MouseMove(Shift,X,Y);
end;
Procedure TMyImageControl.SetBounds(ALeft,ATop,AWidth,AHeight: Integer);
var lTextWidth, lTextHeight: Integer;
begin
if FBakBitmap <> nil then
begin
try
lTextWidth:= Canvas.TextWidth(FText);
lTextHeight:= Canvas.TextHeight(FText);
except
lTextWidth := 0;
lTextHeight := 0;
end;
AWidth := Max(FBakBitmap.Width, lTextWidth);
AHeight := FBakbitmap.Height + lTextHeight+5;
if (AWidth = 0) or (AHeight = 0) then
begin
AWidth:= 60;
AHeight:= 60;
end;
end;
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TMyImageControl.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
end;
Constructor TMyImageControl.Create(Aowner: TComponent);
begin
Inherited Create(AOwner);
ControlStyle:= ControlStyle + [csOpaque];
FBakBitmap:= TBitmap.Create;
FBakBitmap.Transparent:= True;
end;
Destructor TMyImageControl.Destroy;
begin
FBakBitmap.Free;
Inherited Destroy;
end;
Procedure TMyImageControl.Paint;
var lBitmap: TBitmap;
lTextWidth: Integer;
begin
lBitMap:= FBakBitmap;
lTextWidth := Canvas.TextWidth(FText);
Canvas.Brush.Color:= Color;
Canvas.FillRect(ClientRect);
SetBkMode(Canvas.Handle, TRANSPARENT);
if lBitmap.Width > lTextWidth then
begin
Canvas.Draw(0, 0, lBitmap);
Canvas.TextOut((lBitmap.Width - lTextWidth) div 2,lBitmap.Height, FText);
end
else begin
Canvas.Draw((lTextWidth - lBitmap.Width) div 2, 0, lbitmap);
Canvas.TextOut(0, lBitmap.Height, FText);
end;
end;
procedure TMyImageControl.Click;
begin
Screen.Cursor:= crDefault;
inherited Click;
end;
end.
下面的控件,放到TPanel上时,若改变Panel的大小或位置,该控件就会消失,
可能和该控件的透明有点关联,大家研究研究啊
《两个贴,一共200分,谁解决立马给分》
unit MyControls;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, math, Menus;
type
TMyImageControl = Class(TCustomControl)
Private
FText: String;
FBakBitmap: TBitmap;
FMovingBitMap: TBitmap;
FTransparentColor: TColor;
FMargin: Integer;
FModuleName: String;
Function GetText: String;
Procedure SetText(Value: String);
Procedure SetBitmap(Value: TBitmap);
Function GetFont: TFont;
Procedure SetFont(Value: TFont);
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd);message WM_EraseBkgnd;
Protected
Procedure MouseMove(Shift: TShiftState; X,Y: Integer); Override;
Procedure SetBounds(ALeft,ATop,AWidth,AHeight: Integer);Override;
procedure CreateParams(var Params: TCreateParams); Override;
procedure Click; override;
Public
Constructor Create(Aowner: TComponent); Override;
Destructor Destroy; Override;
Procedure Paint; override;
Published
Property Bitmap: TBitmap Read FBakBitmap Write SetBitmap;
Property Font: TFont Read GetFont Write SetFont;
Property Text:String Read GetText Write SetText;
Property OnClick;
Property OnMouseMove;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('PCP', [TMyImageControl]);
end;
Function TMyImageControl.GetFont: TFont;
begin
Result := Canvas.Font;
end;
Procedure TMyImageControl.SetFont(Value: TFont);
begin
if Value <> nil then
begin
Canvas.Font.Assign(Value);
SetBounds(Left,Top,Width,Height);
Paint;
end;
end;
Function TMyImageControl.GetText: String;
begin
Result := FText;
end;
Procedure TMyImageControl.SetText(Value: String);
begin
if Value <> FText then
begin
FText := Value;
SetBounds(Left, Top, Width, Height);
Paint;
end;
end;
Procedure TMyImageControl.SetBitmap(Value: TBitmap);
begin
if Value <> nil then
begin
FBakBitmap.Assign(Value);
SetBounds(Left,Top,Width,Height);
Paint;
end;
end;
procedure TMyImageControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1
end;
Procedure TMyImageControl.MouseMove(Shift: TShiftState; X,Y: Integer);
begin
if (GetCapture <> Handle) and PtInRect(ClientRect,Point(X,Y)) then
SetCapture(Handle);
if (GetCapture = Handle) and Not PtInRect(ClientRect,Point(X,Y)) then
ReleaseCapture;
if PtInRect(ClientRect,Point(X,Y)) Then
begin
Screen.Cursor:= crHandPoint;
end else
begin
Screen.Cursor:= crDefault;
end;
inherited MouseMove(Shift,X,Y);
end;
Procedure TMyImageControl.SetBounds(ALeft,ATop,AWidth,AHeight: Integer);
var lTextWidth, lTextHeight: Integer;
begin
if FBakBitmap <> nil then
begin
try
lTextWidth:= Canvas.TextWidth(FText);
lTextHeight:= Canvas.TextHeight(FText);
except
lTextWidth := 0;
lTextHeight := 0;
end;
AWidth := Max(FBakBitmap.Width, lTextWidth);
AHeight := FBakbitmap.Height + lTextHeight+5;
if (AWidth = 0) or (AHeight = 0) then
begin
AWidth:= 60;
AHeight:= 60;
end;
end;
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TMyImageControl.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
end;
Constructor TMyImageControl.Create(Aowner: TComponent);
begin
Inherited Create(AOwner);
ControlStyle:= ControlStyle + [csOpaque];
FBakBitmap:= TBitmap.Create;
FBakBitmap.Transparent:= True;
end;
Destructor TMyImageControl.Destroy;
begin
FBakBitmap.Free;
Inherited Destroy;
end;
Procedure TMyImageControl.Paint;
var lBitmap: TBitmap;
lTextWidth: Integer;
begin
lBitMap:= FBakBitmap;
lTextWidth := Canvas.TextWidth(FText);
Canvas.Brush.Color:= Color;
Canvas.FillRect(ClientRect);
SetBkMode(Canvas.Handle, TRANSPARENT);
if lBitmap.Width > lTextWidth then
begin
Canvas.Draw(0, 0, lBitmap);
Canvas.TextOut((lBitmap.Width - lTextWidth) div 2,lBitmap.Height, FText);
end
else begin
Canvas.Draw((lTextWidth - lBitmap.Width) div 2, 0, lbitmap);
Canvas.TextOut(0, lBitmap.Height, FText);
end;
end;
procedure TMyImageControl.Click;
begin
Screen.Cursor:= crDefault;
inherited Click;
end;
end.