暑期实习16之字符串的显示

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 19:37   447   0

这是我第一次来到大连,来到这座美丽的城市,来到东软软件园。东软给我的第一印象是环境很美,很适合居住。第一天到时,由于坐了一天车感觉很疲惫就没有参观这个园区。第二天领队老师带领我们参观了这座花园,感觉这的很美。我们来这不是为了玩是为了学习的,当天下午分好教室我们就开始了第一堂课———学会沟通。沟通时生活必不可少的一部分,老师给我们分析了沟通的重要性。第三天我们正式开始了C++的实训,第一堂课老师教我们用MFC的单文档输出字符串。在窗口上输出字符串有很多种方法,其

//CMy123View类的声明
#if !defined(AFX_123VIEW_H__131858DC_27C4_4865_A54C_4F1007802FF6__INCLUDED_)
#define AFX_123VIEW_H__131858DC_27C4_4865_A54C_4F1007802FF6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMy123View : public CView
{
protected: // create from serialization only
 CMy123View();
 DECLARE_DYNCREATE(CMy123View)

// Attributes
public:
 CMy123Doc* GetDocument();

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CMy123View)
 public:
 virtual void OnDraw(CDC* pDC);  // overridden to draw this view
 virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
 protected:
 virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
 virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
 virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMy123View();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
 //{{AFX_MSG(CMy123View)
  // NOTE - the ClassWizard will add and remove member functions here.
  //    DO NOT EDIT what you see in these blocks of generated code !
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in 123View.cpp
inline CMy123Doc* CMy123View::GetDocument()
   { return (CMy123Doc*)m_pDocument; }
#endif

//类的成员函数实现
#include "stdafx.h"
#include "369.h"

#include "369Doc.h"
#include "369View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy369View

IMPLEMENT_DYNCREATE(CMy369View, CView)

BEGIN_MESSAGE_MAP(CMy369View, CView)
 //{{AFX_MSG_MAP(CMy369View)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  //    DO NOT EDIT what you see in these blocks of generated code!
 //}}AFX_MSG_MAP
 // Standard printing commands
 ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy369View construction/destruction

CMy369View::CMy369View():str("Hello word!")
{
 // TODO: add construction code here

}

CMy369View::~CMy369View()
{
}

BOOL CMy369View::PreCreateWindow(CREATESTRUCT& cs)
{
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs

 return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy369View drawing

void CMy369View::OnDraw(CDC* pDC)
{
 CMy369Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 pDC->TextOut(20,20,str);
}

/////////////////////////////////////////////////////////////////////////////
// CMy369View printing

BOOL CMy369View::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
 return DoPreparePrinting(pInfo);
}

void CMy369View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add extra initialization before printing
}

void CMy369View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy369View diagnostics

#ifdef _DEBUG
void CMy369View::AssertValid() const
{
 CView::AssertValid();
}

void CMy369View::Dump(CDumpContext& dc) const
{
 CView::Dump(dc);
}

CMy369Doc* CMy369View::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy369Doc)));
 return (CMy369Doc*)m_pDocument;
}
#endif //_DEBUG

一就是直接在视图类中直接编代码:
 
其二是在框架类中定义字符串,正视图中调用:
//CMainFrame中的字符串声明
#if !defined(AFX_MAINFRM_H__E28014E2_1C9F_4928_A99E_B804982DC78E__INCLUDED_)
#define AFX_MAINFRM_H__E28014E2_1C9F_4928_A99E_B804982DC78E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMainFrame : public CFrameWnd
{
 
protected: // create from serialization only
 CMainFrame();
 DECLARE_DYNCREATE(CMainFrame)

// Attributes
public:
CString str;
// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CMainFrame)
 virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMainFrame();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
 CStatusBar  m_wndStatusBar;
 CToolBar    m_wndToolBar;

// Generated message map functions
protected:
 //{{AFX_MSG(CMainFrame)
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  // NOTE - the ClassWizard will add and remove member functions here.
  //    DO NOT EDIT what you see in these blocks of generated code!
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
//字符串初始化
#include "stdafx.h"
#include "123.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 //{{AFX_MSG_MAP(CMainFrame)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  //    DO NOT EDIT what you see in these blocks of generated code !
 ON_WM_CREATE()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
 ID_SEPARATOR,           // status line indicator
 ID_INDICATOR_CAPS,
 ID_INDICATOR_NUM,
 ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame():str("hello")
{
 
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
 {
  TRACE0("Failed to create toolbar\n");
  return -1;      // fail to create
 }

 if (!m_wndStatusBar.Create(this) ||
  !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
 {
  TRACE0("Failed to create status bar\n");
  return -1;      // fail to create
 }

 // TODO: Delete these three lines if you don't want the toolbar to
 //  be dockable
 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
 EnableDocking(CBRS_ALIGN_ANY);
 DockControlBar(&m_wndToolBar);

 return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
 if( !CFrameWnd::PreCreateWindow(cs) )
  return FALSE;
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs

 return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
 CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
 CFrameWnd::Dump(dc);
}
//视图中的调用
// 123View.cpp : implementation of the CMy123View class
//

#include "stdafx.h"
#include "123.h"

#include "123Doc.h"
#include "123View.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy123View

IMPLEMENT_DYNCREATE(CMy123View, CView)

BEGIN_MESSAGE_MAP(CMy123View, CView)
 //{{AFX_MSG_MAP(CMy123View)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  //    DO NOT EDIT what you see in these blocks of generated code!
 //}}AFX_MSG_MAP
 // Standard printing commands
 ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy123View construction/destruction

CMy123View::CMy123View()
{
 // TODO: add construction code here

}

CMy123View::~CMy123View()
{
}

BOOL CMy123View::PreCreateWindow(CREATESTRUCT& cs)
{
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs

 return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy123View drawing

void CMy123View::OnDraw(CDC* pDC)
{
 CMy123Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
    CWnd*pw=::AfxGetApp()->m_pMainWnd;
  CMainFrame *pp=(CMainFrame *)pw;
  pDC->TextOut(10,10,pp->str);

}

/////////////////////////////////////////////////////////////////////////////
// CMy123View printing

BOOL CMy123View::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
 return DoPreparePrinting(pInfo);
}

void CMy123View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add extra initialization before printing
}

void CMy123View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy123View diagnostics

#ifdef _DEBUG
void CMy123View::AssertValid() const
{
 CView::AssertValid();
}

void CMy123View::Dump(CDumpContext& dc) const
{
 CView::Dump(dc);
}

CMy123Doc* CMy123View::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy123Doc)));
 return (CMy123Doc*)m_pDocument;
}
#endif //_DEBUG

其三是在文档类中定义字符串,视图中实现,方法同上。

接下来是在文档中定义字符串,单机左键实现:视图类中调用如下:

// 456View.cpp : implementation of the CMy456View class
//

#include "stdafx.h"
#include "456.h"

#include "456Doc.h"
#include "456View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy456View

IMPLEMENT_DYNCREATE(CMy456View, CView)

BEGIN_MESSAGE_MAP(CMy456View, CView)
 //{{AFX_MSG_MAP(CMy456View)
 ON_WM_LBUTTONDOWN()
 //}}AFX_MSG_MAP
 // Standard printing commands
 ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy456View construction/destruction

CMy456View::CMy456View()
{
 // TODO: add construction code here

}

CMy456View::~CMy456View()
{
}

BOOL CMy456View::PreCreateWindow(CREATESTRUCT& cs)
{
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs

 return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy456View drawing

void CMy456View::OnDraw(CDC* pDC)
{
 CMy456Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMy456View printing

BOOL CMy456View::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
 return DoPreparePrinting(pInfo);
}

void CMy456View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add extra initialization before printing
}

void CMy456View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy456View diagnostics

#ifdef _DEBUG
void CMy456View::AssertValid() const
{
 CView::AssertValid();
}

void CMy456View::Dump(CDumpContext& dc) const
{
 CView::Dump(dc);
}

CMy456Doc* CMy456View::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy456Doc)));
 return (CMy456Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy456View message handlers

void CMy456View::OnLButtonDown(UINT nFlags, CPoint point) 
{
 // TODO: Add your message handler code here and/or call default
 CMy456Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 CDC *pDC=this->GetDC();
 pDC->TextOut(10,10,pDoc->str);
 this->ReleaseDC(pDC);

 CView::OnLButtonDown(nFlags, point);
}

结果如下:


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP