我需要将一个 WinForms 表单(带有 BorderStyle = None
)嵌入到 Inno 安装向导中,但遇到了问题。
这是一个 Inno Setup 脚本:
procedure EmbedConfiguratorForm(parentWnd: HWND);
external 'EmbedConfiguratorForm@files:configurator.dll stdcall';
procedure InitializeWizard();
var
cfgPageHandle: HWND;
begin
cfgPageHandle := CreateCustomPage(wpSelectDir,
'Configuration',
ExpandConstant(description)).Surface.Handle;
EmbedConfiguratorForm(cfgPageHandle);
end;
这是一个 C# 代码:
class WizardWindow : IWin32Window
{
public WizardWindow(IntPtr handle)
{
Handle = handle;
}
public WizardWindow(int handle) : this(new IntPtr(handle))
{
}
public IntPtr Handle { get; private set; }
}
public static class MainClass
{
[DllExport("EmbedConfiguratorForm", CallingConvention.StdCall)]
public static void EmbedConfiguratorForm(int parentWnd)
{
// System.Diagnostics.Debugger.Launch();
ConfiguratorForm form = new ConfiguratorForm();
form.Show(new WizardWindow(parentWnd));
}
}
它有效但不像预期的那样。安装程序加载后,它会自动从
EmbedConfiguratorForm
调用 configurator.dll
并且表单显示但不进入安装向导页面。它显示在后面(见截图)。那么我做错了什么?
最佳答案
解决了。
解决办法是从DLL中返回一个新窗口(窗体)的句柄,并使用user32.SetParent
WinAPI函数将窗体强制嵌入到向导中。这里有一段代码。
C#:
namespace configurator
{
class WizardWindow : IWin32Window
{
public WizardWindow(IntPtr handle)
{
Handle = handle;
}
public WizardWindow(int handle) : this(new IntPtr(handle))
{
}
public IntPtr Handle { get; private set; }
}
public static class MainClass
{
private static ConfiguratorForm _configuratorForm;
[DllExport("EmbedConfiguratorForm", CallingConvention.StdCall)]
public static IntPtr EmbedConfiguratorForm(int parentWnd)
{
_configuratorForm = new ConfiguratorForm();
_configuratorForm.Show(new WizardWindow(parentWnd));
return _configuratorForm.Handle;
}
[DllExport("CloseConfiguratorForm", CallingConvention.StdCall)]
public static void CloseConfiguratorForm()
{
if (_configuratorForm != null)
{
_configuratorForm.Close();
_configuratorForm.Dispose();
_configuratorForm = null;
}
}
}
}
Inno 安装脚本:
[Code]
const
description = 'my page description';
var
configFile: string;
configuratorPage: TWizardPage;
function EmbedConfiguratorForm(parentWnd: HWND): HWND;
external 'EmbedConfiguratorForm@files:configurator.dll stdcall';
procedure CloseConfiguratorForm();
external 'CloseConfiguratorForm@files:configurator.dll stdcall';
function SetParent(hWndChild, hWndNewParent: HWND): HWND;
external '[email protected] stdcall';
procedure InitializeWizard();
begin
configuratorPage := CreateCustomPage(wpSelectDir,
'Title', 'Description');
end;
procedure ShowConfigurationStep();
var
cfgPageHandle: HWND;
cfgWinHandle: HWND;
begin
cfgPageHandle := configuratorPage.Surface.Handle;
cfgWinHandle := EmbedConfiguratorForm(cfgPageHandle);
SetParent(cfgWinHandle, cfgPageHandle);
end;
procedure CurPageChanged(CurPageId: Integer);
begin
if (CurPageId = configuratorPage.ID) then
begin
ShowConfigurationStep();
end else
begin
CloseConfiguratorForm(); // here we can make some optimization like checking previos page
end;
end;
procedure DeinitializeSetup();
begin
CloseConfiguratorForm();
end;
关于 C# DLL 的注意事项:
它使用
UnmanagedExports
NuGet 数据包(包含 DLLExportAttribute
)。关于 Inno Setup 脚本的注意事项:
在
InitializeWizard
函数中我们只需要创建新页面,但是我们需要在 CurPageChanged
中实现 DLL 调用以确保我们的页面现在被打开。经过一些研究工作,我创建了一个小示例项目来解释 .Net 和 InnoSetup 的双向集成
https://github.com/sharpcoder7/innoGlue.net
关于c# - 将 WinForms 表单嵌入 Inno Setup 向导,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34581407/