在ActiveX控件中引入窗體技術(shù)
 一、引入Dialog技術(shù) 
---- 下面介紹在制作ActiveX控件時引入有模式對話框技術(shù),制作步驟如下: 
創(chuàng)建一新的MFC ActiveX ControlWizard項目,取名為Hello,其他用缺省選項;
在ResourceView頁中新增一對話框資源,命名為IDD_HELLODIALOG,可以在對話框上放自己的控件;
為對話框資源IDD_HELLODIALOG創(chuàng)建新類CHelloDialog,從CDialog繼承;
確認(rèn)在HelloCtrl.h中已加入語句#include "HelloDialog.h",為CHelloCtrl類添加成員變量CHelloDialog m_helloDialog;
用ClassWizard在Automation頁中為CHelloCtrl添加一方法void DoHello(),外部名亦為DoHello; 
void CHelloCtrl::DoHello() 
{
// 顯示對話框
m_helloDialog.DoModal();
}
---- 可以用ActiveX Control Test Container測試Hello Control的DoHello方法。
---- 下面介紹在制作ActiveX控件時引入無模式對話框技術(shù),制作步驟如下:
在上面工作的基礎(chǔ)上,用ClassWizard為CHelloCtrl添加WM_CREATE的處理函數(shù)OnCreate,在此創(chuàng)建無模式對話框;
修改DoHello代碼,在此顯示對話框; 
int CHelloCtrl::OnCreate
(LPCREATESTRUCT lpCreateStruct) 
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
 return -1;
 
// 創(chuàng)建對話框
m_helloDialog.Create(IDD_HELLODIALOG);
return 0;
}
void CHelloCtrl::DoHello()
{
// 顯示對話框
m_helloDialog.ShowWindow(SW_SHOW);
}
---- 下面介紹制作以對話框作為界面的ActiveX控件技術(shù),制作步驟如下:
在上面工作的基礎(chǔ)上,設(shè)置對話框資源IDD_HELLODIALOG屬性的Style頁為Style:Child、Border:Dialog Frame、Title Bar:unchecked;設(shè)置More Style頁為Visible:checked;Control:checked;設(shè)置Extended Styles頁為Static Edge:checked;
在CHelloCtrl::OnCreate中寫入m_helloDialog.Create(IDD_HELLODIALOG,this)語句;
在CHelloCtrl::OnDraw中寫入m_helloDialog.MoveWindow(rcBounds,TRUE); 
int CHelloCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
 return -1;
 
// 創(chuàng)建對話框
m_helloDialog.Create(IDD_HELLODIALOG,this);
 
return 0;
}
void CHelloCtrl::OnDraw(CDC* pdc, const
 CRect& rcBounds, const CRect& rcInvalid)
{
// 定位Hello對話框
m_helloDialog.MoveWindow(rcBounds,TRUE);
}
---- 二、引入FormView技術(shù)
---- 下面介紹在制作ActiveX控件時引入FormView技術(shù),制作步驟如下:
在上面工作的基礎(chǔ)上,在ResourceView頁中新增一對話框資源,命名為IDD_HELLOFORMVIEW,可以在對話框上放自己的控件;
設(shè)置對話框資源IDD_HELLODIALOG屬性的Style頁為Style:Child、Border:Dialog Frame、Title Bar:unchecked;設(shè)置More Style頁為Visible:checked;Control:checked;設(shè)置Extended Styles頁為Static Edge:checked;
為對話框資源IDD_HELLOFORMVIEW創(chuàng)建新類CHelloFormView,從CFormView繼承;
在HelloFormView.h中將CHelloFormView的構(gòu)造函數(shù)CHelloFormView()和析構(gòu)函數(shù)virtual ~CHelloFormView()從protected改為public;
在HelloFormView.h中對CHelloFormView類加入public friend class CHelloCtrl;
確認(rèn)在HelloCtrl.h中已加入語句#include "HelloFormView.h",為CHelloCtrl類添加成員變量CHelloFormView m_helloFormView;
修改CHelloCtrl::OnCreate函數(shù),在此創(chuàng)建m_helloFormView;
修改DoHello代碼,在此顯示FormView; 
int CHelloCtrl::OnCreate
(LPCREATESTRUCT lpCreateStruct) 
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
 return -1;
 
// 創(chuàng)建FormView
m_helloFormView.Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL);
return 0;
}
void CHelloCtrl::OnDraw(CDC* pdc, const
 CRect& rcBounds, const CRect& rcInvalid)
{
// 定位Hello對話框
m_helloFormView.MoveWindow(rcBounds,TRUE);
}
---- 三、引入Document/View結(jié)構(gòu)技術(shù)
---- 下面介紹在制作ActiveX控件時引入Document/View技術(shù),制作步驟如下:
在上面工作的基礎(chǔ)上,在Hello工程中用ClassWizard添加一新類CPrintFrame,取其父類為CFrameWnd;
在PrintFrame.h中將CPrintFrame的構(gòu)造函數(shù)CPrintFrame()和析構(gòu)函數(shù)virtual ~CPrintFrame()從protected改為public;
在Hello工程中用ClassWizard添加一新類CPrintView,取其父類為CView;
在PrintView.h中將CPrintView的構(gòu)造函數(shù)CPrintView()和析構(gòu)函數(shù)virtual ~CPrintView()從protected改為public;
在Hello工程中用ClassWizard添加一新類CPrintDoc,取其父類為CDocument;
在PrintDoc.h中將CPrintDoc的構(gòu)造函數(shù)CPrintDoc()和析構(gòu)函數(shù)virtual ~CPrintDoc()從protected改為public;
在Hello工程中用ClassWizard添加一新類CPrintThread,取其父類為CWinThread;
在HelloCtrl.h文件中為CHelloCtrl類添加成員變量CPrintThread* m_pPrintThread,確認(rèn)在HelloCtrl.h中已加入語句#include "PrintThread.h"; 
void CHelloCtrl::DoHello() 
{
// 創(chuàng)建打印線程
m_pPrintThread = (CPrintThread*)
AfxBeginThread(RUNTIME_CLASS(CPrintThread),
THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED, NULL);
m_pPrintThread- >ResumeThread();
}
在PrintThread.h中添加新成員變量 
CPrintDoc* m_pPrintDoc和CPrintFrame* m_pPrintFrame,
并在構(gòu)造函數(shù)和析構(gòu)函數(shù)中完成對它們的初始設(shè)置和清除,
確認(rèn)在PrintThread.h中已加入語句#include 
"PrintDoc.h"和#include "PrintFrame.h";
CPrintThread::CPrintThread()
{
m_pPrintDoc=NULL;
m_pPrintFrame=NULL;
}
CPrintThread::~CPrintThread()
{
if (m_pPrintDoc!=NULL)
 delete m_pPrintFrame;
if (m_pPrintFrame!=NULL)
 delete m_pPrintDoc;
}
在PrintThread.cpp的CPrintThread::InitInstance中,進(jìn)行創(chuàng)建窗體CPrintFrame,確認(rèn)在PrintThread.cpp中已加入語句#include "PrintFrame.h"; 
BOOL CPrintThread::InitInstance()
{
// 創(chuàng)建文檔/視圖框架
CPrintFrame* pFrame = new CPrintFrame;
m_pMainWnd = pFrame;
m_pPrintFrame=pFrame;
m_pPrintDoc=new CPrintDoc;
CCreateContext context;
context.m_pCurrentDoc = m_pPrintDoc;
context.m_pNewViewClass = RUNTIME_CLASS(CPrintView);
pFrame- >Create(NULL,"打印主窗體",
WS_OVERLAPPEDWINDOW,CRect(0,0,100,100),
NULL,NULL,0,&context);
pFrame-  >InitialUpdateFrame(m_pPrintDoc, TRUE);
return TRUE;
}
在PrintView.h的CPrintView中,添加成員函數(shù)CPrintDoc* GetDocument(),確認(rèn)在PrintView.h中已加入語句#include "PrintDoc.h"; 
CPrintDoc* CPrintView::GetDocument()
{
ASSERT(m_pDocument- >IsKindOf
(RUNTIME_CLASS(CPrintDoc)));
return (CPrintDoc*)m_pDocument;
}
---- 四、實現(xiàn)ActiveX打印預(yù)覽技術(shù)
---- 下面介紹利用上面的技術(shù)成果來實現(xiàn)ActiveX的打印預(yù)覽技術(shù),實現(xiàn)步驟如下:
在上面工作的基礎(chǔ)上,用ClassWizard對CPrintView類實現(xiàn)OnPreparePrinting函數(shù),如下: 
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo) 
{
// 準(zhǔn)備打印
return DoPreparePrinting(pInfo);
}
用ClassWizard在Automation頁中為CHelloCtrl添加一方法void DoPreview(),外部名亦為DoPreview; 
void CHelloCtrl::DoPreview() 
{
// 進(jìn)行打印預(yù)覽
::PostMessage(m_pPrintThread- >m_pPrintFrame- >
GetActiveView()- >m_hWnd,WM_USER_PREVIEW,0,0); 
}
在PrintView.h中添加#define WM_USER_PREVIEW WM_USER+10
在PrintView.cpp中的消息映射中添加ON_MESSAGE(WM_USER_PREVIEW, DoPreview),形成如下: 
BEGIN_MESSAGE_MAP(CPrintView, CView)
ON_MESSAGE(WM_USER_PREVIEW, DoPreview)
//{{AFX_MSG_MAP(CPrintView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
為類CPrintView添加成員函數(shù)LRESULT DoPreview(WPARAM wParam, LPARAM lParam)
實現(xiàn)CPrintView::DoPreview如下: 
LRESULT CPrintView::DoPreview
(WPARAM wParam, LPARAM lParam)
{
// 進(jìn)入打印預(yù)覽
OnFilePrintPreview();
return 0;
}
為CPrintView添加public成員變量COleControl* m_pControlPreview,并初始化如下: 
CPrintView::CPrintView()
{
m_pControlPreview=NULL; // 
初始化要預(yù)覽的ActiveX控件類為空
}
在CPrintView::OnDraw中對控件內(nèi)容進(jìn)行顯示 
void CPrintView::OnDraw(CDC* pDC)
{
if (m_pControlPreview==NULL)
 pDC->TextOut(0,0,"No Preview View");
else {
CRect controlRect;
m_pControlPreview- >GetClientRect(&controlRect);
CRect previewRect(0,0,controlRect.
Width(),controlRect.Height());
 m_pControlPreview- >OnDraw
(pDC,controlRect,controlRect); 
}
 }
用ClassWizard在Automation頁中為CHelloCtrl添加一方法void SetPreviewControl(),外部名亦為SetPreviewControl,對其實現(xiàn)如下: 
void CHelloCtrl::SetPreviewControl() 
{
// 設(shè)置要預(yù)覽的View
CView* pView=m_pPrintThread- >
m_pPrintFrame- >GetActiveView();
CPrintView* pPrintView=(CPrintView*)pView;
pPrintView- >m_pControlPreview=this;
}
---- 在ActiveX Control Test Container測試,激活方法次序為DoHello、SetPreviewControl、DoPreview。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=321854

 
                         
                                
 
                                 在ActiveX控件中引入窗體技術(shù)
 在ActiveX控件中引入窗體技術(shù)




 
                        
