我正在开发可以优化我的工作的应用程序,它通过RDP到服务器的多个连接.到那时,我想做一个单一的Windows表单,向我显示所有服务器.据我所知-我想以编程方式在Windows窗体中添加AxMsRdpClient2或AxMsTscAxNotSafeForScripting控件.在开始时,我有一个Form1,我在其中保存我的凭据(IP,USER,PASS),并使用简单的FOR来将ActiveX Client添加到Form2中并连接到服务器.
这是我的问题.让我们在下面看我的代码.
从Form1()调用>
var btt = new RunRDP();
for (int i = 0; i < 15; i++)
{
btt.PozitionMaster();
}
RunRDP类别:
private static int _count;
private static int _row = 4;
private static int _colum = 4;
private static int _objId;
public void PozitionMaster()
{
if (_count != 6)
{
UInitializer(_objId++, _row, _colum);
_row += 196;
_count++;
}
else
{
_colum += 196;
_row = 4;
_count = 0;
}
}
UInitializer函数:
public AxMsRdpClient2 rdpClient;
public void UInitializer(int id, int x, int y)
{
Form2 ippo = new Form2();
rdpClient = new AxMsRdpClient2();
rdpClient.UserName = "username";
rdpClient.Server = "192.168.0.100";
rdpClient.Height = y;
rdpClient.Width = x;
rdpClient.BeginInit();
rdpClient.CreateControl();
if (ippo.InvokeRequired)
{
ippo.Invoke(new Action(() => ippo.Controls.Add(rdpClient)));
ippo.Invoke(new Action(() => ippo.Update()));
}
else
{
ippo.Controls.Add(rdpClient);
}
//this.Controls.Add(rdpClient);
rdpClient.AdvancedSettings2.RDPPort = 3389;
rdpClient.AdvancedSettings2.ClearTextPassword = "hello_rdp";//
rdpClient.ColorDepth = 16;//
rdpClient.FullScreen = false;//
rdpClient.Connect();
}
得到那个错误:
An unhandled exception of type 'System.Windows.Forms.AxHost.
InvalidActiveXStateException' occurred in AxInterop.MSTSCLib.dll
{"Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown."}
解决方法:
当您在创建ActiveX控件的本机窗口之前开始使用它时,会引发InvalidActiveXStateException.直到您调用ippo.Show()之后,这种情况才会发生.在该语句之后移动关键属性分配和方法调用.