使用鼠标拖曳的方式移动、更改panel控件的大小

 1 procedure   ManipulateControl(WinControl:   TControl;   Shift:   TShiftState;   
 2                                                                   X,   Y,   Precision:   integer);   
 3                                                                   //Precision:精度,该方法可以在onmousemove中调用   
 4   var   
 5       SC_MANIPULATE:   Word;   
 6   begin   
 7       //光标在控件的最左侧   
 8       if   (X<=Precision)   and   (Y>Precision)   and   (Y<WinControl.Height-Precision)   then   
 9       begin   
10           SC_MANIPULATE     :=   $F001;   
11           WinControl.Cursor   :=   crSizeWE;   
12     
13       end   
14       else   
15           //光标在控件的最右侧   
16           if   (X>=WinControl.Width-Precision)   and   (Y>Precision)   and   (Y<WinControl.Height-Precision)   then   
17           begin   
18               SC_MANIPULATE     :=   $F002;   
19               WinControl.Cursor   :=   crSizeWE;   
20           end   
21           else   
22               //光标在控件的最上侧   
23               if   (X>Precision)   and   (X<WinControl.Width-Precision)   and   (Y<=Precision)   then   
24               begin   
25                   SC_MANIPULATE     :=   $F003;   
26                   WinControl.Cursor   :=   crSizeNS;   
27               end   
28               else   
29                   //光标在控件的最下侧   
30                   if   (X>Precision)   and   (X<WinControl.Width-Precision)   and   (Y>=WinControl.Height-Precision)   then   
31                   begin   
32                       SC_MANIPULATE     :=   $F006;   
33                       WinControl.Cursor   :=   crSizeNS;   
34                   end   
35                   else   
36                       //光标在控件的左上角   
37                       if   (X<=Precision)   and   (Y<=Precision)   then   
38                       begin   
39                           SC_MANIPULATE     :=   $F004;   
40                           WinControl.Cursor   :=   crSizeNWSE;   
41                       end   
42                       else   
43                           //光标在控件的右上角   
44                           if   (X>=WinControl.Width-Precision)   and   (Y<=Precision)   then   
45                           begin   
46                               SC_MANIPULATE     :=   $F005;   
47                               WinControl.Cursor   :=   crSizeNESW   ;   
48                           end   
49                           else   
50                               //光标在控件的左下角   
51                               if   (X<=Precision)   and   (Y>=WinControl.Height-Precision)   then   
52                               begin   
53                                   SC_MANIPULATE     :=   $F007;   
54                                   WinControl.Cursor   :=   crSizeNESW;   
55                               end   
56                               else   
57                                   //光标在控件的右下角   
58                                   if   (X>=WinControl.Width-Precision)   and   (Y>=WinControl.Height-Precision)   then   
59                                   begin   
60                                       SC_MANIPULATE     :=   $F008;   
61                                       WinControl.Cursor   :=   crSizeNWSE;   
62                                   end   
63                                   else   
64                                       //光标在控件的客户区(移动整个控件)   
65                                       if   (X>5)   and   (Y>5)   and   (X<WinControl.Width-5)   and     
66                                                   (Y<WinControl.Height-5)   then   
67                                       begin   
68                                           SC_MANIPULATE     :=   $F009;   
69                                           WinControl.Cursor   :=   crSizeAll;   
70                                       end   
71                                       else   
72                                       begin   
73                                           SC_MANIPULATE   :=   $F000;   
74                                           WinControl.Cursor   :=   crDefault;   
75                                       end;   
76       if   Shift=[ssLeft]   then   
77       begin   
78           ReleaseCapture;   
79           WinControl.Perform(WM_SYSCOMMAND,   SC_MANIPULATE,   0);   
80       end;   
81   end;   
82     
83   //调用方法:   
84   procedure   TForm1.Panel1MouseMove(Sender:   TObject;   Shift:   TShiftState;   X,   
85       Y:   Integer);   
86   begin   
87       Caption   :=   IntToStr(X)   +   '/'   +   IntToStr(Y);   
88       ManipulateControl((Panel1   as   TwinControl),   Shift,   X,   Y,   10);   
89   end;   
90     
91 

 

  注意:   只有从TWinControl继承的控件,才能用此方法。

 

posted @ 2010-01-06 22:27  Delcpp  阅读(1323)  评论(2编辑  收藏  举报