製作可移動的表單的MovePanel控制項 |
|
conundrum
尊榮會員 發表:893 回覆:1272 積分:643 註冊:2004-01-06 發送簡訊給我 |
製作可移動的表單的MovePanel控制項 http://www.hoyoco.net/soft/delphil/ocx/96.htm ---- 使用Winamp是有個EasyMove的功能,也就是不在標題欄上拖動滑鼠就能移 動表單,雖然EasyMove功能很好實現,可還不如做個控制項一勞永逸,另外這個 控制項還有一個更有用的功能,呆會兒就能見到。我們先看看如何實現它吧! ---- 建立一個空的Unit,把以下代碼Copy進去,再把它添加到Delphi的控制項 庫裏,這樣MovePanel控制項就做好了。 unit MovePanel; interface uses Windows Classes Controls ExtCtrls; type TMovePanel = class(TPanel) //這個控制項是繼承Tpanel類的 private PrePoint:TPoint; Down:Boolean; { Private declarations } protected { Protected declarations } public constructor Create(AOwner:TComponent); override; //重載滑鼠事件,搶先處理消息 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X Y: Integer);override; procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X Y: Integer);override; procedure MouseMove(Shift: TShiftState; X Y: Integer);override; { Public declarations } published { Published declarations } end; procedure Register; implementation constructor TMovePanel.Create(AOwner:TComponent); begin inherited Create(AOwner); //繼承父類的Create方法 end; procedure TMovePanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X Y: Integer); begin if (Button=MBLeft) then begin Down:=true; GetCursorPos(PrePoint); end; //如果方法已存在,就觸發相應事件去調用它, 若不加此語句會造成訪存異常 if assigned(OnMouseDown) then OnMouseDown(self Button shift x y); end; procedure TMovePanel.MouseUp(Button: TMouseButton; Shift: TShiftState; X Y: Integer); begin if (Button=MBLeft) and Down then Down:=False; if assigned(OnMouseUp) then OnMouseUp(Self Button shift X y); end; procedure TMovePanel.MouseMove(Shift: TShiftState; X Y: Integer); Var NowPoint:TPoint; begin if down then begin GetCursorPos(nowPoint); //self.Parent在Form中就是MovePanel所在的表單, 或是MovePanel所在的容器像Panel self.Parent.Left:=self.Parent.left NowPoint.x-PrePoint.x; self.parent.Top:=self.Parent.Top NowPoint.y-PrePoint.y; PrePoint:=NowPoint; end; if Assigned(OnMouseMove) then OnMouseMove(self Shift X y); end; procedure Register; begin RegisterComponents('Md3' [TMovePanel]); end; end. ---- 接下來,看看怎麼用它吧。 ---- 用法一:拖一個Form下來,加上我們的MovePanel,Align屬性設為 alClient,運行一下,移動表單的效果還不錯吧!想取消此功能,把MovePanel 的Enabled屬性設為False即可,簡單吧! ---- 用法二:拖一個Form下來,加上普通的Panel,調整好大小,再在Panel上 加上我們的MovePanel Align屬性設為alClient,運行一下,這一次在我們拖動MovePanel時不是表單在 移動,而是Panel和MovePanel一起在表單上移動,如果我們再把其他的控制項放 在MovePanel上,就成了可以在表單上任意移動的控制項了,就這麼簡單! |
jest0024
高階會員 發表:11 回覆:310 積分:224 註冊:2002-11-24 發送簡訊給我 |
另一種方法,我個人比較喜好的.. procedure TForm.MouseDown... begin ReleaseCapture; perform(WM_SYSCOMMAND, SC_MOVE or 2, 0); //^移動元件就加入例:Panel1.perform(WM_SYSCOMMAND,SC... [red] TControl(Sender).Parent.perform(WM_SysCommand, SC_MOVE or 2, 0); //註元件的上層跟著移動的,放在form移動form,放在panel移動panel.. end;ABCD分開看的懂,連起來像鬼劃符~~!!! 發表人 - jest0024 於 2004/06/06 10:44:39 |
conundrum
尊榮會員 發表:893 回覆:1272 積分:643 註冊:2004-01-06 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |