Bill
Bill

Reputation: 3033

How to eliminate screen flicker when getting handle of screen object to DrawFocusRect

I am attempting to draw a focused rectangle around a selected screen object in my Apprehend Screen Capture Component that was developed many years ago. I can DrawFocusRect by getting the handle to the object under the cursor with Handles := WindowFromPoint( P ); but that requres me to hide then show self for it to work otherwise the handle to self is returned.

Unfortunately when I hide and show the form it causes flicker when the form is hidden and shown.

I can get the bitmap of the selected object with no problem, just painting the selected object is driving me nuts.

Does anyone have any suggestions to draw a FocusedRect around the selected object so there is no flickering? Is there any API to get a handle to the screen if a form lies on top of it?

I tried using Handles := WindowFromDC(ScreenDC) so I do not have to hide and show the form, but WindowFromDC still returns the form rather than the screen.

The TCaptureObjectForm is transparent and lies over the screen. I need the TCaptureObjectForm in the component.

// FormMouseMove event - added 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;

Upvotes: 4

Views: 1855

Answers (2)

JRL
JRL

Reputation: 3401

This article is an example in Visual Basic from Microsoft doing something very similar to what you need.

They take the following approach:

  1. Capture the mouse in Form_MouseDown.
  2. As the mouse moves, draw a rectangle around the window pointed with the mouse: Form_MouseMove.
  3. Release the mouse in Form_MouseUp and also invalidate the entire screen to erase the last rectangle drawn.

They draw directly in the window that they are selecting. I don't think that all flickering can be avoided using the transparent window approach.

That code sample seems incomplete and doesn't work well, so I've modified it (and translated to 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;

Here is a screenshot of how Microsoft uses this technique in Visual Studio's Spy++. The red balloon and text are mine!

Spy++

Upvotes: 2

opc0de
opc0de

Reputation: 11768

I use this

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

Upvotes: 0

Related Questions