gpt4 book ai didi

delphi - 将窗口嵌入到另一个进程中

转载 作者:行者123 更新时间:2023-12-03 15:22:38 25 4
gpt4 key购买 nike

我在 StackOverflow 上读过一些帖子,但没有一个对我有用。这是我用来在表单上显示标准计算器窗口的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
Tmp: Cardinal;
R: TRect;
begin
CalcWindow := FindWindow(nil, 'Calculator');
if (CalcWindow <> 0) then
begin
GetWindowThreadProcessID(CalcWindow, CalcProcessID);

Tmp := GetWindowLong(CalcWindow, GWL_STYLE);
Tmp := (Tmp and not WS_POPUP) or WS_CHILD;
SetWindowLong(CalcWindow, GWL_STYLE, Tmp);
GetWindowRect(CalcWindow, R);

SetForegroundWindow(CalcWindow);
Windows.SetParent(CalcWindow, Panel1.Handle);
SetWindowPos(CalcWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_FRAMECHANGED);

AttachThreadInput(GetCurrentThreadID(), CalcWindow, True);
end;
end;

它确实在我的表单上显示了窗口,但玻璃边框丢失了,有时(特别是当我移动表单时),很难将焦点恢复到嵌入的窗口(我需要单击几次)。

这可能是什么原因造成的?另外,您是否发现使用此方法可能会遇到任何潜在问题?

感谢您的宝贵时间。

最佳答案

试试这个代码。我从我的一份旧源代码中获取了它。您将失去玻璃框架,但主菜单是可见的,并且我没有注意到将焦点设置回嵌入式应用程序有任何问题。您应该能够使用 SetForegroundWindow() API 函数来执行此操作。每当您移动容器表单时,您的嵌入式应用程序就会失去焦点,因此您需要再次调用 SetForegroundWindow 来恢复焦点:

procedure ShowAppEmbedded(WindowHandle: THandle; Container: TWinControl);
var
WindowStyle : Integer;
FAppThreadID: Cardinal;
begin
/// Set running app window styles.
WindowStyle := GetWindowLong(WindowHandle, GWL_STYLE);
WindowStyle := WindowStyle
- WS_CAPTION
- WS_BORDER
- WS_OVERLAPPED
- WS_THICKFRAME;
SetWindowLong(WindowHandle,GWL_STYLE,WindowStyle);

/// Attach container app input thread to the running app input thread, so that
/// the running app receives user input.
FAppThreadID := GetWindowThreadProcessId(WindowHandle, nil);
AttachThreadInput(GetCurrentThreadId, FAppThreadID, True);

/// Changing parent of the running app to our provided container control
Windows.SetParent(WindowHandle,Container.Handle);
SendMessage(Container.Handle, WM_UPDATEUISTATE, UIS_INITIALIZE, 0);
UpdateWindow(WindowHandle);

/// This prevents the parent control to redraw on the area of its child windows (the running app)
SetWindowLong(Container.Handle, GWL_STYLE, GetWindowLong(Container.Handle,GWL_STYLE) or WS_CLIPCHILDREN);
/// Make the running app to fill all the client area of the container
SetWindowPos(WindowHandle,0,0,0,Container.ClientWidth,Container.ClientHeight,SWP_NOZORDER);

SetForegroundWindow(WindowHandle);
end;

你可以这样调用它:

  ShowAppEmbedded(FindWindow(nil, 'Calculator'), Panel1);

关于delphi - 将窗口嵌入到另一个进程中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7611103/

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