建议结合作者第一篇博客观看,本片博客是对于第一篇博客第三种方法的补充。
以加工模块创建刀具窗口为例,打开uifw侦察,发现并没有类名
直接上代码:
在你需要调用UG对话框的地方加入以下代码,参数ugButtonName参考第一篇博客方法一,
bool callUGButton(string ugButtonName)
{
int button_id = 0;
HWND ugWndH = NULL;
ugWndH = (HWND)UF_UI_get_default_parent();
UF_CALL(UF_MB_ask_button_id(ugButtonName.data(), &button_id));
if (button_id)
{
//BOOL result = SendMessage(ugWndH, WM_COMMAND, button_id, 0);
BOOL result = PostMessage(ugWndH, WM_COMMAND, button_id, 0);
return result;
}
else
{
return false;
}
}
注意使用PostMessage发送消息;
这时运行程序,会弹出对话框,但是程序无法识别对话框什么时候弹出,什么时候关闭;再添加代码:
添加定时器:
HWND ugWndH = NULL;
ugWndH = (HWND)UF_UI_get_default_parent();
SetTimer(ugWndH, 1, 200, TimerProc);
定时器回调函数:
HWND g_hCreateTool = NULL;
HWND g_hMillCutParam = NULL;
int g_nLastMillCount = 0;
int g_nIsSearchCreateTool = 0;
int g_nSearchCount = 0;
VOID CALLBACK TimerProc(HWND hWnd, UINT msg, UINT_PTR id, DWORD time)
{
if (g_hCreateTool == NULL)
{
g_hCreateTool = FindWindowW(NULL, L"创建刀具");
if (g_hCreateTool && g_nIsSearchCreateTool == 0)
g_nIsSearchCreateTool = 1;
}
if (g_hCreateTool == NULL || g_nIsSearchCreateTool == 0)
return;
if (IsWindowVisible(g_hCreateTool))
return;
if (g_hMillCutParam == NULL)
{
g_nSearchCount++;
g_hMillCutParam = SearchWindow("NX_SURFACE_WND_DIALOG", "铣刀-5 参数");
//g_hMillCutParam = FindWindowW(L"NX_SURFACE_WND_DIALOG", L"铣刀-5 参数");
if (g_hMillCutParam == NULL)
return;
if (!IsWindowVisible(g_hMillCutParam))
{
//对话框关闭,执行你想要的动作;
}
if (g_nSearchCount >= 5 && g_hMillCutParam == NULL)
KillTimer(hWnd, 1);
}
}
注:发现问题或者有其他方案,欢迎评论指正