Delphi中的窗体移动[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 masterlee 的 blog

如果你在开发图形或多媒体应用程序,你可能正在为如何不使用窗体的标题栏而移动窗体发愁。其实只需用鼠标拖动窗体的客户区就可以了。

方法一
  以下是完成上述功能最普通的方法:在窗体的private声明部分加入以下过程声明:

  procedure wmnchittest(var msg:twmnchittest);message wm_nchittest;

  然后在implementation部分加入以下代码:

procedure tform1{或你定义的form名}.wmnchittest(var msg:twmnchittest);
begin
defaulthandler(msg);
if msg.result = htclient then
msg.result:= htcaption;
end;

  此方法中使当鼠标点击窗体客户区时,令windows认为被点击的是标题栏。

方法二
  以下是另一个实现用鼠标移动普通窗体的方法。

procedure tform1.formmousemove(sender: tobject; shift: tshiftstate; x, y: integer);
begin
if (ssleft in shift) then begin
releasecapture;
sendmessage(form1.handle,wm_syscommand,sc_move+1,0);
end;
end;

以上方法不完善之处
  当把“拖动窗口的过程中显示其内容”的选项取消时,让我们看看会发生什么。这是windows窗口的一项设置,你可以在“开始菜单-->设置-->文件夹选项-->查看-->高级设置”中找到该属性。在windows95中,你需要修改注册表。当此属性被设为无效,拖动窗体时窗体会变为一个正方形轮廓线。也许你使用一个不规则窗体,但它仍然会显示轮廓线。
  当你想要让你的窗体停靠在屏幕的边缘(如:winamp,当你拖动窗体进入屏幕顶部某一特定位置时,窗体便紧靠在屏幕顶部),如果你使用以上第二种方法,在鼠标按钮放开前,你将无法处理窗体位置,并无法处理停靠问题。
  下面我会用简单的方法解决两个问题:
  第一,无论设置为何,在拖动窗体时都不显示轮廓线;
  第二,移动窗体时进行位置检测,并在位置适当时停靠在某一特定位置。
  很多人也许已经解决了这些问题,但是也许下面的代码对你会有帮助。

方法三
  以下代码可以直接复制到delphi中,前提是你将form1存为umain.pas,form2存为udock.pas。用到的事件是:onmousedown,onmousemove,onmouseup,onshow(form1)。
  这是一个根据鼠标的移动移动窗体的方法,包含两个窗体,umain和udock(form1和form2)。form2通过form1打开,并可以停靠在form1的底部。停靠后,form2将随form1一起移动,直到你将form2移开。

form1

unit umain;

interface

uses
windows, messages, sysutils, classes, graphics, controls, forms, dialogs;

type
tform1 = class(tform)
procedure formmousedown(sender:tobject; button:tmousebutton;shift:tshiftstate;x,y: integer);
procedure formmousemove(sender: tobject; shift: tshiftstate;x,y: integer);
procedure formmouseup(sender:tobject;button:tmousebutton;shift:tshiftstate;x,y: integer);
procedure formshow(sender: tobject);
private
{ private declarations }
public
docktoform: boolean;
{ public declarations }
end;

var
form1: tform1;
canmove, canmovex, canmovey: boolean;
oldx, oldy: integer;
f1x,f1y,f2x,f2y: integer;
workarea : trect;

implementation

uses udock;

{$r *.dfm}

procedure tform1.formmousedown(sender: tobject; button: tmousebutton;shift: tshiftstate; x, y: integer);
begin
canmovex := true;
canmovey := true;
canmove := true;
oldx := x;
oldy := y;

if docktoform then
begin
f1x := form1.left;
f1y := form1.top;
f2x := form2.left;
f2y := form2.top;
end;

end;

procedure tform1.formmousemove(sender: tobject; shift: tshiftstate; x, y: integer);
begin
if (canmove) then
begin
if canmovex then
form1.left := form1.left + (x - oldx);

if canmovey then
form1.top := form1.top + (y - oldy);

//this section latches to the top
if (form1.top < workarea.top + 10) and (form1.top > workarea.top-10) then
begin
form1.top := workarea.top;
if (y-oldy > 10) or (y-oldy < -10) then
canmovey := true
else
canmovey := false;
end;

//this section latches to the left side
if (form1.left < workarea.left+10) and (form1.left > woskarea.left-10) then
begin
form1.left := workarea.left;

if (x-oldx > 10) or (x-oldx < -10) then
canmovex := true
else
canmovex := false;
end;

//this section latches to the right side
if (form1.left > workarea.right-form1.width-10) and (form1.left < workarea.right-form1.width+10) then
begin

form1.left := workarea.right-form1.width;

if (x-oldx > 10) or (x-oldx < -10) then
canmovex := true
else
canmovex := false;
end;

//this section latches to the taskbar
if docktoform then
begin

本文关键:Delphi中的窗体移动
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top