老UI嵌入MFC
在liang的帮助下,完成了老UI嵌入MFC的问题,后面增加了控件随UI对话框大小改变而改变,以下是主要的代码,录制了有视频,请联系QQ:3243729373
视频地址:
1,讲解嵌入MFC
链接:https://pan.baidu.com/s/1vttC9gJwtDhFusojEbajfQ 密码:
此处内容已隐藏,评论后刷新即可查看!
;
2,讲解mfc控件自适应对话框
链接:https://pan.baidu.com/s/1X0XVg7MZCyl8Xujhy2sVgg 密码:
此处内容已隐藏,评论后刷新即可查看!
;
D:\HB_MOULD_N\ugopen\7.5-64\UGOPEN libufun.lib;libugopenint.lib;libnxopencpp.lib;libnxopenuicpp.lib //////以下复制到头部 #include "Resource.h" #include "mfc_block.h"///这里有改为新建的类名 #include <uf.h> #include <uf_ui.h> #include <uf_part.h> mfc_block *dlg = NULL; ///这里有改为新建的类名 ////////////////////////////////////////////////////////////////////////////以下复制到尾部 extern void PrintErrorMessage( int errorCode )//输出错误信息 { if ( 0 != errorCode ) { char message[256]; UF_get_fail_message( errorCode, message ); UF_UI_set_status( message ); } } extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) { //返回的正确的模块状态,得到正确的句柄 AFX_MANAGE_STATE(AfxGetStaticModuleState()); int errorCode = UF_initialize();//UF初始化 int res = UF_PART_ask_display_part(); mfc_ui(); UF_terminate(); //UF终止化 PrintErrorMessage( errorCode ); //输出错误信息 } extern void ufusr_cleanup (void) //释放申请的内存 { if (dlg) { delete dlg; } return; } extern "C" DllExport int ufusr_ask_unload() { return (UF_UNLOAD_IMMEDIATELY); //return (UF_UNLOAD_SEL_DIALOG); } ///////////////////////////////////////////////////////////////// m_list.InsertColumn(0, _T("刀具名称"), LVCFMT_LEFT); m_list.SetColumnWidth(0, 80); m_list.InsertColumn(1, _T("直径"), LVCFMT_LEFT); m_list.SetColumnWidth(1, 50); m_list.InsertColumn(2, _T("下半径"), LVCFMT_LEFT); m_list.SetColumnWidth(2, 55); m_list.InsertColumn(3, _T("类型"), LVCFMT_LEFT); m_list.SetColumnWidth(3, 55); m_list.InsertColumn(4, _T("刃长"), LVCFMT_LEFT); m_list.SetColumnWidth(4, 50); m_list.InsertColumn(5, _T("长度"), LVCFMT_LEFT); m_list.SetColumnWidth(4, 50); for (int i=0;i<10;i++) { CString tempStr; tempStr.Format(_T("第%d把刀"),i+1); int row=m_list.InsertItem(0,tempStr); m_list.SetItemText(row,1,tempStr); m_list.SetItemText(row,2,tempStr); m_list.SetItemText(row,3,tempStr); m_list.SetItemText(row,4,tempStr); m_list.SetItemText(row,5,tempStr); } /////////////////////////////////////////////////////////////////// #include <uf.h> #include <uf_ui.h> #include "mfcDlg.h" HWND UIHwnd; mfcDlg*IDD1; //////////////////////////////////////////////////////////////////// //枚举子窗口回调函数原型 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); //则开始获取窗口的类名 if (strcmp(child_buffer_2, "#32770") == 0)//判断子窗口类名是否是#32770 { /* UF_UI_open_listing_window(); UF_UI_write_listing_window(child_buffer_2); UF_UI_write_listing_window("\n"); */ theUIMfc->UIHwnd = hwnd;//保存UI父句柄 //return FALSE; } } return TRUE; } void mfc_ui(){ try { theUIMfc = new UIMfc(); theUIMfc->UIHwnd=NULL; // The following method shows the dialog immediately theUIMfc->Show(); theUIMfc->Dispose(); delete theUIMfc; } catch (const NXOpen::NXException& ex) { // ---- Enter your exception handling code here ----- UIMfc::theUI->NXMessageBox()->Show("UI Styler", NXOpen::NXMessageBox::DialogTypeError, ex.Message()); } } if (theUIMfc->UIHwnd) { delete theUIMfc->UIHwnd; theUIMfc->UIHwnd=NULL; } UIHwnd = GetForegroundWindow(); //返回前台窗回的句柄,如果你调了UI或MFC界面那么会获得UI或MFC界面的窗口句柄 if (UIHwnd)//如果UI窗口句柄不为空 { //GetWindowText(UIHwnd, pub_data->parent_buffer_1, 255); //获取UI窗口的标题 //GetClassName(UIHwnd, pub_data->parent_buffer_2, 255); //获取UI窗口的类名 EnumChildWindows(UIHwnd, (WNDENUMPROC)EnumChildProc, (LPARAM)NULL); //枚举子窗口 } else { uc1601("提示!对话框初始化错误...", 1); } AFX_MANAGE_STATE(AfxGetStaticModuleState()); IDD1 = new mfcDlg(); CWnd *UG_pWnd = CWnd::FromHandle(UIHwnd);//句柄转换 IDD1->Create(IDD_DIALOG1,UG_pWnd); //创建一个非模态对话框 IDD1->SetParent(UG_pWnd); IDD1->ShowWindow(SW_SHOW); //显示非模态对话框 IDD1->UpdateWindow(); CPoint c_point; CRect rect[2]; int mfc_size[2];// ui_size[2]; GetWindowRect(IDD1->m_hWnd, rect[0]);//获取MFC尺寸 mfc_size[0] = rect[0].Width();//宽度 mfc_size[1] = rect[0].Height();//高度 GetWindowRect(UIHwnd, rect[1]);//获取ui尺寸 //c_point = rect[1].left;//ui左上角坐标 IDD1->MoveWindow(0,30, rect[0].Width(), rect[0].Height()); ///////////////////////////////////////////////////////////////// int dlgWidth,dlgHeight; KillTimer(UIHwnd,1); void Ui::setVisi() { if (changeSep1->Visibility()) { changeSep1->SetVisibility(false); } } void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT_PTR nTimerid,DWORD dwTime) { CRect rect[2]; int chaValue[2]; switch(nTimerid) { case 1: ///处理ID为1的定时器 GetWindowRect(theUi->UIHwnd, rect[1]);//获取ui尺寸 chaValue[0]=rect[1].Width()-theUi->dlgWidth; chaValue[1]=rect[1].Height()-theUi->dlgHeight; theUi->dlgWidth=rect[1].Width(); theUi->dlgHeight=rect[1].Height(); if (abs(chaValue[0])>0|| abs(chaValue[1])>0) { GetWindowRect(theUi->IDD1->m_hWnd, rect[0]);//获取MFC尺寸 theUi->IDD1->MoveWindow(0,30, rect[0].Width()+chaValue[0], rect[0].Height()+chaValue[1]); theUi->setVisi(); } break; case 2: ///处理ID为2的定时器 break; } } dlgWidth=rect[1].Width(); dlgHeight=rect[1].Height(); ::SetTimer(UIHwnd,1,100,(TIMERPROC)TimerProc); ///////////////////////////////////////////////////////////////// void ChangeSize(UINT nID, int x, int y); CRect m_rect; GetClientRect(&m_rect); void mfcDlg::ChangeSize(UINT nID, int x, int y) //nID为控件ID,x,y分别为对话框的当前长和宽 { CWnd *pWnd; pWnd=GetDlgItem(nID); if(pWnd!=NULL) //判断是否为空,因为在窗口创建的时候也会调用OnSize函数,但是此时各个控件还没有创建,Pwnd为空 { CRect rec; pWnd->GetWindowRect(&rec); //获取控件变化前的大小 ScreenToClient(&rec); //将控件大小装换位在对话框中的区域坐标 rec.left=rec.left; rec.top=rec.top; rec.bottom=rec.top+y; rec.right=rec.left+x; pWnd->MoveWindow(rec); //伸缩控件 } } void mfcDlg::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 if(nType!=SIZE_MINIMIZED ) //判断窗口是不是最小化了,因为窗口最小化之后 ,窗口的长和宽会变成0,当前一次变化的时就会出现除以0的错误操作 { ChangeSize(IDC_LIST1,cx,cy); //对每一个控件依次做调整 GetClientRect(&m_rect); //最后要更新对话框的大小,当做下一次变化的旧坐标; } }
转载请注明出处: 芝麻开花
本文的链接地址: http://www.vmould.cn/?post=170
上一篇
sqlite数据库使用方法
您可能对以下文章感兴趣
发表评论 取消回复