我的目标是在没有主菜单栏的情况下从外部应用程序获取窗口的屏幕截图。我使用此代码:
BitBlt(Canvas.Handle, 0, 0, Width, Height, WinDC, xShift, yShift, SRCCOPY);
我需要自己确定xShift
和yShift
变量值,因为各种应用程序中的窗口可以有不同的样式,或者系统的主题可能不同。
所以我的问题是,如何获取窗口主菜单栏左下角的坐标(我的xShift
和yShift
变量需要)?这一点说明了这一点:
或者,有没有办法在没有主菜单栏的情况下直接获取窗口的客户端边界,没有这一步?
答案 0 :(得分:3)
如果您有窗口句柄,您可以获得所需的所有信息。 GetClientRect
函数将为您提供窗口客户区,但左上角坐标为(0,0)。要将其转换为偏移量,您必须使用ClientToScreen
函数获取该点的屏幕坐标,然后只需减去窗口屏幕坐标以获得所需的偏移量。
var
WindowRect, WindowClientRect: TRect;
Origin: TPoint;
Ofs: TPoint;
Windows.GetWindowRect(Handle, WindowRect);
Windows.GetClientRect(Handle, WindowClientRect);
Origin := WindowClientRect.TopLeft;
Windows.ClientToScreen(Handle, Origin);
Ofs.X := Origin.X - WindowRect.Left;
Ofs.Y := Origin.Y - WindowRect.Top;
所以,对BitBlt
函数的调用看起来像这样
BitBlt(Canvas.Handle, 0, 0, WindowClientRect.Width, WindowClientRect.Height, WinDC, Ofs.X, Ofs.Y, SRCCOPY);
我不确定TRect
在Delphi 2010中是否具有Width
和Height
属性,因此您可能需要计算Width
和Height
{ {1}}你自己。
答案 1 :(得分:0)
感谢@Dalija Prasnikar,工作代码是:
function WindowToBMP(WD: HWND ): TBitmap;
var
WinDC: HDC;
WindowRect, WindowClientRect: TRect;
Origin: TPoint;
Ofs: TPoint;
begin
Result := TBitmap.Create;
GetWindowRect(WD, WindowRect);
GetClientRect(WD, WindowClientRect);
Origin := WindowClientRect.TopLeft;
ClientToScreen(WD, Origin);
Ofs.X := Origin.X - WindowRect.Left;
Ofs.Y := Origin.Y - WindowRect.Top;
with Result, WindowClientRect do
begin
Width := WindowClientRect.Right - WindowClientRect.Left;
Height := WindowClientRect.Bottom - WindowClientRect.Top;
WinDC:=GetWindowDC(Wd);
ShowWindow(Wd, SW_SHOW);
BringWindowToTop(WD);
try
BitBlt( Canvas.Handle, 0, 0, Width, Height, WinDC, Ofs.X, Ofs.Y, SRCCOPY);
finally
end;
end;
end;