Resizing borderless form from different constraints than far edges?
Asked Answered
A

1

2

I have a custom form which does not have any type of border. I'm drawing some custom borders of my own instead, which do not extend up to the far edges of the form. Instead, whatever's outside this custom drawn border is transparent, through the use of the form's transparent properties. This leaves a smaller portion of the form to be usable and visible.

I know there are tons of solutions out there to accomplish this, and I've already found the best suited method to do this. However, this method assumes that user will be pointing the mouse along the far edges of the form. I need to limit it to react from within different constraints (for example a smaller sized rect).

Here's the code I found which already works on a next-to-the-edge constraint:

procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;

....

procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
const
  EDGEDETECT = 7;  //adjust to suit yourself
var
  deltaRect: TRect;  //not really used as a rect, just a convenient structure
begin
  inherited;
  if BorderStyle = bsNone then begin
    with Message, deltaRect do begin
      Left := XPos - BoundsRect.Left;
      Right := BoundsRect.Right - XPos;
      Top := YPos - BoundsRect.Top;
      Bottom := BoundsRect.Bottom - YPos;
      if (Top<EDGEDETECT)and(Left<EDGEDETECT) then
        Result := HTTOPLEFT
      else if (Top<EDGEDETECT)and(Right<EDGEDETECT) then
        Result := HTTOPRIGHT
      else if (Bottom<EDGEDETECT)and(Left<EDGEDETECT) then
        Result := HTBOTTOMLEFT
      else if (Bottom<EDGEDETECT)and(Right<EDGEDETECT) then
        Result := HTBOTTOMRIGHT
      else if (Top<EDGEDETECT) then
        Result := HTTOP
      else if (Left<EDGEDETECT) then
        Result := HTLEFT
      else if (Bottom<EDGEDETECT) then
        Result := HTBOTTOM
      else if (Right<EDGEDETECT) then
        Result := HTRIGHT
    end; 
  end;
end;

How would I go about changing the bounds for this to react? For example, the left and right edges should react 10 pixels into the form. The standard form rect may be (0, 0, 100, 100) but I want this method above to work within bounds of (10, 3, 90, 97)

Abbotsun answered 5/12, 2011 at 20:5 Comment(0)
P
5

Actually it would make sense to define two constants instead of the only EDGEDETECT since you require horizontal and vertical offsets to be different and write it from scratch, but here is a quick patch:

procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
const
  EDGEDETECT = 17;  //adjust to suit yourself    // <- increased to suit outer offset
var
  deltaRect: TRect;  //not really used as a rect, just a convenient structure

  OuterRect: TRect;                              // used as a rect
begin
  inherited;
  if BorderStyle = bsNone then begin
    with Message, deltaRect do begin

     ..
      else if (Right<EDGEDETECT) then
        Result := HTRIGHT;
     ..

      OuterRect := BoundsRect;                    // patch
      InflateRect(OuterRect, -10, -3);
      if not PtInRect(OuterRect, SmallPointToPoint(Message.Pos)) then
        Message.Result := HTTRANSPARENT;

    end;
  end;
end;
Palladio answered 5/12, 2011 at 20:28 Comment(2)
Very nice :D Still could use a little tweaking, but looks like that'll do the trick. New things here I've never seen before are use of InflateRect, PtInRect, and SmallPointToPoint. I'm sure I'll figure them all out, thanks :DAbbotsun
@Jerry - You're welcome! > "tweaking" > Yeah, if you have the time write it from scratch, it will be better..Palladio

© 2022 - 2024 — McMap. All rights reserved.