您好,登錄后才能下訂單哦!
在多文檔編程框架中,要顯示多個(gè)視圖,如何將視圖類封裝到DLL中?運(yùn)行時(shí)將其創(chuàng)建此視圖,并嵌入到到主框架中,以此實(shí)現(xiàn)模塊編程呢?步驟如下:
(1)創(chuàng)建一個(gè)MFC的擴(kuò)展DLL工程,在資源中添加一個(gè)FormView窗口,并創(chuàng)建相應(yīng)的類CDeviceManageView。添加導(dǎo)出函數(shù)如下:
在stdafx.h文件中定義全局變量 //全局變量 extern CWnd * g_pParentWnd ; extern BOOL g_bIsCloseView ; //在dllmain.cpp中聲明 CWnd * g_pParentWnd = NULL; BOOL g_bIsCloseView = FALSE; extern "C" AFX_EXT_API UINT Create(CRuntimeClass * * view,CWnd * p) { new CDynLinkLibrary(Scale3DCDeviceManageDLL); g_pParentWnd = p; *view = RUNTIME_CLASS(CScale3DCDeviceManageView); g_bIsCloseView = TRUE; return(0); } extern "C" AFX_EXT_API BOOL GetVisual() {//視圖是否可見 return g_bIsCloseView; }
(2)主框架中調(diào)用DLL函數(shù)創(chuàng)建視圖
HINSTANCE hDllInst = AfxLoadLibrary(CScale3DCGlobal::GetApplicationPath() + _T("\\DeviceManage.DLL")); if (hDllInst) { typedef UINT(*MYFUNC)(CRuntimeClass**,CWnd *parent); MYFUNC CreateViewUI = NULL; CreateViewUI = (MYFUNC)GetProcAddress(hDllInst, "Create"); if (CreateViewUI) { CRuntimeClass* pNewViewClass; CreateViewUI(&pNewViewClass,AfxGetMainWnd()); ASSERT(pNewViewClass); CCreateContext context; context.m_pNewViewClass = pNewViewClass; pChildFrame = new CScale3DCChildFrame;//測(cè)試工程中的子框架窗口 if (!pChildFrame->LoadFrame(32000,//與DLL中的資源ID一致 WS_OVERLAPPEDWINDOW | FWS_PREFIXTITLE, AfxGetMainWnd(), &context)) return; pChildFrame->SetWindowText(_T("通信連接")); pChildFrame->InitialUpdateFrame(NULL, TRUE); } //FreeLibrary(hDllInst); } }
編譯器相關(guān)設(shè)置,如下:
(2)設(shè)置視圖的標(biāo)簽頁標(biāo)題
#include <afxMDIChildWndEx.h> void CScale3DCScaleWeightView::OnInitialUpdate() { CFormView::OnInitialUpdate(); CMDIChildWndEx * pFrame = (CMDIChildWndEx*)this->GetParentFrame(); //CScale3DCChildFrame * pMidFrame = (CScale3DCChildFrame*)pFrame; pFrame->SetWindowText(_T("稱重?cái)?shù)據(jù)區(qū)")); // TODO: 在此添加專用代碼和/或調(diào)用基類 }
(3)設(shè)置視圖窗口的背景顏色
BOOL CScale3DCScaleWeightView::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值 CRect rt; GetClientRect(&rt); CBrush brush; brush.CreateSolidBrush(RGB(224, 241, 251)); CBrush * pOldBrush = pDC->SelectObject(&brush); pDC->FillRect(&rt, &brush); pDC->SelectObject(pOldBrush); return TRUE; //return CFormView::OnEraseBkgnd(pDC); }
(4)在FormView中添加消息處理如下:
void CScale3DCUserManageView::OnNcDestroy() { CFormView::OnNcDestroy(); g_bIsCloseView = FALSE; // TODO: 在此處添加消息處理程序代碼 }
(5)為DLL中的視圖窗口,設(shè)置唯一的ID(即相對(duì)于所有的DLL包含的視圖)。
(6)控件產(chǎn)生提示
.h文件中添加提示控件 CToolTipCtrl m_tip; void CScale3DCWeightView::OnInitialUpdate() { CFormView::OnInitialUpdate(); m_tip.Create(this); m_tip.AddTool(GetDlgItem(IDC_ADD_SCALE), _T("稱重界面")); } BOOL CScale3DCDeviceManageView::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加專用代碼和/或調(diào)用基類 if (pMsg->message == WM_MOUSEMOVE) m_Mytip.RelayEvent(pMsg); return CFormView::PreTranslateMessage(pMsg); }
(7)窗口靜態(tài)標(biāo)簽控件透明
HBRUSH CScale3DCWeightView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: 在此更改 DC 的任何特性 if (nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkMode(TRANSPARENT); //設(shè)備背景透明模式 return (HBRUSH)GetStockObject(NULL_BRUSH); } // TODO: 如果默認(rèn)的不是所需畫筆,則返回另一個(gè)畫筆 return hbr; }
(8)顯示滾動(dòng)條
SetScrollSizes(MM_TEXT, CSize(800, 170*4));//設(shè)置滾動(dòng)條范圍
(9)在子框架中給主框架發(fā)消息
void CScale3DCWeightView::OnNcDestroy() {//通知主視圖,關(guān)閉相關(guān)面板 g_bIsCloseView = FALSE; CMDIChildWndEx * pFrame = (CMDIChildWndEx*)this->GetParentFrame(); CWnd * pMainFrame = pFrame->GetParentFrame(); if(pMainFrame) ::SendMessage(pMainFrame->m_hWnd, WM_APP + 301, 0, 0); CFormView::OnNcDestroy();//放到最后一句 }
(10)CImage類加載DLL中的資源
VOID CScale3DCYiBiaoData::SetImage(INT id) { m_nImgID = id; HINSTANCE temp = AfxGetResourceHandle();//保存舊的句柄 //設(shè)置當(dāng)前DLL資源環(huán)境句柄 AfxSetResourceHandle(GetModuleHandle(_T("Scale3DCDeviceManage")));//DLL名稱 m_img.Destroy();//為了多次加載 m_img.LoadFromResource(AfxGetResourceHandle(), id); AfxSetResourceHandle(temp);//恢復(fù)原先句柄 }
從資源中加載字符串,則不必,直接LoadString(id)即可
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。