全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2000
推到 Plurk!
推到 Facebook!

為什麼Button上的TShape不會Refresh没用

尚未結案
bill534
一般會員


發表:38
回覆:51
積分:17
註冊:2007-02-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-01-09 14:47:04 IP:125.230.xxx.xxx 訂閱
這是一個TButton上外加了一個LED(用TShape顯示)的元件,操作大致ok,現在的問題是如下文的紅文部份,在都没
設事件下按Button後其上的Shape顯示不出來,若按下時改變AsBool則正常喔!好奇怪喔!!


unit MyLedButton;

interface

uses StdCtrls, Graphics, ExtCtrls, Classes, SysUtils, Types, Windows, Messages;

type
TMyLedButton = class(TButton)
private
FAsBool: Boolean;
FLayout: TTextLayout;
FON: TColor;
FOFF: TColor;
procedure LedResize;
procedure SetAsBool(const Value: Boolean);
procedure SetLayout(const Value: TTextLayout);
procedure SetON(const Value: TColor);
procedure SetOFF(const Value: TColor);
protected
led: TShape;
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure repaint; override;
published
property AsBool: Boolean read FAsBool write SetAsBool; True時show On的顏色,反之則OFF !
property Layout: TTextLayout read FLayout write SetLayout;
property on: TColor read FON write SetON;
property OFF: TColor read FOFF write SetOFF;
end;

procedure Register;

implementation

{--------------------------------------------------------------------------------------------------}

procedure Register;
begin
RegisterComponents('myVCL', [TMyLedButton]);
end; {Register}

constructor TMyLedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);

led := nil;

FAsBool := False;
FON := clLIME;
FOFF := clGreen;
// if not (csDesigning in ComponentState) then
FLayout := tlCenter;
end;

destructor TMyLedButton.Destroy;
begin
if led <> nil then
FreeandNil(led);

inherited;
end;

procedure TMyLedButton.LedResize;
begin
if led <> nil then
begin
if FLayout = tlTop then
led.Top := ClientHeight - 11
else
led.Top := 5;
led.Left := 4;
led.Width := ClientWidth - 9;
led.Height := 6;
end;
end;

procedure TMyLedButton.repaint;
begin
// TODO -cMM: TMyLedButton.repaint default body inserted
inherited;

if led <> nil then
led.Refresh;
end;

procedure TMyLedButton.SetAsBool(const Value: Boolean);
begin
if FAsBool <> Value then
begin
FAsBool := Value;
if FLayout <> tlCenter then
begin
if led <> nil then
begin
if FAsBool then
led.Brush.Color := FON
else
led.Brush.Color := FOFF;

led.Refresh;
end;
end;
end;
end;

procedure TMyLedButton.SetLayout(const Value: TTextLayout);
var
defstyle: dWord;
begin
if FLayout <> Value then
begin
FLayout := Value;
//
defstyle := GetWindowLong(Handle, GWL_STYLE) and (not BS_TOP) and (not BS_BOTTOM) and (not BS_CENTER);

if FLayout = tlCenter then
begin
SetWindowLong(Handle, GWL_STYLE, defstyle or BS_CENTER);
if led <> nil then
begin
led.Free;
led := nil;
end;
end
else
begin
if FLayout = tlTop then
SetWindowLong(Handle, GWL_STYLE, defstyle or BS_TOP)
else
SetWindowLong(Handle, GWL_STYLE, defstyle or BS_BOTTOM);
//
if led = nil then
begin
led := TShape.Create(self);
led.Parent := self;
LedResize;
led.Brush.Color := FOFF;
led.BringToFront;
led.Tag := Width Height;
end;
end;
Refresh;
end;
end;

procedure TMyLedButton.SetOFF(const Value: TColor);
begin
if FOFF <> Value then
begin
FOFF := Value;
if led <> nil then
begin
led.Brush.Color := FOFF;
led.Refresh;
end;
end;
end;

procedure TMyLedButton.SetON(const Value: TColor);
begin
if FON <> Value then
begin
FON := Value;
if led <> nil then
begin
led.Brush.Color := FON;
led.Refresh;
end;
end;
end;

procedure TMyLedButton.WndProc(var Message: TMessage);
begin
case Message.Msg of
WM_LBUTTONDOWN, WM_LBUTTONDBLCLK: 事件有反應但程式碼也執行了但就是Show不出來!!
begin
if led <> nil then
begin
led.BringToFront;
led.Refresh;
end;
end;

WM_SIZE:
begin
if led <> nil then
begin
if led.tag <> (Width Height) then
begin
self.LedResize;
led.Tag := Width Height;
end;
end;
end;
end;

inherited WndProc(Message);
end;

end.
chenyk
高階會員


發表:14
回覆:95
積分:171
註冊:2002-07-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-02-18 16:27:25 IP:125.230.xxx.xxx 訂閱
TShape的顏色只要這樣就可以設定

procedure TBtn.SetShape(Value:boolean);
begin
if( Value)then
shape.color := clred
else
shape.color := clgreen;
Invalidate;
end;

至於重繪的部份,直接 override OnPaint() 事件
系統時間:2024-04-19 8:21:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!