我有一个自定义表单,没有任何类型的边框。我正在绘制一些自己的自定义边框,它不会延伸到窗体的远边缘。相反,通过使用表单的透明属性,此自定义绘制边框外的任何内容都是透明的。这使得表单的一小部分可以使用和可见。
我知道有很多解决方案可以实现这一目标,而且我已经找到了最合适的方法来实现这一目标。但是,此方法假定用户将鼠标指向窗体的远边缘。我需要限制它在不同的约束内作出反应(例如较小的矩形)。
这是我发现的代码,它已经适用于下一个边缘约束:
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;
我将如何改变对此作出反应的界限?例如,左边和右边应该在表单中反应10个像素。标准表单rect可能是(0, 0, 100, 100)
但我希望上面的方法在(10, 3, 90, 97)
答案 0 :(得分:5)
实际上,定义两个常量而不是唯一的EDGEDETECT
是有意义的
你需要水平和垂直偏移是不同的,并从头开始写,但这是一个快速补丁:
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;