窗口调整消息

捕获窗口尺寸调整消息,实时显示窗口尺寸虚框,控制窗口大小。

WM_SIZING 消息

返回窗口图标的句柄。

参数 意义
hwnd 接收消息的窗口句柄
msg WM_SIZING
wParam fwSide 指示调整的边框方位
lParam lprc 窗口位置,RECT 结构

fsSide 常量

指示窗口调整时,鼠标点住的边框位置

WMSZ_TOPLEFT  WMSZ_TOP  WMSZ_TOPRIGHT 
WMSZ_LEFT WMSZ_RIGHT 
WMSZ_BOTTOMLEFT  WMSZ_BOTTOM  WMSZ_BOTTOMRIGHT 
常量 意义
WMSZ_LEFT = 1 Left edge
WMSZ_RIGHT = 2 Right edge
WMSZ_TOP = 3 Top edge
WMSZ_TOPLEFT = 4 Top-left corner
WMSZ_TOPRIGHT = 5 Top-right corner
WMSZ_BOTTOM = 6 Bottom edge
WMSZ_BOTTOMLEFT = 7 Bottom-left corner
WMSZ_BOTTOMRIGHT = 8 Bottom-right corner

lprc 结构

Public Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

源代码(Delphi)

// 限制窗口最小尺寸,从拖动中的虚框上实时表达
procedure TForm1.user_size(var msg:TMessage);
var
    r : ^TRect;
    w , h : integer;
    minwidth : integer;
    minheight : integer;
    maxwidth : integer;
    maxheight : integer;
begin
    minwidth := self.Constraints.MinWidth;
    minheight:= self.Constraints.MinHeight;
    maxwidth := self.Constraints.MaxWidth;
    maxheight:= self.Constraints.MaxHeight;

    r := Pointer(msg.lparam);

    w := r.Right - r.Left;
    h := r.Bottom - r.Top;

    if w<minwidth then
       if msg.WParam in [WMSZ_RIGHT,WMSZ_TOPRIGHT,WMSZ_BOTTOMRIGHT] then
            r.Right:=r.left+minwidth
       else
            r.left := r.right-minwidth;

    if (w>maxwidth) and (maxwidth>minwidth) then
       if msg.WParam in [WMSZ_RIGHT,WMSZ_TOPRIGHT,WMSZ_BOTTOMRIGHT] then
            r.Right:=r.left+maxwidth
       else
            r.left := r.right-maxwidth;

    if h<minheight then
        if msg.WParam in [WMSZ_BOTTOM,WMSZ_BOTTOMLEFT,WMSZ_BOTTOMRIGHT] then
            r.Bottom := r.Top + minheight
        else
            r.Top := r.Bottom - minheight;

    if (h>maxheight) and (maxheight>minheight) then
        if msg.WParam in [WMSZ_BOTTOM,WMSZ_BOTTOMLEFT,WMSZ_BOTTOMRIGHT] then
            r.Bottom := r.Top + maxheight
        else
            r.Top := r.Bottom - maxheight;
end;

WM_NCHITTEST 消息

当鼠标位于窗口上方时发送此消息,以确定鼠标位于窗口的哪个部分,并据此实现窗口移动、改变大小等。

// 限制鼠标只能在窗口的边角调整大小

procedure MousePos(var msg:TMessage);message WM_NCHITTEST;
begin
    inherited ;
    case msg.Result of
    HTRIGHT, HTLEFT, HTTOP, HTBOTTOM:
            if msg.LParamHi < top + Height div 2 then
                if msg.LParamLo < left + Width div 2 then
                    msg.Result := HtTopLeft
                else
                    msg.Result := HtTopRight
            else
                if msg.LParamLo < left + Width div 2 then
                    msg.Result := HtBottomLeft
                else
                    msg.Result := HtBottomRight;
    end;
end;