// NotepadView.cpp : implementation of the CNotepadView class
//
#include "stdafx.h"
#include "Notepad.h"
#include "NotepadDoc.h"
#include "NotepadView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNotepadView
IMPLEMENT_DYNCREATE(CNotepadView, CEditView)
BEGIN_MESSAGE_MAP(CNotepadView, CEditView)
//{{AFX_MSG_MAP(CNotepadView)
ON_COMMAND(IDC_AUTORETURN, OnAutoreturn)
ON_UPDATE_COMMAND_UI(IDC_AUTORETURN, OnUpdateAutoreturn)
ON_COMMAND(IDC_FONT, OnFont)
ON_UPDATE_COMMAND_UI(IDC_FONT, OnUpdateFont)
ON_COMMAND(ID_EDIT_SCAN_TEXT, OnEditScanText)
ON_UPDATE_COMMAND_UI(ID_EDIT_SCAN_TEXT, OnUpdateEditScanText)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNotepadView construction/destruction
CNotepadView::CNotepadView()
{
// TODO: add construction code here
LOGFONT logFont;
memset(&logFont,0,sizeof(LOGFONT));
// strcpy(szTextInsert,"\0");
logFont.lfHeight=-28;
logFont.lfWidth = 0;
logFont.lfStrikeOut=FALSE;
logFont.lfWeight=400;
logFont.lfCharSet=134;
strcpy(logFont.lfFaceName,_T("仿宋_GB2312"));
pFont=new CFont;
pFont->CreateFontIndirect(&logFont);
}
CNotepadView::~CNotepadView()
{
delete pFont;
}
BOOL CNotepadView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CNotepadView drawing
void CNotepadView::OnDraw(CDC* pDC)
{
CNotepadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
COLORREF OldColor;
CFont* pOldFont;
// CSize sTextSize;
// int iText_Length, iIndex,iWord_Width,MaxCx,MaxCy;
OldColor=pDC->SetTextColor(RGB(0,0,0));
pOldFont=(CFont*) pDC->SelectObject(pFont);//设置正文字体与颜色
pDC->SetTextColor(OldColor);
pDC->SelectObject(pOldFont);////恢复原来的字体与颜色
}
/////////////////////////////////////////////////////////////////////////////
// CNotepadView printing
BOOL CNotepadView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default CEditView preparation
return CEditView::OnPreparePrinting(pInfo);
}
void CNotepadView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView begin printing.
CEditView::OnBeginPrinting(pDC, pInfo);
}
void CNotepadView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView end printing
CEditView::OnEndPrinting(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CNotepadView diagnostics
#ifdef _DEBUG
void CNotepadView::AssertValid() const
{
CEditView::AssertValid();
}
void CNotepadView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CNotepadDoc* CNotepadView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNotepadDoc)));
return (CNotepadDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNotepadView message handlers
void CNotepadView::OnAutoreturn()
{
// TODO: Add your command handler code here
//自动换行实现
bChk=!bChk;
if(!bChk)
{
ShowScrollBar(SB_HORZ,true);
}
else
{
ShowScrollBar(SB_HORZ,false);
}
}
void CNotepadView::OnUpdateAutoreturn(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(bChk);
}
void CNotepadView::OnFont()
{
// TODO: Add your command handler code here
LOGFONT logFont;
memset(&logFont,0,sizeof(LOGFONT));
pFont->GetLogFont( &logFont );
CFontDialog FontDlg( &logFont );
if(FontDlg.DoModal()==IDOK)
{
FontDlg.GetCurrentFont(&logFont);
delete pFont;
pFont= new CFont;
pFont->CreateFontIndirect(&logFont);
}
Invalidate();
}
void CNotepadView::OnUpdateFont(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CNotepadView::OnEditScanText()
{
// TODO: Add your command handler code here
char szFileFilter[]=
"Source files(*.c)|*.c|"
"Source files(*.asm)|*.asm|"
"Text files(*.txt)|*.txt|"
"All files(*.*)|*.*||";
CFileDialog FileDlg(TRUE,"","",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST,
szFileFilter,this);
FileDlg.m_ofn.lpstrTitle=_T("打开");
if(FileDlg.DoModal()!=IDOK)
return;
// CHAR buf[1024]; // 读缓冲
// UINT uFileLength;
LOGFONT logFont;
memset(&logFont,0,sizeof(LOGFONT));
AfxString.Empty();
TextInsert.Empty();
strcpy(szTextInsert,"\0");
CStdioFile File;
if(!File.Open(FileDlg.GetPathName(),CFile::modeRead|CFile::typeText))
{
File.Abort();
AfxMessageBox("文本文件打不开");
return ;
}
CString str;
POSITION m_Pos;
CWordElement* m_pWordElement;
m_Pos = m_WordList.GetHeadPosition();
CString TempString;
//获取编辑正文
GetWindowText(str);
while(m_Pos!=NULL)
{
m_pWordElement = ( CWordElement* ) m_WordList.GetNext(m_Pos);
TempString=m_pWordElement->Word;
str+=TempString;
}
SetWindowText(str);
}
void CNotepadView::OnUpdateEditScanText(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CNotepadView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
dc.SelectObject(pFont);
dc.SetTextColor(RGB(0,0,0));
dc.SetBkColor(RGB(255,255,255));
// Do not call CEditView::OnPaint() for painting messages
}
int CNotepadView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEditView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
void CNotepadView::OnSize(UINT nType, int cx, int cy)
{
CEditView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
}
void CNotepadView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CEditView::OnVScroll(nSBCode, nPos, pScrollBar);
}
大家帮我看一下,字体选择对话框可以出来,但是选择以后窗口里的字没有跟看变化,是哪里漏了?