实现控件的移动、改变大小(DELPHI实现)

王朝delphi·作者佚名  2006-01-08
宽屏版  字体: |||超大  

实现控件的移动,改变大小(DELPHI实现)

主要使用Perform方法

function Perform(Msg: Cardinal; WParam, LParam: Longint): Longint;

只要能够使用类似于win32API的函数SendMessage(),本方法同样可在其他环境里应用。

用Delphi实现

首先,建立一个应用程序,在一个窗体里加入一个Panel1,保存为main.pas;

然后,分别在Panel1的鼠标移动、鼠标按下事件里添加代码;

鼠标移动:控制光标的形状

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

begin

if (x>=0)and(x<=3) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNWSE;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;

if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Cursor:=crSizeNESW;

end

else if (x>3)and(x<Panel1.Width-3) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNS;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crArrow;

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNS;

end

else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNESW;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNWSE;

end;

end;

鼠标按下:控制Panel的大小或位置

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

ReleaseCapture;

if (x>=0)and(x<=3) then

begin

file://左上角方向改变大小

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F004,0);

file://左侧

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F001,0);

file://左下角

if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Perform(WM_SysCommand,$F007,0);

end

else if (x>3)and(x<Panel1.Width-3) then

begin

file://上侧

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F003,0);

file://移动控件

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F012,0);

file://下侧

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F006,0);

end

else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then

begin

file://右上角

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F005,0);

file://右侧

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F002,0);

file://右下角

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F008,0);

end;

end;

主要使用 Perform 方法为

附录1.全部代码如下:

unit main;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, ExtCtrls;

type

TForm1 = class(TForm)

Panel1: TPanel;

procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

ReleaseCapture;

if (x>=0)and(x<=3) then

begin

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F004,0);

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F001,0);

if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Perform(WM_SysCommand,$F007,0);

end

else if (x>3)and(x<Panel1.Width-3) then

begin

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F003,0);

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F012,0);

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F006,0);

end

else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then

begin

if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F005,0);

if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F002,0);

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F008,0);

end;

end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

begin

if (x>=0)and(x<=3) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNWSE;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;

if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Cursor:=crSizeNESW;

end

else if (x>3)and(x<Panel1.Width-3) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNS;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crArrow;

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNS;

end

else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then

begin

if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNESW;

if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;

if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNWSE;

end;

end;

附录2.API SendMessage()介绍

SendMessage === user32.lib

LRESULT SendMessage(

HWND hWnd, // handle of destination window

UINT Msg, // message to send

WPARAM wParam, // first message parameter

LPARAM lParam // second message parameter

);

2001.9.27

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有