110,532
社区成员
发帖
与我相关
我的任务
分享
class WindowWrapper : IWin32Window
{
public IntPtr Handle {get; set;}
}
// 调用方法:
IntPtr intptr = FindWindow等等;
Show(new WindowWrapper (){Handle = intptr});
// MyWindow.cs
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
public interface IMyWindow
{
Point Location { get; set; }
Size Size { get; set; }
}
public class MyWindow : MarshalByRefObject, IMyWindow
{
public Point Location
{
get
{
Form form = Application.OpenForms.OfType<Form>().FirstOrDefault();
return form == null ? Point.Empty : form.Location;
}
set
{
Form form = Application.OpenForms.OfType<Form>().FirstOrDefault();
if (form != null) form.Location = value;
}
}
public Size Size
{
get
{
Form form = Application.OpenForms.OfType<Form>().FirstOrDefault();
return form == null ? Size.Empty : form.Size;
}
set
{
Form form = Application.OpenForms.OfType<Form>().FirstOrDefault();
if (form != null) form.Size = value;
}
}
}
public Form1()
{
InitializeComponent();
// 登记Remote Server
try
{
ChannelServices.RegisterChannel(new TcpChannel(54322), false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyWindow), "MyWindow", WellKnownObjectMode.SingleCall);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
4、再新建一个Winform项目作为客户。
5、拉一个PropertyGrid到窗体上。
6、打开该项目的Form1.cs
7、改成类似以下代码(关键是接口不要有任何命名空间,与Server保持一致)
8、记得要先运行server,再运行客户。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.propertyGrid1.SelectedObject = new MyController(); // -- 添加
}
}
}
// -- 添加
public interface IMyWindow
{
Point Location { get; set; }
Size Size { get; set; }
}
public class MyController
{
IMyWindow remoteObject;
public MyController()
{
try
{
remoteObject = Activator.GetObject(typeof(IMyWindow), "tcp://localhost:54322/MyWindow") as IMyWindow;
}
catch (Exception ex)
{
this.Error = "Failed remote activation:" + ex.Message;
}
}
public string Error { get; private set; }
public Point Location
{
get { return remoteObject == null ? Point.Empty : remoteObject.Location; }
set { if (remoteObject != null) remoteObject.Location = value; }
}
public Size Size
{
get { return remoteObject == null ? Size.Empty : remoteObject.Size; }
set { if (remoteObject != null) remoteObject.Size = value; }
}
}
// -- 结束添加