C#实现 NX窗口中进度条显示,Winform样式 ,不说废话,直接上效果图!
*GIF示例
正题如下
思路与方法
思路
1.创建Winform窗口或者控件
2.将改窗口设置为NX的子窗口
3.(可选)监听NX窗口的大小变化,一般NX是单线程的,界面基本是卡死状态,所以这个用不太上。
工具
1.任意版本的VS,本实例使用VS2022
2.任意版本NX,本实例使用NX10.0.3.5
1.在nx环境中创建这个进度条的窗口,我这直接使用窗口,而不是控件,因为要设置大小
public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();
//在这里初始化winform界面
//你也可以在其他地方,实例化这个界面,
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ProgressBar_Nx.Form1(UFSession.GetUFSession().Ui.GetDefaultParent()));
theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
return retValue;
}
2. WinForm内的响应
2.1 dll引用
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
2.2 构造函数与定义的变量
intPtr 是从另一边传值过来的,你也可以使用 UFSession.GetUFSession().Ui.GetDefaultParent() 直接得到NX的句柄
public IntPtr nx;
public IntPtr nx2;
public Rect rect;
public Form1(IntPtr intPtr)
{
InitializeComponent();
nx = intPtr;
this.Location = new Point(0, 0);
}
2.3 窗口Load事件
private void Form1_Load(object sender, EventArgs e)
{
nx2 = FindWindowEx(nx,IntPtr.Zero, "MDIClient", null);
SetParent(this.Handle, nx2);
}
这里不一定要放在 MDIClient 里面,你可以放置在NX任意的一个位置,我个人觉得放置在MDI内好看而已!
2.4 窗口大小
我这里使用计时器来更新窗口大小,如果是实际项目中,NX跑循环的化界面应该是卡死状态,我们可以直接设置大小就好了
private void timer1_Tick(object sender, EventArgs e)
{
if (nx2 != IntPtr.Zero)
{
GetWindowRect(nx2,out rect);
this.Size = new Size(rect.Right - rect.Left,30);
}
}
项目地址
Github内搜索 ProgressBar_Nx