引入Dialog技術
下面介紹在制作ActiveX控件時引入有模式對話框技術,制作步驟如下:
1.創建一新的MFC ActiveX ControlWizard項目,取名為Hello,其他用缺省選項;
2.在ResourceView頁中新增一對話框資源,命名為IDD_HELLODIALOG,可以在對話
框上放自己的控件;
3.為對話框資源IDD_HELLODIALOG創建新類CHelloDialog,從CDialog繼承;
4.確認在HelloCtrl.h中已加入語句#include “HelloDialog.h",為CHelloCtrl類添加成員
變量CHelloDialog m_helloDialog;
5.用ClassWizard在Automation頁中為CHelloCtrl添加一方法void DoHello(),外部名亦為
DoHello;
void CHelloCtrl::DoHello()
{
// 顯示對話框
m_helloDialog.DoModal();
}
可以用ActiveX Control Test Container測試Hello Control的 DoHello方
法。
下面介紹在制作ActiveX控件時引入無模式對話框技術,
制作步驟如下:
1.在上面工作的基礎上,用ClassWizard為CHelloCtrl添加WM_CREATE的處理函數
OnCreate,在此創建無模式對話框;
2.修改DoHello代碼,在此顯示對話框;
int CHelloCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// 創建對話框
m_helloDialog.Create(IDD_HELLODIALOG);
return 0;
}
void CHelloCtrl::DoHello()
{
// 顯示對話框
m_helloDialog.ShowWindow(SW_SHOW);
}
下面介紹制作以對話框作為界面的ActiveX控件技術,制
作步驟如下:
1.在上面工作的基礎上,設置對話框資源IDD_HELLODIALOG屬性的Style頁為Style:Child、Border:Dialog
Frame、Title Bar:unchecked;設置MoreStyle頁為Visible: checked;Control:checked;設置Extended
Styles頁為Static Edge:checked;
2.在CHelloCtrl::OnCreate中寫入
m_helloDialog.Create(IDD_HELLODIALOGthis)語句;
3.在CHelloCtrl::OnDraw中寫入m_helloDialog.MoveWindow(rcBounds,TRUE);
int CHelloCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// 創建對話框
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技術
下面介紹在制作ActiveX控件時引入FormView技術, 制作步驟如下:
1.在上面工作的基礎上,在ResourceView頁中新增一對話框資源,命名為IDD_HELLOFORMVIEW,可以在對話框上放自己的控件;
2.設置對話框資源IDD_HELLODIALOG屬性的Style頁為Style:Child、Border:
Dialog Frame、Title Bar:unchecked;設置More Style頁為Visible:
checked;Control: checked;設置Extended Styles頁為Static Edge:
checked;
3.為對話框資源IDD_HELLOFORMVIEW創建新類CHelloFormView,從CFormView繼承;
4.在HelloFormView.h中將CHelloFormView的構造函數CHelloFormView()和析構函數
virtual ~CHelloFormView()從protected改為public;
5.在HelloFormView.h中對CHelloFormView類加入public friend classCHelloCtrl;
6.確認在HelloCtrl.h中已加入語句#include “HelloFormView.h",為CHelloCtrl類添加
成員變量CHelloFormView m_helloFormView;
7.修改CHelloCtrl::OnCreate函數,在此創建m_helloFormView;
8.修改DoHello代碼,在此顯示FormView;
int CHelloCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// 創建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結構技術
下面介紹在制作ActiveX控件時引入Document/View技
術,制作步驟如下:
1.在上面工作的基礎上,在Hello工程中用ClassWizard添加一新類CPrintFrame,取
其父類為CFrameWnd;
2.在PrintFrame.h中將CPrintFrame的構造函數CPrintFrame()和析構函數virtual
~ CPrintFrame()從protected改為public;
3.在Hello工程中用ClassWizard添加一新類CPrintView,取其父類為CView;
4.在PrintView.h中將CPrintView的構造函數CPrintView()和析構函數virtual~
CPrintView()從protected改為public;
5.在Hello工程中用ClassWizard添加一新類CPrintDoc,取其父類為CDocument;
6.在PrintDoc.h中將CPrintDoc的構造函數CPrintDoc()和析構函數virtual~CPrintDoc()
從protected改為public;
7.在Hello工程中用ClassWizard添加一新類CPrintThread,取其父類為CWinThread;
8.在HelloCtrl.h文件中為CHelloCtrl類添加成員變量CPrintThread*m_pPrintThread,
確認在HelloCtrl.h中已加入語句#include“PrintThread.h";
void CHelloCtrl::DoHello()
{
// 創建打印線程
m_pPrintThread = (CPrintThread*)AfxBeginThread
(RUNTIME_CLASS(CPrintThread),
THREAD_PRIORITY_NORMAL,
CREATE_SUSPENDED, NULL);
m_pPrintThread->ResumeThread();
}
9. 在PrintThread.h中添加新成員變量 CPrint
Doc*m_pPrintDoc和CPrintFrame* m_pPrintFrame,并
在構造函數和析構函數中完成對它們的初始設置和清除,確認在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;
}
10.在PrintThread.cpp的CPrintThread::InitInstance中,進行 創建窗體CPrintFrame,確認在PrintThread.cpp中已加入語句#include
“PrintFrame.h";
BOOL CPrintThread::InitInstance()
{
// 創建文檔/視圖框架
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;
}
11. 在PrintView.h的CPrintView中,添加成員函數 CPrintDoc*
GetDocument(),確認在PrintView.h中已加入語句#include “PrintDoc.h";
CPrintDoc* CPrintView::GetDocument()
{
ASSERT(m_pDocument->IsKindOf
(RUNTIME_CLASS(CPrintDoc)));
return (CPrintDoc*)m_pDocument;
}
實現ActiveX打印預覽技術
下面介紹利用上面的技術成果來實現ActiveX的打印預
覽技術,實現步驟如下:
1. 在上面工作的基礎上,用ClassWizard對CPrintView 類實現
OnPreparePrinting函數,如下:
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 準備打印
return DoPreparePrinting(pInfo);
}
2. 用ClassWizard在Automation頁中為CHelloCtrl添加一 方法void
DoPreview(),外部名亦為DoPreview;
void CHelloCtrl::DoPreview()
{
// 進行打印預覽
::PostMessage(m_pPrintThread->
m_pPrintFrame->GetActiveView()->
m_hWnd,WM_USER_PREVIEW,0,0);
}
3.在PrintView.h中添加#define WM_USER_PREVIEW
WM_USER+10;
4. 在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()
5. 為類CPrintView添加成員函數LRESULT DoPreview(WPARAM wParam,LPARAM
lParam);
6. 實現CPrintView::DoPreview如下:
LRESULT CPrintView::DoPreview
(WPARAM wParam, LPARAM lParam)
{
// 進入打印預覽
OnFilePrintPreview();
return 0;
}
7. 為CPrintView添加public成員變量COleControl* m_pControlPreview,
并初始化如下:
CPrintView::CPrintView()
{
m_pControlPreview=NULL;
// 初始化要預覽的ActiveX控件類為空
}
8. 在CPrintView::OnDraw中對控件內容進行顯示;
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); }
}
9. 用ClassWizard在Automation頁中為CHelloCtrl添加一 方法voidSetPreviewControl(),外部名亦為SetPreviewControl,對其實現如下:
void CHelloCtrl::SetPreviewControl()
{
// 設置要預覽的View
CView* pView=m_pPrintThread->
m_pPrintFrame->GetActiveView();
CPrintView* pPrintView=(CPrintView*)pView;
pPrintView->m_pControlPreview=this;
}
在ActiveX Control Test Container測試,激活方法次序為 DoHello、SetPreviewControl、DoPreview。