線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1929
推到 Plurk!
推到 Facebook!

奇怪在TCustomControl內建的Timer不會動作!

答題得分者是:jow
bill534
一般會員


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

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-07-02 17:30:36 IP:220.130.xxx.xxx 訂閱
各位先進我的問題是如下建立了一個Timer元件,但它不會觸發事件,為什麼呢!

TMyAlarmBar = class(TCustomControl)
private
FAlarm: Integer;
FDescription, FAlarmText: string;
FFont: TFont;
{ Private declarations }
fPaperColor: TColor;
fLineColor: TColor;
FMarginPosition: integer;
FOnClickBar: TNotifyEvent;
fPageLeft: integer;
FTimer: TTimer;

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

ControlStyle := ControlStyle [csAcceptsControls];
fPaperColor := clWhite;
fLineColor := $00F6ACA5;
fMarginPosition := 80;
fPageLeft := 28;
FAlarm := 0;
FAlarmText := '';
FDescription := '';
//
FFont := TFont.Create;
//
FTimer := TTimer.Create(self);
FTimer.Interval := 750;
FTimer.OnTimer := OnTimer;
FTimer.Enabled:=True;

end; {constructor}
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-07-02 19:01:33 IP:123.193.xxx.xxx 未訂閱
FTimer.OnTimer := OnTimer;

OnTimer這個位置應該Assign一個型態為
TNotifyEvent的 Event Handler

FTimer.OnTiimer := DO_OnTimer;

procedure TMyAlarmBar.DO_OnTimer(Sender: TObject);
begin
...
end;

bill534
一般會員


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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-07-02 21:28:08 IP:125.230.xxx.xxx 訂閱
我有加,但是不會觸發!
procedure TMyAlarmBar.OnTimer(Sender: TObject);
begin
Beep;
end;
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-07-02 23:36:50 IP:123.193.xxx.xxx 未訂閱
試試! 可以聽到 Beep()指令所發出來的聲音.......

[code delphi]
unit fMain;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;

type
TMyControl = class(TCustomControl)
protected
FTimer: TTimer;
procedure DO_OnTimer(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
property Font;//既有屬性,只需開放其存取權限
end;

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
MyControl: TMyControl;
public
end;

var
Form1: TForm1;

implementation

uses Math;

{$R *.dfm}

{ TMyControl }

constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
Parent := TWinControl(AOwner);
Color := clBlack;
Font.Name := 'ARIAL';
Font.Color := clLime;
Width := 200;
Height := 100;
FTimer := TTimer.Create(Self);
FTimer.OnTimer := DO_OnTimer;
end;

destructor TMyControl.Destroy;
begin
// FreeAndNil(FTimer);<--其實這是不必要的
inherited;
end;

procedure TMyControl.DO_OnTimer(Sender: TObject);
begin
Beep;
Invalidate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
MyControl := TMyControl.Create(Self);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// FreeAndNil(MyControl);<--其實這也是不必要的
end;

procedure TMyControl.Paint;
var
S: string;
begin
inherited;
S := FormatDateTime('hh:nn:ss', Now);
Canvas.Lock;
try
Canvas.Brush.Color := Color;
Canvas.Brush.Style := bsSolid;
Canvas.Font.Assign(Self.Font);
Canvas.Font.Height := Min(ClientHeight div 2, ClientWidth * 2 div Length(S));
Canvas.TextRect(ClientRect,
(ClientWidth-Canvas.TextWidth(S)) shr 1,
(ClientHeight-Canvas.TextHeight('0')) shr 1,S);
finally
Canvas.Unlock;
end;
end;

end.
[/code]

謹供參考. ^_^
編輯記錄
jow 重新編輯於 2008-07-02 23:46:44, 註解 無‧
bill534
一般會員


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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-07-03 07:26:50 IP:125.230.xxx.xxx 訂閱
奇怪還是不會叫,全跟你寫的一樣了啦!

unit MyAlarmBar;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, QExtCtrls;

type
TMyAlarmBar = class(TCustomControl)
private
FAlarm: Integer;
FDescription, FAlarmText: string;
{ Private declarations }
fPaperColor: TColor;
fLineColor: TColor;
FMarginPosition: integer;
FOnClickBar: TNotifyEvent;
fPageLeft: integer;
procedure SetfPaperColor(Value: TColor);
procedure SetfLineColor(Value: TColor);
procedure SetfMarginPosition(Value: integer);
protected
FTimer: TTimer;
procedure Paint; override;
procedure DrawNotebook;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure DoOnTimer(Sender: TObject);
procedure WMEraseBkGnd(var Message: TMessage); message WM_EraseBkGnd;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AlarmDescription(nAlarm: integer; nDescription: string);
published
property Anchors;
property Align;
property Description: string read FDescription write FDescription;
property Font;
property PaperColor: TColor read fPaperColor write SetfPaperColor;
property LineColor: TColor read fLineColor write SetfLineColor;
property MarginPosition: integer read FMarginPosition write SetfMarginPosition;
property OnDblClick;

end;

procedure Register;

implementation

//{$R *.RES}

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

constructor TMyAlarmBar.Create(AOwner: TComponent);
begin
inherited;
Parent := TWinControl(AOwner);
//
ControlStyle := ControlStyle [csAcceptsControls];
fPaperColor := clWhite;
fLineColor := $00F6ACA5;
fMarginPosition := 80;
fPageLeft := 28;
FAlarm := 0;
FAlarmText := '';
FDescription := '';
//
Font.Size := 12;
Font.Name := 'ARIAL';
//
FTimer := TTimer.Create(self);
FTimer.Interval := 750;
FTimer.OnTimer := DoOnTimer;
FTimer.Enabled := True;

end; {constructor}

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

destructor TMyAlarmBar.Destroy;
begin
inherited;
end; {destructor}

procedure TMyAlarmBar.AlarmDescription(nAlarm: integer; nDescription: string);
begin
if nAlarm <> FAlarm then
begin
FAlarm := nAlarm;
if FAlarm > 0 then
begin
FTimer.Enabled := True;
FAlarmText := Inttostr(FAlarm);
FDescription := nDescription;
end
else
begin
FTimer.Enabled := False;
FAlarmText := '';
FDescription := '';
end;
Paint;
end;
end;

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

procedure TMyAlarmBar.SetfPaperColor(Value: TColor);
begin
if (fPaperColor <> Value) then
begin
fPaperColor := Value;
Invalidate;
end; {if}
end; {SetfPaperColor}

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

procedure TMyAlarmBar.SetfLineColor(Value: TColor);
begin
if (fLineColor <> Value) then
begin
fLineColor := Value;
Invalidate;
end; {if}
end; {SetfLineColor}

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

procedure TMyAlarmBar.SetfMarginPosition(Value: integer);
begin
if (fMarginPosition <> Value) then
begin
fMarginPosition := Value;
Invalidate;
end; {if}
end; {SetfMarginPosition}

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

procedure TMyAlarmBar.WMEraseBkGnd(var Message: TMessage);
begin
Message.Result := 1;
end; {WMEraseBkGnd}

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

procedure TMyAlarmBar.DrawNotebook;
const
Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
LinePosition: integer;
HoleVPosition: integer;
BinderHeight: integer;
BinderPosition: integer;
DrawStyle: Longint;
Rect: TRect;
begin
canvas.Lock;
try
Canvas.Font.Assign(Font);
with Canvas do
begin
Brush.Color := fPaperColor; // Draw paper background.
FillRect(ClientRect);

Pen.Color := fLineColor;
// draw edge
MoveTo(0, 0);
LineTo(0, ClientHeight - 1);
MoveTo(ClientWidth - 1, 0);
LineTo(ClientWidth - 1, ClientHeight - 1);
MoveTo(0, 0);
LineTo(ClientWidth - 1, 0);
MoveTo(0, ClientHeight - 1);
LineTo(ClientWidth - 1, ClientHeight - 1);
MoveTo(FMarginPosition, 0);
LineTo(FMarginPosition, ClientHeight - 1);

// draw caption1
Brush.Style := bsClear;
Rect.Left := 1;
Rect.Top := 1;
Rect.Right := FMarginPosition;
Rect.Bottom := ClientHeight - 1;
DrawStyle := DT_VCENTER or DT_CENTER or DT_SINGLELINE; // or DT_CALCRECT;
DrawText(Handle, PChar(FAlarmText), Length(FAlarmText), Rect, DrawStyle);
// draw caption2
Brush.Style := bsClear;
Rect.Left := FMarginPosition 6;
Rect.Top := 1;
Rect.Right := ClientWidth - 6;
Rect.Bottom := ClientHeight - 1;
DrawStyle := DT_VCENTER or DT_LEFT or DT_SINGLELINE; // or DT_CALCRECT;
DrawText(Handle, PChar(FDescription), Length(FDescription), Rect, DrawStyle);
end; {with}
finally
canvas.Unlock;
end;
end; {DrawNotebook}

procedure TMyAlarmBar.Notification(AComponent: TComponent; Operation:
TOperation);
begin
inherited Notification(Acomponent, Operation);

if Operation = opRemove then
begin
end;
end;

procedure TMyAlarmBar.DoOnTimer(Sender: TObject);
begin
Beep;
end;


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

procedure TMyAlarmBar.Paint;
begin
DrawNotebook;
end; {Paint}

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

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

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

end.
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-07-03 07:58:01 IP:123.193.xxx.xxx 未訂閱
錯誤引用到 TTimer的實作碼...

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls{, QExtCtrls};
bill534
一般會員


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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-07-03 09:44:38 IP:220.130.xxx.xxx 訂閱
謝謝啦!愛死你了!親親!
要不要來我們公司上班!旭東機械http://www.wisepioneer.com.tw/index.htm
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#8 引用回覆 回覆 發表時間:2008-07-03 10:12:25 IP:118.169.xxx.xxx 訂閱
不知道要花多少錢才請的起jow前輩:P


===================引 用 bill534 文 章===================
謝謝啦!愛死你了!親親!
要不要來我們公司上班!旭東機械http://www.wisepioneer.com.tw/index.htm
系統時間:2024-04-19 12:13:42
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!