delphi - 如何在 delphi TFrame 上编写调整大小句柄?

标签 delphi windows-messages

我有一个 TFrame,我希望能够通过单击并拖动右下角来调整其大小。功能应该是;

当鼠标移到右下角时,光标应该发生变化,以反射(reflect)框架可以调整大小。如果不在底角上方,光标应该是标准箭头。

运行时框架顶部会有控件,因此我无法使用 OnMouseMove 事件。所以我用这个;

private
  procedure WMSetCursor(var Msg: TWMSetCursor); message WM_SETCURSOR;

procedure TfraApplet.WMSetCursor(var Msg: TWMSetCursor);
var
  Point: TPoint;
begin

  Point := ScreenToClient(Mouse.CursorPos);
  Label1.Caption := 'X:' + IntToStr(Point.X) + ' Y:' + IntToStr(Point.Y);

  // Resize area (bottom right)
  if (Point.X >= (Width - 10)) and (Point.Y >= (Height - 10)) then
    Screen.Cursor := crSizeNWSE
  else
    Screen.Cursor := crDefault;

end;

但是,一旦光标设置为 crSizeNWSE,我的程序就会停止接收 WM_SETCURSOR Windows 消息。

当光标未设置为默认箭头时,我是否可以收到不同的 Windows 消息?

最佳答案

并不是框架停止接收 WM_SETCURSOR 消息,而是光标卡在了 crSizeNWSE 处。当您切换回将 crDefault 设置为 Screen.Cursor 时,VCL 会向框架发送一个 WM_SETCURSOR 来让它设置默认光标。实际上,没有光标被设置。如果你想改变之前的光标,就必须设置一个光标,将最后一部分替换为:

  // Resize area (bottom right)
  if (Point.X >= (Width - 10)) and (Point.Y >= (Height - 10)) then begin
    winapi.Windows.SetCursor(Screen.Cursors[crSizeNWSE]);
    Message.Result := 1;
  end else
    inherited;


作为替代方案,您可以处理 WM_NCHITTEST 将该区域定义为大小调整区域,然后框架的默认窗口过程在处理 WM_SETCURSOR 时将设置适当的光标:

procedure TfraApplet.WMNCHitTest(var Message: TWMNCHitTest);
var
  Point: TPoint;
begin

  Point := ScreenToClient(SmallPointToPoint(Message.Pos));
  Label1.Caption := 'X:' + IntToStr(Point.X) + ' Y:' + IntToStr(Point.Y);

  // Resize area (bottom right)
  if (Point.X >= (Width - 10)) and (Point.Y >= (Height - 10)) then
    Message.Result := HTBOTTOMRIGHT
  else
    inherited;

end;

另一个好处是,您无需编写额外的代码来调整框架大小。

关于delphi - 如何在 delphi TFrame 上编写调整大小句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195671/

相关文章:

c++ - 显示断言对话框时提供 Windows 消息服务吗?

c++ - 使用 BroadcastSystemMessage 杀死旧进程

c# - 如何在 Delphi 中使用非托管导出

delphi - 连接图像 - Delphi

windows - 使用 WM_SHOWWINDOW 代替 ShowWindow() 来显示窗口

delphi - 如何停止Memo控件的自动滚动?

C++ 分层窗口和 WM_MOUSEMOVE

php - Delphi 代码在控制台中有效,但在 VLC 中无效

multithreading - Delphi中如何将异常从一个线程传递到另一个(调用者的)线程?

德尔福: 'Property ClientHeight does Not Exist'