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

📄 028.htm

📁 Delphi书籍--Delphi网上教程
💻 HTM
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI专题文档-程序应用-->在Delphi应用程序中拖动控件</TITLE>
<META NAME="keywords" CONTENT=" DELPHI专题文档-程序应用 在Delphi应用程序中拖动控件">
<META NAME="description" CONTENT=" - DELPHI专题文档-程序应用 - 在Delphi应用程序中拖动控件">

<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="index6.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">在Delphi应用程序中拖动控件</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<p><span style="font-size: 9pt">  在应用程序的开发中,常常要将某些控件对象(如标签)用鼠标选中后进行拖动操作,用以改变控件的位置,在Delphi中如何实现这一功能呢?笔者想了一个方法:通过在 
public 中定义全局变量用以跟踪 
鼠标在移动和拖动控件时的坐标,然后在拖动结束时将鼠标的坐标值赋给控件的 
TOP和LEFT属性,以达到拖动的目的。<br> 
  举例如下:创建一个 Panel,再创建一个Label,通过编程使控件 
Lable 可以在 Panel 中被拖放到任意位置。以下是具体实现过程:<br> 
  1. 首先做一下准备工作,运行Delphi3.0,进入集成开发环境,在 
File 菜单中选择New Application 。<br> 
  2. 在Form1中创建对象 Panel1,并在 Panel1 中创建另一对象 Label1。<br> 
  3. 选中Label1,修改其下列属性的值: <br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 属性值:<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Caption 
&nbsp;&nbsp; :标签移动测试!<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cursor 
&nbsp;&nbsp;&nbsp; :crHandPoint<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DragCursor:crDrag<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DragMode &nbsp; 
:dmAutomatic<br> 
  4.在程序的开头部分声明全局变量 x_panel,y_panel,x_label,y_label,其中,x_panel,y_panel 
:鼠标在Panel1上的坐标;x_label,y_label :鼠标在label1上的坐标。<br> 
  <font color="#ff0000">注:这里分别获取在Panel1和Label1上的坐标是为了更精确地计算出Label1实际的移动距离。</font><br>
  5.在Panel1的OnDragOver 和OnMouseMove 事件中添加如下代码:<br> 
 &nbsp;&nbsp;&nbsp; x_panel:=X;<br> 
 &nbsp;  y_panel:=Y;<br>
   <font color="#ff0000">注:该操作是获得 mouse 在Panel1上的坐标。</font><br> 
  6.在Label1的OnMouseMove 事件中添加如下代码:<br> 
 &nbsp;&nbsp;  x_Labell:=X;<br>
 &nbsp;&nbsp;  y_Label1:=Y;<br>
 &nbsp;&nbsp;  <font color="#ff0000">注:该操作是获得 mouse 在Label1上的坐标。</font>
<br>
  7.在Label1的OnEndDrag 事件中添加如下代码:<br> 
 &nbsp;&nbsp;  label1.left :=x_panel-x_label;<br> 
 &nbsp;&nbsp;  label1.top :=y_panel-y_label; <br> 
 &nbsp;&nbsp;  <font color="#ff0000">说明:两者相减是为了求得 label1
实际的移动距离。 </font><br>
  8.创建一个对象 Button1 ,并在其 OnClick 事件中添加如下代码:<br> 
 &nbsp;&nbsp;  close; <font color="#408080"><em>//用以关闭应用程序。</em></font><br> 
  好了,现在运行程序,测试一下结果。<br>
  <b>以下是程序源代码,在Delphi3.0、Windows95/98中测试通过。</b><br>
unit test_move;<br> 
interface<br> 
uses<br> 
&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> 
&nbsp; StdCtrls, ExtCtrls;<br> 
type<br> 
&nbsp; TForm1 = class(TForm)<br> 
&nbsp; Panel1: TPanel;<br> 
&nbsp; Label1: TLabel;<br> 
&nbsp; Button1: TButton;<br> 
&nbsp; procedure Button1Click(Sender: TObject);<br> 
&nbsp; procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);<br> 
&nbsp; procedure Panel1DragOver(Sender, Source: TObject; X, Y: Integer;<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; State: TDragState; var 
Accept: Boolean);<br> 
&nbsp; procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);<br> 
&nbsp; procedure Label1EndDrag(Sender, Target: TObject; X, Y: Integer);<br> 
private<br> 
&nbsp; { Private declarations }<br> 
public<br> 
&nbsp; { Public declarations }<br> 
&nbsp; x_panel,y_panel,x_label,y_label:integer;<br> 
end;<br> 
var<br> 
&nbsp; Form1: TForm1;<br> 
implementation<br> 
{$R *.DFM}<br> 
procedure TForm1.Button1Click(Sender: TObject);<br> 
begin<br> 
&nbsp;&nbsp; close;<br> 
end;<br> 
procedure FTorm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState;X,Y: Integer);<br> 
begin<br> 
&nbsp;&nbsp; x_panel:=X;<br> 
&nbsp;&nbsp; y_panel:=Y;<br> 
end;<br> 
procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; State: TDragState; var Accept: 
Boolean);<br> 
begin<br> 
&nbsp;&nbsp; x_panel:=X;<br> 
&nbsp;&nbsp; y_panel:=Y;<br> 
end;<br> 
procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState;X,Y: Integer);<br> 
begin<br> 
&nbsp;&nbsp; x_label:=X;<br> 
&nbsp;&nbsp; y_label:=Y;<br> 
end;<br> 
procedure TForm1.Label1EndDrag(Sender, Target: TObject; X, Y: Integer);<br> 
begin<br> 
&nbsp; label1.left :=x_panel-x_label; <br> 
&nbsp; label1.top:=y_panel-y_label; <br> 
end;<br> 
end.<br> 
<b></b> </span></p> 
<hr color="#EE9B73" size="1" width="94%"> 
 
</TD> 
 
</TR> 
</table> 
</BODY></HTML>

⌨️ 快捷键说明

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