为什么带有pmXOR笔的TCanvas.Rectangle()只能“有时”工作?

时间:2011-01-26 16:14:27

标签: delphi desktop draw drawrect

我开发了以下AnimateRects()方法在Windows桌面上绘制动画矩形。我用它来动画显示模态形式,使它看起来像是从网格单元“增长”。

在表单显示之前,我使用bExpand参数= True调用该方法一次。然后,当用户关闭表单时,我再次调用它,但使用bExpand = False,以显示表单“折叠”到网格单元格中。

问题在于bExpand = False情况......在循环的第一次迭代中,第一次调用Rectangle(r)按预期绘制矩形,但就像<从未调用过强>第二对Rectangle(r)的调用 - 第一个矩形永远不会被异或。因此,在绘制了“折叠”矩形序列之后,我最终将第一个矩形保留为屏幕上的工件

任何想法我做错了什么?

const
  MSECS_PER_DAY = 24.0 * 60.0 * 60.0 * 1000;

procedure DelayMSecs(msecs: Word);
var
  Later:  TDateTime;
begin
  Later := Now + (msecs / MSECS_PER_DAY);
  while Now < Later do begin
    Application.ProcessMessages;
    sleep(0);     //give up remainder of our time slice
  end;
end;


procedure T_fmExplore.AnimateRects(ASourceRect, ADestRect: TRect; bExpand:
    boolean; bAdjustSourceForFrame: boolean = True);
const
  MINSTEPS = 10;
  MAXSTEPS = 30;
  MAXDELAY = 180;              //150 - 200 is about right
  MINDELAY = 1;
var
  iSteps: integer;
  DeltaHt: Integer;               //Rect size chg for each redraw of animation window
  DeltaWidth: Integer;
  DeltaTop :  integer;            //Origin change for each redraw
  DeltaLeft :  integer;
  NewWidth, NewHt: Integer;
  iTemp: Integer;
  iDelay: integer;
  r : Trect;
  ScreenCanvas: TCanvas;
begin
  r := ASourceRect;
  with r do begin
    NewWidth :=   ADestRect.Right - ADestRect.Left;           //Target rect's Width
    NewHt :=      ADestRect.Bottom - ADestRect.Top;           //Target rect's Height
        //Temporarily, Deltas hold the total chg in Width & Height
    DeltaWidth := NewWidth - (Right - Left);                //NewWidth - old width
    DeltaHt :=    NewHt - (Bottom - Top);
        //With a static number of iSteps, animation was too jerky for large windows.
        //So we adjust the number of iSteps & Delay relative to the window area.
    iSteps := Max( DeltaWidth * DeltaHt div 6500, MINSTEPS );  //eg. 10 iSteps for 250x250 deltas (62500 pixels)
    iSteps := Min( iSteps, MAXSTEPS );
        //Now convert Deltas to the delta in window rect size
    DeltaWidth := DeltaWidth div iSteps;
    DeltaHt :=    DeltaHt div iSteps;
    DeltaTop :=   (ADestRect.Top - ASourceRect.Top) div iSteps;
    DeltaLeft :=  (ADestRect.Left - ASourceRect.Left) div iSteps;

    iDelay := Max( MAXDELAY div iSteps, MINDELAY );

    ScreenCanvas := TCanvas.Create;
    try
      ScreenCanvas.Handle := GetDC( 0 );              //Desktop
      try
        with ScreenCanvas do begin
          Pen.Color := clWhite;
          Pen.Mode := pmXOR;
          Pen.Style := psSolid;
          Pen.Width := GetSystemMetrics(SM_CXFRAME);
          Brush.Style := bsClear;
          if bAdjustSourceForFrame then
            InflateRect(ASourceRect, -Pen.Width, -Pen.Width);

          repeat
            iTemp := (Bottom - Top) + DeltaHt;        //Height
            if (bExpand and (iTemp > NewHt)) or (not bExpand and (iTemp < NewHt)) then begin
              Top := ADestRect.Top;
              Bottom := Top + NewHt;
            end else begin
              Top := Top + DeltaTop;            //Assign Top first...Bottom is calc'd from it
              Bottom := Top + iTemp;
            end;

            iTemp := (Right - Left) + DeltaWidth;     //Width
            if (bExpand and (iTemp > NewWidth)) or (not bExpand and (iTemp < NewWidth)) then begin
              Left := Left + DeltaLeft;
              Right := Left + NewWidth;
            end else begin
              Left := Left + DeltaLeft;         //Assign Left first...Right is calc'd from it
              Right := Left + iTemp;
            end;

            ScreenCanvas.Rectangle(r);
            SysStuff.DelayMSecs( iDelay );
            ScreenCanvas.Rectangle(r);               //pmXOR pen ...erase ourself

          until (Right - Left = NewWidth) and (Bottom - Top = NewHt);
        end;
      finally
        ReleaseDC( 0, ScreenCanvas.Handle );
        ScreenCanvas.Handle := 0;
      end;
    finally
      ScreenCanvas.Free;
    end;
  end;
end;

1 个答案:

答案 0 :(得分:1)

最有可能的问题是,当模态形式仍然可见时,您开始绘制矩形。在某一点上,表单从屏幕上消失,上面有一个矩形,当你绘制相同的矩形以擦除前一个时,它现在在屏幕上。请注意,在表单上调用“Free”,“Hide”等不会立即隐藏它。

(编辑:这需要一些解释:表单将在代码的下一行运行之前隐藏,但无法保证未覆盖的窗口何时更新其无效区域)。

在关闭模式表单之后和调用Sleep之前,或者调用AnimateRects之前,解决方案将是Application.ProcessMessages 一段时间。如果模态形式不完全在您自己的应用程序的窗口上,后者可能没有多大帮助。如果模态形式在一个同时不断进行绘制的应用程序上,那么前者可能不会有太大帮助。像任务经理f.i ...

编辑虽然我可能对此感到不满,但这个问题正是LockWindowUpdate存在的原因。当你想到它时,你会发现当你移动它时,当你显示一个窗口的拖动轮廓时(当禁用“拖动时显示窗口内容”),你正在做的事情与shell的作用没有什么不同。