OnDrawCell的问题(100分)

  • 主题发起人 主题发起人 doud
  • 开始时间 开始时间
D

doud

Unregistered / Unconfirmed
GUEST, unregistred user!
我给 TCalendar 加入了OnDrawCell事件,其他都正常,但调用时却没响应,
请问哪出了问题?

unit fhlcalendar;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, Calendar;

type
TDrawCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
Rect: TRect; State: TGridDrawState) of object;
TFHLCalendar = class (TCalendar)
private
FOnDrawCell: TDrawCellEvent;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
property OnMouseDown;
property FixedColor;
property Row;
property Col;
property OnDrawCell: TDrawCellEvent read FOnDrawCell write FOnDrawCell;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents ('FHL', [TFHLCalendar]);
end;

end.

 
这样做是什么意思?
 
加入OnDrawCell事件,例如:
procedure TFHLCalendar.DrawCell (ACol, ARow : Longint; Rect : TRect; AState :
TGridDrawState);
var
TheText : string;
TheDate : TDate;
begin
with TFHLCalendar do begin
TheText := CellText [ACol, ARow];
if (TheText <> '') and (ARow > 0) then
begin
TheDate := EncodeDate (Year, Month, StrToInt (TheText));
if (DayOfWeek (TheDate) = 1) then
Canvas.Font.Color := clRed;
if (DayOfWeek (TheDate) = 7) then
Canvas.Font.Color := clGreen;
end;
with Rect, Canvas do
TextRect (Rect, Left + (Right - Left - TextWidth (TheText)) div 2,
Top + (Bottom - Top - TextHeight (TheText)) div 2, TheText);
end;
end;
 
TDrawCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
Rect: TRect;Text:String; State: TGridDrawState) of object;
 
是加入了OnDrawCell事件,但你的控件并没有调用该事件啊。
你得定义一个方法,你如DrawCell:
procedure TFHLCalendar.DrawCell(...);
begin
if Assigned(FOnDrawCell) then FOnDrawCell(Self);
end;

你还得截获某一消息(包括自定义消息)以调用DrawCell。
const
FHLM_DrawCell = WM_USER + 1;

procedure FHLMDrawCell(var Msg: TMessage); message FHLM_DrawCell;
...

procedure TFHLCalendar.FHLMDrawCell(var Msg: TMessage);
begin
DrawCell;
end;

btw: 其实以上所写是为了规范化,并不一定要这样写。
 
还有,如果是自定义消息,得自己触发该消息,例如:
procedure WMNCPaint(var Msg: TMessage); message WM_NCPAINT;

procedure TFHLCalendar.WMNCPaint(var Msg: TMessage);
begin
inherited;
Perform(FHLM_DrawCell, ...);
end;
 
接受答案了.
 

Similar threads

I
回复
0
查看
633
import
I
I
回复
0
查看
759
import
I
I
回复
0
查看
728
import
I
I
回复
0
查看
515
import
I
I
回复
0
查看
504
import
I
后退
顶部