MFC 输出文字
#include "stdafx.h" #include "TestText.h" #include "TestTextDoc.h" #include "TestTextView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTestTextView IMPLEMENT_DYNCREATE(CTestTextView, CView) BEGIN_MESSAGE_MAP(CTestTextView, CView) //{{AFX_MSG_MAP(CTestTextView) ON_WM_CREATE() //}}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() ///////////////////////////////////////////////////////////////////////////// // CTestTextView construction/destruction CTestTextView::CTestTextView() { // TODO: add construction code here } CTestTextView::~CTestTextView() { } BOOL CTestTextView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CTestTextView drawing void CTestTextView::OnDraw(CDC* pDC) { CTestTextDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); /*CString str("这是我的测试程序");*///用字符串本身构造对象 /************************************************************************/ /*因为CSring类重载了操作符,所以也可以用=,+等.. 所以Csring str; str="这是我的测试程序";也是可以的 /************************************************************************/ /* pDC->TextOut(200,200,str);*/ /************************************************************************/ /* GetTextExtent函数的返回值是个CSzie对象,包含两个数据成员CX,CY。标识了字符串的高度和宽度。 /************************************************************************/ /* CSize size = pDC->GetTextExtent(str);*/ /************************************************************************/ /* Rectangle的参数为左上坐标x,y,右下坐标x,y beginpath和endpath中包含的是路径层,路径层覆盖在VIEW窗口之上 SelectClipPath函数是选择剪贴区域,后面的参数设置的是覆盖模式。 /************************************************************************/ // pDC->BeginPath(); // pDC->Rectangle(200,200,200+size.cx,200+size.cy); // pDC->EndPath(); // pDC->SelectClipPath(RGN_DIFF); // for (int i =0;i<=500;i=i+10) // { // pDC->MoveTo(0,i); // pDC->LineTo(500,i); // pDC->MoveTo(i,0); // pDC->LineTo(i,500); // } // } ///////////////////////////////////////////////////////////////////////////// // CTestTextView printing BOOL CTestTextView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CTestTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CTestTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CTestTextView diagnostics #ifdef _DEBUG void CTestTextView::AssertValid() const { CView::AssertValid(); } void CTestTextView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CTestTextDoc* CTestTextView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestTextDoc))); return (CTestTextDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CTestTextView message handlers int CTestTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; /************************************************************************/ /* 使用CClientDC 定义一个DC以调用dc.TextMetrics,得到字体大小 由于TextMetrcis需要一个指向TEXTMETRIC对象的指针,所以定义对象tm CreateSolidCaret里的参数是平均宽度,和高度。(字母的宽度是不一样的 比如(W,i)所以只有平均宽度。 /************************************************************************/ // CClientDC dc(this); // TEXTMETRIC tm; // dc.GetTextMetrics(&tm); // CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); // ShowCaret(); /************************************************************************/ /* 使用位图资源作为插入符 */ /************************************************************************/ // CClientDC dc(this); // static CBitmap bmp; // bmp.LoadBitmap(IDB_BITMAP1); // CreateCaret(&bmp); // ShowCaret(); return 0; }