type HidePosKind = (hpTop,hpLeft,hpBottom,hpRight); type THidePos = set of HidePosKind; 不过,类似的集合在Delphi本身就已经存在,譬如TAnchors集合。TAnchors集合原来是用于指明一个控件如何锚定于其父类控件的位置,我们在这里则借用来描述窗体对屏幕的隐藏位置。
在TAnchors集合中也包含了四个值,其定义如下:
type TAnchorKind = (akTop, akLeft, akRight, akBottom); type TAnchors = set of TAnchorKind; 在代码的实现中,我们将定义一个TAnchors类型的全局变量FAnchors去描述窗体隐藏的位置。
三、初步的实现
首先我们定义一个过程对WM_MOVING消息进行拦截处理,代码如下:
.. private FAnchors: TAnchors; procedure WMMOVING(var Msg: TMessage); message WM_MOVING; .. uses Math,type; procedure TForm1.WMMOVING(var Msg: TMessage); begin inherited; with PRect(Msg.LParam)^ do begin Left := Min(Max(0, Left), Screen.Width - Width); Top := Min(Max(0, Top), Screen.Height - Height); Right := Min(Max(Width, Right), Screen.Width); Bottom := Min(Max(Height, Bottom), Screen.Height); FAnchors := []; if Left = 0 then Include(FAnchors, akLeft); if Right = Screen.Width then Include(FAnchors, akRight); if Top = 0 then Include(FAnchors, akTop); if Bottom = Screen.Height then Include(FAnchors, akBottom); Timer1.Enabled := FAnchors <> []; end; end; 在该过程中,我们通过对矩形参数Left、Top、Right、Bottom的判断确定窗体所处位置是否符合隐藏条件,判断结果存放在全局变量Fanchors之中。当触发隐藏时,在Fanchors中将至少有一个值而不多于两个值。(为什么呢?)
procedure TForm1.Timer1Timer(Sender: TObject); const cOffset = 2; begin if WindowFromPoint(Mouse.CursorPos) = Handle then begin if akLeft in FAnchors then Left := 0; if akTop in FAnchors then Top := 0; if akRight in FAnchors then Left := Screen.Width - Width; if akBottom in FAnchors then Top := Screen.Height - Height; end else begin if akLeft in FAnchors then Left := -Width + cOffset; if akTop in FAnchors then Top := -Height + cOffset; if akRight in FAnchors then Left := Screen.Width - cOffset; if akBottom in FAnchors then Top := Screen.Height - cOffset; end; end; 在这里,我们首先定义一个常量cOffset去表示窗体隐藏后显露部分的大小。然后我们利用WindowFromPoint这个Windows API函数检测鼠标是否位于窗体之上。接下来的判断就是处理在显示和隐藏状态下窗体Left 和Top 属性值的设置。注意,针对Fanchors中存在不同值的情况,窗体Left和Top的设置是各不相同的,但是这些设置只有顺序上的差异而并没有优先级别的差异。(为什么要提到这一点呢?)