程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 實現控件的移動、改變大小

實現控件的移動、改變大小

編輯: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
     );

   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved