在对话框中生成视图

Posted on 2008-04-13 00:13  少林  阅读(731)  评论(0编辑  收藏  举报
 

1.       通过classward建立一个对话框,然后再添加两个类,CTestViewCTestDoc类,分别继承与CViewCDocument

2.       在对话框主程序里,添加两个变量,m_TestViewm_TestDoc类别分别是CTestView CTestDoc.

3.       在资源管理器中添加一个按钮,命名为 CreateView,生成相应的事件。并在该事件添加如下代码:

CWnd* pFrameWnd = this;

       pContext.m_pCurrentDoc = new CTestDoc;

       //pContext.m_pCurrentFrame = pFrameWnd;

       pContext.m_pNewViewClass = RUNTIME_CLASS(CTestView);

       m_TestView = (CTestView*)((CFrameWnd*)pFrameWnd)->CreateView(&pContext);

       ASSERT(m_TestView);

       m_TestView->ShowWindow(SW_NORMAL);

       CRect rect;

       GetWindowRect(rect);

       ScreenToClient(rect);

       m_TestView->MoveWindow(rect);

       CButton* button = (CButton*)GetDlgItem(IDOK);

       button->ShowWindow(SW_HIDE);

       CButton* button1 = (CButton*)GetDlgItem(IDCANCEL);

       button1->ShowWindow(SW_HIDE);

4.       CTestView里加上以下代码:

   void CTestView::OnDraw(CDC* pDC)

{

//     CDocument* pDoc = GetDocument();

       // TODO: add draw code here

 CDocument* pDoc = GetDocument();

    // TODO: add draw code here

 //pDC->TextOut(100,100,TEXT("EHEEKEKEKEKEK"));

 CFont font,*pOldFont;

 LOGFONT lp;

 lp.lfCharSet = DEFAULT_CHARSET;

 lp.lfHeight = 50;

 strcpy(lp.lfFaceName,"宋体");

 lp.lfStrikeOut = NULL;

 lp.lfItalic = NULL;

 lp.lfEscapement = 0;

 pOldFont = pDC->SelectObject(&font);

 pDC->SetBkMode(TRANSPARENT);

 pDC->SetTextColor(RGB(0,0,0));

 CRect rect(11,11,501,301);

 pDC->DrawText("Hello, this is a view in dialog",&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE);

 CRect r1(10,10,500,300);

 pDC->SetTextColor(RGB(255,0,0));

 pDC->DrawText("Hello, this is a view in dialog",&r1,DT_CENTER | DT_VCENTER | DT_SINGLELINE);

 pDC->SelectObject(pOldFont);

 font.DeleteObject();

}

大功告成。

Copyright © 2024 少林
Powered by .NET 9.0 on Kubernetes