如何让一个Panel始终居中(相对于Parent) |
答題得分者是:OsX
|
aKnightChen@Hotmail.com
一般會員 發表:62 回覆:57 積分:23 註冊:2003-06-13 發送簡訊給我 |
|
OsX
版主 發表:6 回覆:151 積分:111 註冊:2003-05-03 發送簡訊給我 |
繼承 TControl 以下都有一個屬性為 Align 的屬性, 裏面有一項對齊的方式為 alCustom
( 開發者自訂對齊方式, 就像是你所提到的特殊對齊方式, 這須要寫程式碼控制 ) 1. 假設 Panel 的 Parent 為 Form1 2. 設定 Pane1.Align = alCustom 3. 在 Form1 的 Proected 區塊中覆寫 CustomAlignPosition 函式 [code delphi] type TForm1 = class(TForm) Panel1: TPanel protected procedure CustomAlignPosition(Control: TControl; var NewLeft, NewTop, NewWidth, NewHeight: Integer; var AlignRect: TRect; AlignInfo: TAlignInfo); override; public end implementation procedure TForm1.CustomAlignPosition(Control: TControl; var NewLeft, NewTop, NewWidth, NewHeight: Integer; var AlignRect: TRect; AlignInfo: TAlignInfo); begin inherited; if ( Control = Panel1 ) then begin NewLeft := ( AlignRect.Right - NewWidth ) div 2; end; end; [/code]
編輯記錄
OsX 重新編輯於 2008-06-30 11:32:39, 註解 無‧
|
aKnightChen@Hotmail.com
一般會員 發表:62 回覆:57 積分:23 註冊:2003-06-13 發送簡訊給我 |
感谢Osx大哥的指点, 不过,我想做成一个控件,这样,不用影响其它地方的源代码. 我做了一个控件,不过,很丑,没有过滤,(不知道怎么过滤消息)(效率很低) 帮看看,哪里可以改时一下.
感谢Osx大哥的指点, 不过,我想做成一个控件,这样,不用影响其它地方的源代码. 我做了一个控件,不过,很丑,没有过滤,(不知道怎么过滤消息)(效率很低) 帮看看,哪里可以改进一下. unit kCPal; interface uses Windows, Messages, Classes, ExtCtrls,Graphics,Controls,AppEvnts,SysUtils; type TkCPal = class(TPanel) private FkAutoCenterPosition:Boolean; FApplicationEvents: TApplicationEvents; protected procedure pSetPalCenter; procedure pSetkAutoCenterPosition(aAutoCenterPosition:Boolean); procedure ApplicationEventsMessage(var Msg: tagMSG;var Handled: Boolean); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Paint;override; published property kAutoCenterPosition:Boolean read FkAutoCenterPosition write pSetkAutoCenterPosition; end; procedure Register; implementation procedure Register; begin RegisterComponents('kERP', [TkCPal]); end; constructor TkCPal.Create(AOwner: TComponent); begin inherited; if not (csDesigning in ComponentState) then begin FApplicationEvents:=TApplicationEvents.Create(aOwner); FApplicationEvents.OnMessage:=ApplicationEventsMessage; end; kAutoCenterPosition:=False; Alignment:=taCenter; BevelOuter:=bvNone; end; procedure TkCPal.ApplicationEventsMessage(var Msg: tagMSG;var Handled: Boolean); begin pSetPalCenter; end; destructor TkCPal.Destroy; begin if not (csDesigning in ComponentState) then FApplicationEvents.Free; inherited; end; procedure TkCPal.pSetPalCenter; begin if (Parent<>nil) and FkAutoCenterPosition then begin Top :=(Parent.ClientHeight-Height) div 2; Left:=(Parent.ClientWidth -Width ) div 2; end; end; procedure TkCPal.pSetkAutoCenterPosition(aAutoCenterPosition:Boolean); begin FkAutoCenterPosition:=aAutoCenterPosition; pSetPalCenter; end; procedure TkCPal.Paint; begin Caption:=''; if (csDesigning in ComponentState) then pSetPalCenter; inherited; end; initialization registerClasses([TApplicationEvents]); end.
編輯記錄
aKnightChen@Hotmail.com 重新編輯於 2008-06-30 22:11:59, 註解 無‧
|
aKnightChen@Hotmail.com
一般會員 發表:62 回覆:57 積分:23 註冊:2003-06-13 發送簡訊給我 |
感谢Osx大哥的指点, 不过,我想做成一个控件,这样,不用影响其它地方的源代码. 我做了一个控件,不过,很丑,没有过滤,(不知道怎么过滤消息)(效率很低) 帮看看,哪里可以改进一下.
unit kCPal; interface uses Windows, Messages, Classes, ExtCtrls,Graphics,Controls,AppEvnts,SysUtils; type TkCPal = class(TPanel) private FkAutoCenterPosition:Boolean; FApplicationEvents: TApplicationEvents; protected procedure pSetPalCenter; procedure pSetkAutoCenterPosition(aAutoCenterPosition:Boolean); procedure ApplicationEventsMessage(var Msg: tagMSG;var Handled: Boolean); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Paint;override; published property kAutoCenterPosition:Boolean read FkAutoCenterPosition write pSetkAutoCenterPosition; end; procedure Register; implementation procedure Register; begin RegisterComponents('kERP', [TkCPal]); end; constructor TkCPal.Create(AOwner: TComponent); begin inherited; if not (csDesigning in ComponentState) then begin FApplicationEvents:=TApplicationEvents.Create(aOwner); FApplicationEvents.OnMessage:=ApplicationEventsMessage; end; kAutoCenterPosition:=False; Alignment:=taCenter; BevelOuter:=bvNone; end; procedure TkCPal.ApplicationEventsMessage(var Msg: tagMSG;var Handled: Boolean); begin pSetPalCenter; end; destructor TkCPal.Destroy; begin if not (csDesigning in ComponentState) then FApplicationEvents.Free; inherited; end; procedure TkCPal.pSetPalCenter; begin if (Parent<>nil) and FkAutoCenterPosition then begin Top :=(Parent.ClientHeight-Height) div 2; Left:=(Parent.ClientWidth -Width ) div 2; end; end; procedure TkCPal.pSetkAutoCenterPosition(aAutoCenterPosition:Boolean); begin FkAutoCenterPosition:=aAutoCenterPosition; pSetPalCenter; end; procedure TkCPal.Paint; begin Caption:=''; if (csDesigning in ComponentState) then pSetPalCenter; inherited; end; initialization registerClasses([TApplicationEvents]); end. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |