delphi - 菜单翻转闪烁 - 如何解释鼠标离开事件

标签 delphi canvas paint mousemove

我正在构建一个自定义菜单,并且在链接上的悬停状态方面遇到了一些问题。现在,经过多次修补,我设法让我的菜单矩形正确响应鼠标悬停状态 - 几乎。

一旦鼠标离开矩形,我一生都无法弄清楚如何让它们恢复正常 - 它仍然处于悬停状态。移动到不同的矩形会正确重置, Canvas 上的其他任何地方都被视为仍然在最后一个悬停的矩形中。

我的 MouseMove 程序。

procedure TOC_MenuPanel.MouseMove(Shift:TShiftState; X,Y:Integer);
var
  i : integer;
begin
 pt := Mouse.CursorPos;
 pt := ScreenToClient(pt);
 for i := 0 to High(MenuRects) do
   begin
     if PtInRect(MenuRects[i], Point(X, Y)) then
        begin
           chosenRect := i;
           Refresh;
        end
     else
        begin
           chosenRect := -1;
        end;
   end;
   inherited;
end;

我的油漆程序:
procedure TOC_MenuPanel.Paint;
var
  // TextStyle: TTextStyle;
  R, itemR: TRect;
  count : Integer;
  x1,y1,x2,y2 : Integer;

begin
  // Set length of array
  SetLength(MenuRects, fLinesText.Count);

// Set TRect to Canvas size
R := Rect(5, 5, Width-5, Height-5);
x1 := 10;
y1 := 10;
x2 := Width-10;

inherited Paint;

with Canvas do begin
  // Set fonts
  Font.Height := MenuFontHeight;
  Font.Color := clWhite;

  // Draw outerbox
  GradientFill(R, clLtGray, clWhite, gdVertical);

  // Draw inner boxes
  if fLinesText.Count = 0 then exit
     else
  for count := 0 to fLinesText.Count - 1 do
     begin
       // Define y2
       y2 := TextHeight(fLinesText.strings[count])*2;

       itemR := Rect(x1, y1, x2, y2*(count+1));
       Pen.color := clGray;

     // Test against chosenRect value and compare mouse position against that of the rectangle
       if (chosenRect = count) and (PtInRect(MenuRects[count], pt)) then
          Brush.color := stateColor[bttn_on]
       else
          Brush.color := stateColor[bttn_off];

       Rectangle(itemR);

       // Push rectangle info to array
       MenuRects[count] := itemR;

       // Draw the text
       TextRect(itemR, x1+5,  y1+5, fLinesText.strings[count]);

       // inc y1 for positioning the next box
       y1 := y1+y2;
     end;
  end;
end;

最佳答案

您在鼠标移动事件处理程序中执行的绘制会立即丢失,因为您通过调用 Invalidate 强制绘制循环.作为一般规则,最好在一个绘制周期中对屏幕进行所有绘制。在某些情况下,在绘制周期之外绘制是有意义的,但众所周知,要做到正确是非常困难的。

因此,我怀疑您需要将所有绘图代码移动到您的绘图程序中,无论它是什么地方。因此,在鼠标移动事件中,您需要使表单或绘制框或绘制场景的任何内容无效。然后在您的绘画程序中使用 GetCursorPos , 或 Mouse.Pos或类似的找到光标的位置。你用它来决定如何绘制场景。您可能会发现,在避免闪烁方面,将其绘制到屏幕外位图然后将其 blit 到 Canvas 上会更有效。

现在,如果您在每次鼠标移动时都无效,那么您可能会发现绘画负担过重。因此,也许您应该跟踪最近绘制的场景的状态。在鼠标移动处理程序中测试新状态是否与最近绘制的不同。只有当它确实不同时,您才会强制执行绘制周期。

关于delphi - 菜单翻转闪烁 - 如何解释鼠标离开事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23936550/

相关文章:

ios - 无法使用 delphi firemonkey iOS 应用程序在应用程序购买中进行测试

javascript - Firefox 中屏幕外 Canvas 的性能问题

html - 关闭 HTML Canvas 的抗锯齿功能仍然无法实现吗?

java - 如何删除 JPanel 对象?

mysql - Delphi 5,如何从 MySQL 的 tis620 字段中检索数据?

forms - Delphi - 如何从该表单上的 TFrame 内关闭表单?

delphi - 编辑路径中的第一个INI文件

jquery - 如何将标签添加到 Chart.js Canvas 插件中?

c - 简单的开源画图程序

java - Android - paint.setShadowLayer 忽略 shadowColor