近期想制作一个FDS的建模工具,其中需要对已生成控件的大小、位置等属性进行调整。于是从百度上搜了一下,很惊讶的发现Controls.TControl.Perform方法有奇效!
函数有三个原型,只列举一个:
Perform(Msg: Cardinal; WParam: WPARAM; LParam: LPARAM): LRESULT; overload;
其中Msg用WM_SYSCOMMAND
WParam用SC_SIZE OR 9
LParam用0
就可以实现已生成组件的拖动。
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin ReleaseCapture; Button1.Perform(WM_SYSCOMMAND,SC_SIZE OR 9,0); end;
很神奇吧,可见windows消息的强大,下面把WM_SYSCOMMAND消息命令整理一下:
SC_CLOSE 关闭窗口
SC_HOTKEY Activate the window associated with the application-specified hot key.
SC_HSCROLL Scroll horizontally.
SC_KEYMENU Retrieve a menu through a keystroke.
SC_MAXIMIZE 最大化窗口
SC_MINIMIZE 最小化窗口.
SC_MOUSEMENU Retrieve a menu through a mouse click.
SC_MOVE Move the window.
SC_NEXTWINDOW Move to the next window.
SC_PREVWINDOW Move to the previous window.
SC_RESTORE Save the previous coordinates (checkpoint).
SC_SCREENSAVE 激活屏幕保护程序.
SC_SIZE Size the window.
SC_TASKLIST 激活开始菜单.
SC_VSCROLL Scroll vertically.
SC_MONITORPOWER 关闭显示器(LPARAM为非0参数)
SC_SEPARATOR
SC_CONTEXTHELP 显示帮助
SC_DEFAULT
SC_SIZE = $F000
SC_MOVE = $F010
SC_MINIMIZE = $F020
SC_MAXIMIZE = $F030
SC_NEXTWINDOW = $F040
SC_PREVWINDOW = $F050
SC_CLOSE = $F060
SC_VSCROLL = $F070
SC_HSCROLL = $F080
SC_MOUSEMENU = $F090
SC_KEYMENU = $F100
SC_ARRANGE = $F110
SC_RESTORE = $F120
SC_TASKLIST = $F130
SC_SCREENSAVE = $F140
SC_HOTKEY = $F150
SC_DEFAULT = $F160
SC_MONITORPOWER = $F170
SC_CONTEXTHELP = $F180
SC_SEPARATOR = $F00F
有的时候我们看到一些这里没有的命令,比如用鼠标拖动控件时用的panel1.Perform(WM_SYSCOMMAND, $F012, 0);这个$F012这个命令在定义里没有,其实它是SC_MOVE or 2的结果,微软的文档中提到WM_SYSCOMMAND命令中wParam的值的低4位值是保留的。
举例说明:
在Form的MouseDown事件里写如下代码
ReleaseCapture;
Perform(WM_SYSCOMMAND, SC_SIZE OR 1, 0); // Left
Perform(WM_SYSCOMMAND, SC_SIZE OR 2, 0); // Right
Perform(WM_SYSCOMMAND, SC_SIZE OR 3, 0); // Top
Perform(WM_SYSCOMMAND, SC_SIZE OR 4, 0); // left-top
Perform(WM_SYSCOMMAND, SC_SIZE OR 5, 0); // Right-top
Perform(WM_SYSCOMMAND, SC_SIZE OR 6, 0); // bottom
Perform(WM_SYSCOMMAND, SC_SIZE OR 7, 0); // left-bottom
Perform(WM_SYSCOMMAND, SC_SIZE OR 8, 0); // right-bottom