如何消除在获取屏幕对象句柄用于绘制焦点矩形时出现的屏幕闪烁问题。

4
我正试图在我开发多年的Apprehend屏幕捕获组件中绘制一个聚焦矩形,以便围绕所选屏幕对象。我可以通过使用Handles:=WindowFromPoint(P)获取到鼠标下的对象的句柄来DrawFocusRect,但这需要我隐藏并显示自身才能工作,否则会返回自身的句柄。
不幸的是,当我隐藏和显示窗体时,它会导致闪烁。
我可以轻松地获取所选对象的位图,只是绘制所选对象让我抓狂了。
有人有任何建议来绘制一个FocusedRect围绕所选对象,以便没有闪烁吗?是否有任何API可以获取屏幕的句柄,如果一个窗体在其上方?
我尝试使用Handles:=WindowFromDC(ScreenDC),这样我就不必隐藏和显示窗体了,但WindowFromDC仍然返回窗体而不是屏幕。
TCaptureObjectForm是透明的,覆盖在屏幕上。我需要TCaptureObjectForm在组件中。
// FormMouseMove事件 - 添加于08/2/2011
procedure TCaptureObjectForm.FormMouseMove( Sender: TObject; Shift: TShiftState; X, Y: Integer );
const
  crHand = -18;
var
  P: TPoint;
  Handles: HWND;
  Rect: TRect;
  ScreenDC: HDC;
begin
  // hide the TCaptureObjectForm form so the screen is found by WindowFromPoint
  Self.Hide;
  // get the object on the screen
  GetCursorPos( P );
  Handles := WindowFromPoint( P );
  // tried this but it returns self.handle rather than the screen handle
  //ScreenDC := GetDC( 0 );
  //Handles := WindowFromDC(ScreenDC);
  //ReleaseDC( 0, ScreenDC );
  // restore the TCaptureObjectForm
  Self.Show;
  // get object rect
  GetWindowRect( Handles, Rect );
  // draw a rect to show it is focused
  Self.Canvas.DrawFocusRect( Rect );
end;

在将DoubleBuffered设置为true后,闪烁减少了很多,但是FocusedRect仍然会闪烁。有更好的方法吗? - Bill
抱歉,我没有其他的想法了... - opc0de
也许David Heffernan在这个问题的回答可以帮到你。 - JRL
2个回答

2

这篇文章是一个微软Visual Basic示例,非常类似于您所需要的功能。

他们采取了以下方法:

  1. Form_MouseDown中捕获鼠标。
  2. 随着鼠标移动,在指向窗口的位置绘制一个矩形:Form_MouseMove
  3. Form_MouseUp中释放鼠标,并使整个屏幕无效以擦除上次绘制的矩形。

他们直接在所选窗口中绘制。我认为透明窗口方法无法避免所有闪烁。

那个代码示例似乎不完整,而且工作效果也不好,因此我对其进行了修改(并将其翻译为Delphi):

// Not global variables, but private form ones
var
  HwndLastTracked: HWND;
  CapturedMouse: boolean;

procedure InvertTracker(hwndWindow: HWND);
var
  rc: TRect;
  dc: HDC;
  pen, oldPen: HPEN;
  oldBrush: HBRUSH;
  style, exStyle: longint;
  cx, cy: integer;
begin
  GetWindowRect(hwndWindow, rc);

  // Window coordinates of the origin (top-left corner) of a window is (0, 0)
  OffsetRect(rc, -rc.Left, -rc.Top);

  // DC returned by GetWindowDC covers the full window area, but in Windows
  // Vista/7 it seems to be clipped excluding the nonclient region, due to
  // DWM handling nonclient drawing, so it doesn't allow painting over it.
  // Thus we need to skip this nonclient area and that is why I adjust the
  // window rect to match the client area. Using GetClientRect instead of
  // GetWindowRect is not suitable as excludes scroll bars and child 
  // parts drawed in WM_NCPAINT, such as Windows' WS_EXEDGEs and Delphi's
  // bevels.

  style := GetWindowLong(hwndWindow, GWL_STYLE);
  exStyle := GetWindowLong(hwndWindow, GWL_EXSTYLE);

  if style and WS_CAPTION <> 0 then begin
    if exStyle and WS_EX_TOOLWINDOW <> 0 then
      cy := GetSystemMetrics(SM_CYSMCAPTION)
    else
      cy := GetSystemMetrics(SM_CYCAPTION);

    // discard area covered by caption 
    Inc(rc.Top, cy);
  end;

  if style and WS_THICKFRAME <> 0 then begin
    cx := GetSystemMetrics(SM_CXFRAME);
    cy := GetSystemMetrics(SM_CYFRAME);
  end
  else if style and WS_DLGFRAME <> 0 then begin
    cx := GetSystemMetrics(SM_CXDLGFRAME);
    cy := GetSystemMetrics(SM_CYDLGFRAME);
  end
  else if style and WS_BORDER <> 0 then begin
    cx := GetSystemMetrics(SM_CXBORDER);
    cy := GetSystemMetrics(SM_CYBORDER);
  end
  else begin
    cx := 0;
    cy := 0;
  end;

  if (cx <> 0) or (cy <> 0) then begin
    // discard area covered by borders
    OffsetRect(rc, cx, cy);
    Dec(rc.Right, cx*2);
    Dec(rc.Bottom, cy*2);
  end;

  // Windows API functions don't raise exceptions, so I don't use try-finally

  dc := GetWindowDC(hwndWindow);

  // Option 1: focused rect
  //DrawFocusRect(dc, rc);

  // Option 2: inverted thick border
  SetROP2(dc, R2_NOT);
  pen := CreatePen(PS_INSIDEFRAME, 3 * GetSystemMetrics(SM_CXBORDER), 0);
  oldPen := SelectObject(dc, pen);
  oldBrush := SelectObject(dc, GetStockObject(NULL_BRUSH));

  Rectangle(dc, rc.Left, rc.Top, rc.Right, rc.Bottom);

  SelectObject(dc, oldBrush);
  SelectObject(dc, oldPen);
  DeleteObject(pen);
  // End option 2

  ReleaseDC(hwndWindow, dc);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if SetCapture(Handle) <> 0 then begin
    CapturedMouse := true;
    HwndLastTracked := 0;
    Screen.Cursor := crCross;
  end;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  hwndCaptured: HWND;
begin
  if CapturedMouse then begin
    hwndCaptured := WindowFromPoint(ClientToScreen(Point(X, Y)));

    // Uncomment this for track root windows instead of childs
    //hwndCaptured := GetAncestor(hwndCaptured, GA_ROOT);

    if hwndCaptured <> HwndLastTracked then begin
      if HwndLastTracked <> 0 then
        InvertTracker(HwndLastTracked);
      InvertTracker(hwndCaptured);
      HwndLastTracked := hwndCaptured;
    end;
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CapturedMouse then begin
    ReleaseCapture;
    CapturedMouse := false;

    if HwndLastTracked <> 0 then begin
      InvertTracker(HwndLastTracked);
      HwndLastTracked := 0;
    end;

    Screen.Cursor := crDefault;
  end;
end;

这是微软在Visual Studio的Spy++中使用该技术的屏幕截图。红色气球和文字是我添加的!

Spy++


我试图将Visual Basic代码转换为Delphi,但整个屏幕变黑,整个屏幕都被聚焦了...现在暂时回到我的旧代码... - Bill
@Bill:我已经编辑了我的答案,并添加了一个由微软有缺陷的代码制作的工作示例。 - JRL
@Bill:使用这种技术,您不会得到一个透明的窗口,只有一个类似于截图中的小“查找窗口”,用户必须单击“查找工具”,将其拖动到他/她想要选择的窗口上,并释放鼠标按钮。我已在XP和Win 7中进行了测试。 - JRL
@JRL - 你错了,没有剪裁。只是你计算矩形的方法不对。删除所有从第一个 ScreenToClient(.. 调用开始(包括该调用)到 (不包括) dc := GetWindowDC(.. 调用的代码。只需使用 OffsetRect(rc, -rc.Left, -rc.Top); 替换所有这些琐碎的操作即可。 - Sertac Akyuz
@JRL - 我已经为您进行了修改。现在更加简洁和正确,因此+1 ;) - Sertac Akyuz
显示剩余6条评论

0

我使用这个

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.DoubleBuffered:=True;
end;

这种方法的缺点是它会导致某些组件无法正确渲染。 - David Heffernan
我从来没有遇到过这个问题,而且我一直在使用它...这就是为什么Borland实现它以减少闪烁,但我不知道更好的解决方案。如果你有一个,请告诉我,因为这是一个我经常遇到的问题。 - opc0de
几个例子,都是在Vista上:1. 在Windows基本主题下尝试下拉列表。使用窗体的双缓冲会看起来很糟糕。2. 默认或聚焦的TButton会脉动以指示如果按下回车键将被按下。但是如果有窗体双缓冲,则不会。实际上,如果按钮双缓冲,则也不会。我成功地从我的应用程序中删除了所有闪烁,而没有使用双缓冲。总有一天我会写下我如何在SO wiki问题中做到这一点。 - David Heffernan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接