⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 028.htm

📁 Delphi书籍--Delphi网上教程
💻 HTM
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI专题--控件应用-->制作可移动的窗体的MovePanel控件</TITLE>
<META NAME="keywords" CONTENT=" DELPHI专题--控件应用 制作可移动的窗体的MovePanel控件">
<META NAME="description" CONTENT=" - DELPHI专题--控件应用 - 制作可移动的窗体的MovePanel控件">

<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<a href="index.html">返回</a>



<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>

<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">制作可移动的窗体的MovePanel控件</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<BR>
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 使用Winamp是有个EasyMove的功能,也就是不在标题栏上拖动鼠标就能移动窗体,虽然EasyMove功能很好实现,可还不如做个控件一劳永逸,另外这个控件还有一个更有用的功能,呆会儿就能见到。我们先看看如何实现它吧! 
</span></p> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 建立一个空的Unit,把以下代码Copy进去,再把它添加到Delphi的控件库里,这样MovePanel控件就做好了。 
</span></p> 
<BR> 
<pre><span style="font-size: 9pt">
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.
</span></pre> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 
接下来,看看怎么用它吧。 </span></p> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>用法一:</b>拖一个Form下来,加上我们的MovePanel,Align属性设为alClient,运行一下,移动窗体的效果还不错吧!想取消此功能,把MovePanel的Enabled属性设为False即可,简单吧! 
</span></p> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>用法二:</b>拖一个Form下来,加上普通的Panel,调整好大小,再在Panel上加上我们的MovePanel, 
Align属性设为alClient,运行一下,这一次在我们拖动MovePanel时不是窗体在移动,而是Panel和MovePanel一起在窗体上移动,如果我们再把其他的控件放在MovePanel上,就成了可以在窗体上任意移动的控件了,就这么简单! 
</span></p> 
<BR> 
<hr color="#EE9B73" size="1" width="94%"> 
 
</TD> 
 
</TR> 
</table> 
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -