delphi技巧——移动无标题窗口
关键词:移动,消息,无标题
作者:朱能文
我们都知道windows是通过标题栏来移动窗体的,当我们在标题栏上按下鼠标左键时,windows将发送wm_nchittest消息,来告诉系统要移动窗体。但我们在实际编程时,有时为了界面的需要或特殊的要求,窗体没有标题栏,我们必须通过程序来移动窗体。下面我将介绍几种移动窗体的方法和技巧。
方法一:根据鼠标按下和移动时的位置差,来计算窗体的位置。需要定义的变量如下:
var
opos, cpos: tpoint;
flag: boolean = false;
处理onmousedown事件代码:
flag := true;
opos.x := x;
opos.y := y;
处理onmousemove事件代码:
if flag then
begin
cpos.x := x;
cpos.y := y;
left := left + cpos.x - opos.x;
top := top + cpos.y - opos.y;
end;
处理onmousemove事件代码:
flag := false;
方法二:用户自定义消息,拦截“wm_nchittest”消息,将消息值“htclient”转为“htcaption”。实现如下:
先定义一消息常量:const wm_mytest = wm_user+200;
在private部分声明过程:
procedure moveclient(var message: tmessage); message wm_nchittest;
过程的实现: