CAA中多次点击按钮只创建一个非模态窗口(全局,即当前系统下)

CAA 中只创建一个非模态对话框

而当多次点击按钮时,对于非模态对话框,默认是启动多个实例,本文的代码将实现在当前系统下只打开一个对话框,缺点就是,即使打开多个 CATIA,也只能创建一个窗口。
实现原理:通过windows api FindowWindow 函数查找对话框的标题,如果不存在,则创建,如果存在,则正常显示,并当前置顶。

创建一个全局类,将实现写在函数中,避免代码多次重用

在 DetectDialog.h 中构造该函数
static  CATBoolean DetectWindow(CATUnicodeString name);
并添加头文件
#include "CATUnicodeString.h"
#include "CATBoolean.h"
#include <string>
#include "windows.h"
#include <iostream>
#include <comutil.h>

在 DetectDialog.cpp 中添加
CATBoolean DetectDialog::DetectWindow(CATUnicodeString name)
{
    //将CATUnicodeString 转换为 wstring  类型
    const char*  temp_char = name.ConvertToChar();    //char*  temp_char = name.CastToCharPtr();CATString to char*
    std::string  temp_string= std::string(temp_char); //char*    to  string
    _bstr_t temp_str = temp_string.c_str();           //string   to  _bstr_t
    wchar_t* temp_wchar = (wchar_t*)temp_str;         //_bstr_t  to  wchar_t *
    std::wstring result = temp_wchar;                 //wcahr_t *  to  wstring

    LPCWSTR title = result.c_str();                   //wstring  to  LPCWSTER type
    HWND hWnd = ::FindWindow(NULL, title);
    if(hWnd != NULL) {
        std::cout<<temp_string<<"    已经有窗口在运行!\n";
        ::ShowWindow(hWnd, SW_SHOWNORMAL);
        ::SetForegroundWindow(hWnd);
        return FALSE;
    }
    else {
        std::cout<<temp_string<<"    没有窗口在运行,正在创建中···\n";
        return TRUE;
    }
}

在你想用该函数的cpp.h文件中添加该头文件,因为声明的是static静态函数,所以不需要实例化,直接用 DetectDialog::DetectWindow() 函数就可以。其中,括号内的数据类型必须是 CATUnicodeString 类型, 比如我的对话框实例化类是 pDiaog, 那么 可以用 CAA 的函数 GetTitle() 函数返回对话框的名字(CATUnicodeString 类型),即

pToolingConfModify=new ToolingConfModify(tempProductName);
if (DetectDialog::DetectWindow(pToolingConfModify->GetTitle())) 
{
    pToolingConfModify->Build();
    pToolingConfModify->SetVisibility(CATDlgShow);  
}
else
{
    delete pToolingConfModify;
    pToolingConfModify = NULL;  
}

猜你喜欢

转载自blog.csdn.net/pangzi1821/article/details/80314549
今日推荐