• 版本

NX9+VS2012

 

  • 说明

我们可以通过获得NX的父项窗口句柄来遍历下面的子项窗口句柄。

然后对每个句柄单独发送消息,来执行操作。

获得NX窗口句柄用这个函数UF_UI_get_default_parent()

遍历子窗口用这个函数EnumChildWindows

 

下面的这个例子是遍历类型过滤器句柄,然后模拟鼠标点击,选择下拉类型。

但是这个例子还不够完善,还有很多问题,后续在花点时间不断完善。

本来想得到句柄后,直接用SetWindowText去设置过滤器当前显示的内容,结果还不行。不起作用。

后来又想用SendMessage发送消息,设置显示内容WM_SETTEXT,还是不行,设置下拉枚举第几个也不行。

几种方法试了都不行,才选择了用鼠标模拟点击去做的。


NX9+VS2012

//NX9_NXOpenCPP_Wizard14

// Mandatory UF Includes

#include <uf.h>
#include <uf_object_types.h>

// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>

// Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>

//头文件
#include <uf.h>
#include <uf_ui.h>
#include <atlstr.h>

// Std C++ Includes
#include <iostream>
#include <sstream>



using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr;


//------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
// class members
public:
static Session *theSession;
static UI *theUI;

MyClass();
~MyClass();

void do_it();
void print(const NXString &);
void print(const string &);
void print(const char*);

private:
Part *workPart, *displayPart;
NXMessageBox *mb;
ListingWindow *lw;
LogFile *lf;
};

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL;

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{

// Initialize the NX Open C++ API environment
MyClass::theSession = NXOpen::Session::GetSession();
MyClass::theUI = UI::GetUI();
mb = theUI->NXMessageBox();
lw = theSession->ListingWindow();
lf = theSession->LogFile();

workPart = theSession->Parts()->Work();
displayPart = theSession->Parts()->Display();

}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
}

//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}



//枚举子窗口回调函数原型
static int child_num;
static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)//回调函数
{
if (hwnd) //如果子窗口存在
{
child_num++;//记录子窗口的数目
LPTSTR child_buffer_1 = new char[255];//子窗口的标题名称
LPTSTR child_buffer_2 = new char[255];//子窗口的类名
GetWindowText(hwnd, child_buffer_1, 255);//则开始获取窗口的标题
GetClassName(hwnd, child_buffer_2, 255);//则开始获取窗口的类名

//char msg[500];
//sprintf(msg, "子窗口的标题名称:%s,子窗口的类名%s\n", child_buffer_1, child_buffer_2);
//UF_UI_write_listing_window(msg);

if (strcmp(child_buffer_1, "No Selection Filter") == 0)//判断子窗口标题名称
{
//保存当前鼠标指针
//取得当前鼠标位置
POINT p;//x,y
GetCursorPos(&p);

//开始移动鼠标模拟点击
//移动到过滤器位置处
RECT rect;
GetWindowRect(hwnd, &rect);
SetCursorPos(rect.left + 30, rect.top + 10);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

//设置点击下拉内容
SetCursorPos(rect.left + 30, rect.top + 50);//计算鼠标位置
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

//还原鼠标位置
SetCursorPos(p.x,p.y);
}
}


return TRUE;
}

//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{

// TODO: add your code here

UF_initialize();

//UF_UI_open_listing_window();


HWND UGHwnd = (HWND)UF_UI_get_default_parent();//获取UG窗口句柄
LPTSTR child_buffer_1 = new char[255];//父窗口的标题名称
LPTSTR child_buffer_2 = new char[255];//父窗口的类名
GetWindowText(UGHwnd, child_buffer_1, 255);//则开始获取窗口的标题
GetClassName(UGHwnd, child_buffer_2, 255);//则开始获取窗口的类名

SetWindowText(UGHwnd, "SB");

//char msg[500];
//sprintf(msg, "父窗口的标题名称:%s,父窗口的类名%s\n", child_buffer_1, child_buffer_2);
//UF_UI_write_listing_window(msg);

EnumChildWindows(UGHwnd, (WNDENUMPROC)EnumChildProc, (LPARAM)NULL); //枚举子窗口

UF_terminate();

}

//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
// Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
try
{
// Create NXOpen C++ class instance
MyClass *theMyClass;
theMyClass = new MyClass();
theMyClass->do_it();
delete theMyClass;
}
catch (const NXException& e1)
{
UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
}
catch (const exception& e2)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
}
catch (...)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
}
}


//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}


阿飞
2021年9月6日


演示

NX二次开发-通过获取窗口句柄方式来设置类型过滤器EnumChildWindows_类名

打印所有子窗口句柄

NX二次开发-通过获取窗口句柄方式来设置类型过滤器EnumChildWindows_类名_02

 

阿飞

2021年9月6日