實現控制元件的移動、改變大小(DELPHI實現) (轉)

gugu99發表於2008-07-11
實現控制元件的移動、改變大小(DELPHI實現) (轉)[@more@]

 

實現的移動,改變大小(實現)

主要使用Perform方法
  function Perform(Msg: Cardinal; WParam, LParam: Longint): Longint;
 
只要能夠使用類似於的SendMessage(),本方法同樣可在其他環境裡應用。

用Delphi實現
首先,建立一個應用,在一個窗體里加入一個Panel1,儲存為main.pas;
然後,分別在Panel1的滑鼠移動、滑鼠按下事件裡新增程式碼;
滑鼠移動:控制游標的形狀
procedure TForm1.Panel1MouseMove(Sender: T; 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  if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Cursor:=crSizeNESW;
  end
  else if (x>3)and(x  begin
  if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNS;
  if (y>3)and(y  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  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_Symmand,$F004,0);
  file://左側
  if (y>3)and(y  下角
  if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Perform(WM_SysCommand,$F007,0);
  end
  else if (x>3)and(x  begin
  file://上側
  if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F003,0);
  file://移動控制元件
  if (y>3)and(y  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  file://右下角
  if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F008,0);
  end;
end;
主要使用 Perfo方法為

附錄1.全部程式碼如下:

unit main;

interface

uses
  , 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  if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Perform(WM_SysCommand,$F007,0);
  end
  else if (x>3)and(x  begin
  if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F003,0);
  if (y>3)and(y  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  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  if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Cursor:=crSizeNESW;
  end
  else if (x>3)and(x  begin
  if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNS;
  if (y>3)and(y  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  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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1007117/,如需轉載,請註明出處,否則將追究法律責任。

相關文章