gpt4 book ai didi

delphi - 将屏幕对象句柄获取到 DrawFocusRect 时如何消除屏幕闪烁

转载 作者:行者123 更新时间:2023-12-03 15:33:36 26 4
gpt4 key购买 nike

我正在尝试在多年前开发的 Apprehend 屏幕捕获组件中围绕选定的屏幕对象绘制聚焦矩形。我可以通过使用 Handles := WindowFromPoint( P ); 获取光标下对象的句柄来绘制焦点矩形。但这需要我隐藏然后显示 self 才能工作,否则会返回 self 的句柄。

不幸的是,当我隐藏和显示表单时,它会在隐藏和显示表单时导致闪烁。

我可以毫无问题地获取所选对象的位图,只是绘制所选对象就让我发疯了。

有人有什么建议可以在选定的对象周围绘制 FocusedRect 以避免闪烁吗?如果表单位于屏幕顶部,是否有 API 可以获取屏幕句柄?

我尝试使用 Handles := WindowFromDC(ScreenDC) 因此我不必隐藏和显示表单,但 WindowFromDC 仍然返回表单而不是屏幕。

TCaptureObjectForm 是透明的并且位于屏幕上方。我需要组件中的 TCaptureObjectForm。

//FormMouseMove 事件 - 添加于 2011 年 8 月 2 日

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;

最佳答案

This article是 Microsoft 的 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;

下面是 Microsoft 如何在 Visual Studio 的 Spy++ 中使用此技术的屏幕截图。红色气球和文字是我的!

Spy++

关于delphi - 将屏幕对象句柄获取到 DrawFocusRect 时如何消除屏幕闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6915510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com