function SubclassProc(Wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM; uIdSubclass: UINT_PTR; RefData: DWORD_PTR): LRESULT; stdcall; var Self: TfrmMain; Message: TMessage; begin Result := DefSubclassProc(Wnd, Msg, wParam, lParam); case Msg of //你可以在这里处理其它WINDOWS消息 WM_WINDOWPOSCHANGING: begin Self := TfrmMain(RefData); Message.Msg := Msg; Message.WParam := wParam; Message.LParam := lParam; Message.Result := Result; Self.Dispatch(Message); Result := Message.Result; end; end; end;
procedure TfrmMain.CreateHandle; var Wnd: HWND; begin inherited; Wnd := WindowHandleToPlatform(Self.Handle).Wnd; SetWindowSubclass(Wnd, SubclassProc, 1, DWORD_PTR(Self)); end;
procedure TfrmMain.DestroyHandle; var Wnd: HWND; begin Wnd := WindowHandleToPlatform(Self.Handle).Wnd; RemoveWindowSubclass(Wnd, SubclassProc, 1); inherited; end;
procedure TfrmMain.WMWindowPosChanging(var Message: TWMWindowPosChanging); procedure HandleEdge(var Edge: Integer; SnapToEdge: Integer; SnapDistance: Integer = 0); begin if (Abs(Edge + SnapDistance - SnapToEdge) < 15) then Edge := SnapToEdge - SnapDistance; end; begin inherited;
var WindowPos : PWindowPos; var LRect: TRect; WindowPos := Message.WindowPos; if (not (csDesigning in ComponentState)) and ((WindowPos.X <> 0) or (WindowPos.Y <> 0)) and ((WindowPos.cx = Width) and (WindowPos.cy = Height)) then begin LRect := Screen.WorkareaRect; HandleEdge(WindowPos.x, LRect.Left, 0); HandleEdge(WindowPos.y, LRect.Top, 0); HandleEdge(WindowPos.x, LRect.Right, Width); HandleEdge(WindowPos.y, LRect.Bottom, Height); Message.WindowPos := WindowPos; end;
if (Message.WindowPos.y + Message.WindowPos.cy > Screen.WorkAreaHeight) then begin Message.WindowPos.Y := Screen.WorkAreaHeight - Height; end; if (Message.WindowPos.x + Message.WindowPos.cx > Screen.WorkAreaWidth) then begin Message.WindowPos.X := Screen.WorkAreaWidth - Width; end;
if Message.WindowPos.x<0 then Message.WindowPos.x := 0; if Message.WindowPos.y<0 then Message.WindowPos.y := 0; end; {$ENDIF}
..........
{$IFDEF MSWINDOWS} var ICC: TInitCommonControlsEx;