⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sfedit.cpp

📁 TXT阅读器,自定义字体,字体颜色,背景颜色及字体大小.支持书签保存及关闭自己动保存
💻 CPP
字号:
// SFEdit.cpp : implementation file
//

#include "stdafx.h"
#include "ReaderSF.h"
#include "SFEdit.h"
#include "resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// SFEdit
LPARAM lparam;
SFEdit::SFEdit()
{
	
	BackColor=RGB(0,0,0);
	TextColor=RGB(255,255,255);
	
	strcpy(lpFileName,"LoveZ3.txt");
	memset(&ChFont ,0,sizeof(ChFont));
	memset(&TextLogFont ,0,sizeof(LOGFONT));
	memset(&ChCol,0,sizeof(ChCol));
	cbr.CreateSolidBrush (BackColor);
	bOpen=FALSE;

	
	
	
	
}

SFEdit::~SFEdit()
{

  SaveDefault();

	
//	MessageBox("BYE");
		
}


BEGIN_MESSAGE_MAP(SFEdit, CEdit)
ON_WM_CONTEXTMENU()
//ON_WM_CONTEXTMENU()
//	ON_WM_CONTEXTMENU()
//	ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(SFEdit)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_VSCROLL()
	ON_WM_CREATE()
	ON_WM_SHOWWINDOW()
	ON_WM_INITMENUPOPUP()
	ON_COMMAND(ID_LOADDEF, OnLoaddef)
	ON_WM_SETFOCUS()
	ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// SFEdit message handlers

BOOL SFEdit::OpenFile(BOOL nf,char *LoadName)
{
	
	OPENFILENAME     Ofn;   
	memset(&Ofn,0,sizeof(OPENFILENAME));
	
	
	
	Ofn.lStructSize   =   sizeof(OPENFILENAME);     
	Ofn.hwndOwner   =   this->m_hWnd ;     
	Ofn.lpstrFilter   =   "小说文件(*.txt)\0*.txt\0\0";     
	Ofn.lpstrFileTitle=txTitle;
	Ofn.nMaxFileTitle =MAX_PATH;
	Ofn.nFilterIndex=1;
	Ofn.lpstrFile=   lpFileName;     
	Ofn.nMaxFile   =   MAX_PATH;     
	
	Ofn.Flags   =   0x1800|OFN_OVERWRITEPROMPT;     
	
	Ofn.lpstrTitle   =   "打开科幻";     
	
	if(nf)
	{
		int ret=GetOpenFileName(&Ofn);  
		if(!ret)return FALSE;
	}
	else
	{
		strcpy(Ofn.lpstrFile ,LoadName);
		
	}
	HANDLE hf=CreateFile(Ofn.lpstrFile ,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hf)
	{
		MessageBox("小说打开失败");
		return FALSE;
	}
	DWORD fsize,rsize;
	fsize=SetFilePointer(hf,NULL,NULL,FILE_END);
	SetFilePointer(hf,NULL,NULL,FILE_BEGIN);
	char* fbuf=new char [fsize];
	if(!ReadFile(hf,fbuf,fsize,&rsize,NULL))
	{
		MessageBox("读取失败");
		CloseHandle(hf);
		return FALSE;
	}
	strcpy(lpFileName,Ofn.lpstrFile );
	
	
	fbuf[fsize]=0;
	SetWindowText (fbuf );
	//MessageBox(sfTitle );
	
	txTitle[lstrlen(txTitle)-4]='\0';
	HWND hp=this->GetParent ()->m_hWnd;
	if(hp)
	::SetWindowText( hp,txTitle);
	CloseHandle(hf);

	
	bOpen=TRUE;

	return TRUE;
	
	
	
	
}

BOOL SFEdit::SetTextFont()
{
	
	
	//memset(&ChFont,0,sizeof(CHOOSEFONTW));
	sfMark.CurPos =GetScrollPos(SB_VERT);
	ChFont.lStructSize =sizeof(ChFont);
	ChFont.hwndOwner =NULL;
	
	ChFont.rgbColors =TextColor;
	ChFont.nFontType =REGULAR_FONTTYPE;
	ChFont.Flags=CF_INITTOLOGFONTSTRUCT|CF_SCREENFONTS|CF_EFFECTS;
	
	//LOGFONT lgf;
	ChFont.lpLogFont =&TextLogFont;
	if(!ChooseFont(&ChFont))
	{

		this->SetCur (sfMark.CurPos );
		return FALSE;
	}
	CFont TextFont;
	TextFont.CreateFontIndirect (ChFont.lpLogFont );
	SetFont (&TextFont);
	
	TextColor=ChFont.rgbColors ;
//	TextSize=ChFont.iPointSize ;
//	memcpy(&TextLogFont,&lgf,sizeof(LOGFONT));
	


	//strcpy(this->FontName ,lgf.lfFaceName );
	
	
	TextFont.Detach();
	this->SetCur (sfMark.CurPos );
	return TRUE;
	
}

BOOL SFEdit::SetBackColor()
{
	
	sfMark.CurPos =GetScrollPos(SB_VERT);
	ChCol.Flags =CC_RGBINIT|CC_FULLOPEN | CC_ANYCOLOR;
	ChCol.hwndOwner =NULL;
	ChCol.lStructSize =sizeof(ChCol);
	ChCol.lpCustColors =&BackColor;
	if(!ChooseColor(&ChCol))
	{
		this->SetCur (sfMark.CurPos );
		return FALSE;
	}
	
	BackColor=ChCol.rgbResult ;
	cbr.Detach ();
	cbr.CreateSolidBrush (BackColor);
	this->RedrawWindow ();

	SetCur(sfMark.CurPos );

	
	return TRUE;
}


HBRUSH SFEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	//	MessageBox("LoveZ3");
	
	//pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(TextColor);
	
	pDC->SetBkColor (BackColor);
	
	
	// TODO: Return a different brush if the default is not desired
	return (HBRUSH)cbr.GetSafeHandle ();
	
}




BOOL SFEdit::PreTranslateMessage(MSG* pMsg) 
{
	
	switch(pMsg->message )
	{
	
		
	case WM_MOUSEWHEEL:
		if(bOpen)
		sfMark.CurPos =this->GetScrollPos (SB_VERT);
		break;
	case WM_KEYDOWN:
		
		if(pMsg->wParam ==VK_UP)
		{
			SendMessage(WM_VSCROLL,SB_LINEUP);
			SendMessage(WM_VSCROLL,SB_ENDSCROLL);
			//return TRUE;
		}
		else if(pMsg->wParam ==VK_DOWN)
		{
			SendMessage(WM_VSCROLL,SB_LINEDOWN);
			SendMessage(WM_VSCROLL,SB_ENDSCROLL);
			//return TRUE;
		}
		else if(pMsg->wParam ==VK_LEFT)
		{
			SendMessage(WM_VSCROLL,SB_PAGEUP);
			SendMessage(WM_VSCROLL,SB_ENDSCROLL);
			return TRUE;
		}
		else if(pMsg->wParam ==VK_RIGHT)
		{
			SendMessage(WM_VSCROLL,SB_PAGEDOWN);
			SendMessage(WM_VSCROLL,SB_ENDSCROLL);
			return TRUE;
		}

	
		break;



		

	}

	return CEdit::PreTranslateMessage(pMsg);
}




void SFEdit::OnContextMenu(CWnd*, CPoint point)
{
		// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1){			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);						point = rect.TopLeft();			point.Offset(5, 5);		}				CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_SFEDIT));				CMenu* pPopup = menu.GetSubMenu(0);		ASSERT(pPopup != NULL);		CWnd* pWndPopupOwner = this;				while (pWndPopupOwner->GetStyle() & WS_CHILD)			pWndPopupOwner = pWndPopupOwner->GetParent();				pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,			pWndPopupOwner);	}
}

BOOL SFEdit::SaveMark()
{

	sfMark.CurPos =GetScrollPos(SB_VERT);
	if(!bOpen)return FALSE;

	strcpy(sfMark.SFMHeader ,SFMHEADER);
//	sfMark.CurPos =GetScrollPos (SB_VERT);	
	sfMark.bkColor =this->BackColor ;
	sfMark.txColor =this->TextColor ;
	strcpy(sfMark.sfPath ,this->lpFileName );
	strcpy(sfMark.sfTitle ,this->txTitle);
//	sfMark.txSize =this->TextSize;
	sfMark.txLogFont =TextLogFont;
	sfMark.txChFont =ChFont;
	
	
	
	
	OPENFILENAME     Ofn;   
	memset(&Ofn,0,sizeof(OPENFILENAME));
	
	char szFile[MAX_PATH];
	strcpy(szFile,txTitle);
	strcat(szFile,".sfm");
	
	Ofn.lStructSize   =   sizeof(OPENFILENAME);     
	Ofn.hwndOwner   =   this->m_hWnd ;     
	Ofn.lpstrFilter   =   "科幻书签文件(*.sfm)\0*.sfm\0\0";     
	
	Ofn.nFilterIndex=1;
	Ofn.lpstrFile=   szFile;    
	Ofn.nMaxFile   =   MAX_PATH;     
	   
	Ofn.Flags   =   0x1800|OFN_OVERWRITEPROMPT;     
	
	Ofn.lpstrTitle   =   "科幻书签保存位置";     
	
	
	int ret=GetSaveFileName(&Ofn);  
	if(!ret)return FALSE;
	
	if(Ofn.lpstrFile [lstrlen(Ofn.lpstrFile )-4]!='.')strcat(Ofn.lpstrFile ,".sfm");
	DWORD size;
	HANDLE hf=CreateFile(Ofn.lpstrFile ,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hf)
	{
		MessageBox("文件创建失败,请保存无重名后重试");
		return FALSE;
	}
	
	if(!WriteFile(hf,&sfMark,sizeof(sfMark),&size,NULL))
	{
		MessageBox("书签写入失败");
		CloseHandle(hf);
		return FALSE;
	}
	
	CloseHandle(hf);
	
	SetCur(sfMark.CurPos );
	
	
	
	
	
	return TRUE;
}

void SFEdit::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	if(bOpen&&nSBCode==SB_ENDSCROLL)
	{
		sfMark.CurPos =GetScrollPos(SB_VERT);

	
	}
	
	CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
}

BOOL SFEdit::LoadMark(BOOL nf,char *LoadName)
{
	OPENFILENAME     Ofn;   
	memset(&Ofn,0,sizeof(OPENFILENAME));
	

	
	
	Ofn.lStructSize   =   sizeof(OPENFILENAME);     
	Ofn.hwndOwner   =   this->m_hWnd ;     
	Ofn.lpstrFilter   =   "科幻书签文件(*.sfm)\0*.sfm\0\0";     
	Ofn.nFilterIndex=1;
	strcpy(lpFileName,"");
	Ofn.lpstrFile=   lpFileName;     
	Ofn.nMaxFile   =   MAX_PATH;     
	
	Ofn.Flags   =   0x1800|OFN_OVERWRITEPROMPT;     
	
	Ofn.lpstrTitle   =   "加载书签";     
	
	if(nf)
	{
		int ret=GetOpenFileName(&Ofn);  
		if(!ret)return FALSE;
	}
	else
	{
		strcpy(Ofn.lpstrFile ,LoadName);
		
	}
	HANDLE hf=CreateFile(Ofn.lpstrFile ,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hf)
	{
		MessageBox("书签打开失败");
		return FALSE;
	}
	DWORD rsize;
	
	
	if(!ReadFile(hf,&sfMark,sizeof(sfMark),&rsize,NULL))
	{
		MessageBox("读取书签失败");
		CloseHandle(hf);
		return FALSE;
	};
	
	CloseHandle(hf);
	
	if(strcmp(sfMark.SFMHeader ,SFMHEADER)!=0)
	{
		MessageBox("书签损坏.");
		return FALSE;
	}
	strcpy(txTitle,sfMark.sfTitle);
	if(!OpenFile(FALSE,sfMark.sfPath ))
	{
		MessageBox("OpenFile 失败");
		return FALSE;
	}


	
	BackColor=sfMark.bkColor;
	TextColor=sfMark.txColor ;
	TextLogFont=sfMark.txLogFont ;

	
		
	
	strcpy(lpFileName,sfMark.sfPath );
	strcpy(txTitle,sfMark.sfTitle );
	::SetWindowText (this->GetParent ()->m_hWnd ,txTitle);
	memset(&ChFont ,0,sizeof(ChFont));
	memset(&ChCol,0,sizeof(ChCol));
	cbr.Detach();
	cbr.CreateSolidBrush (BackColor);

	//TextSize=sfMark.txSize ;

	
	ChFont=sfMark.txChFont ;
	ChFont.lpLogFont =&sfMark.txLogFont ;
	CFont TextFont;
	TextFont.CreateFontIndirect (ChFont.lpLogFont  );
	SetFont (&TextFont);
	TextFont.Detach ();

	//bFs=sfMark.sfFull ;

	
	SetCur(sfMark.CurPos );

	
	

	bOpen=TRUE;
	return TRUE;

}

BOOL SFEdit::SaveDefault()
{
	if(!bOpen)return FALSE;

	strcpy(sfMark.SFMHeader ,SFMHEADER);
	
	sfMark.bkColor =this->BackColor ;
	sfMark.txColor =this->TextColor ;
	strcpy(sfMark.sfPath ,this->lpFileName );
	strcpy(sfMark.sfTitle ,this->txTitle);
//	sfMark.txSize =this->TextSize;
	sfMark.txLogFont =TextLogFont;
	sfMark.txChFont =ChFont;
	
	
	
	
		char szFile[MAX_PATH];
	GetModuleFileName(NULL,szFile,MAX_PATH);
	char *p,*c;
	p=strchr(szFile,'\\');

	
	while(p)
	{
		
		p++;
		c=p;
		p=strchr(p,'\\');
		
	}
	c[0]='\0';

	strcat(szFile,"default.sfm");

	
	DWORD size;
	HANDLE hf=CreateFile(szFile ,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hf)
	{
		return FALSE;
	}
	
	if(!WriteFile(hf,&sfMark,sizeof(sfMark),&size,NULL))
	{
		CloseHandle(hf);
		return FALSE;
	}
	
	CloseHandle(hf);
	

	
	
	return TRUE;

}

BOOL SFEdit::OpenDefault()
{
	

	
	

		char szFile[MAX_PATH];
	GetModuleFileName(NULL,szFile,MAX_PATH);
	char *p,*c;
	p=strchr(szFile,'\\');

	
	while(p)
	{
		
		p++;
		c=p;
		p=strchr(p,'\\');
		
	}
	c[0]='\0';

	strcat(szFile,"default.sfm");
	

	HANDLE hf=CreateFile(szFile ,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hf)
	{
	
		return FALSE;
	}
	DWORD rsize;
	
	
	if(!ReadFile(hf,&sfMark,sizeof(sfMark),&rsize,NULL))
	{
	
		CloseHandle(hf);
		return FALSE;
	};
	
	CloseHandle(hf);
	
	if(strcmp(sfMark.SFMHeader ,SFMHEADER)!=0)
	{
	
		return FALSE;
	}
	strcpy(txTitle,sfMark.sfTitle);
	if(!OpenFile(FALSE,sfMark.sfPath ))
	{
		MessageBox("载入默认失败.");
		return FALSE;
	}


	
	BackColor=sfMark.bkColor;
	TextColor=sfMark.txColor ;
	TextLogFont=sfMark.txLogFont ;

	
	
	strcpy(lpFileName,sfMark.sfPath );
	strcpy(txTitle,sfMark.sfTitle );
	::SetWindowText (this->GetParent ()->m_hWnd ,txTitle);
	memset(&ChFont ,0,sizeof(ChFont));
	memset(&ChCol,0,sizeof(ChCol));
	cbr.Detach();
	cbr.CreateSolidBrush (BackColor);

	//TextSize=sfMark.txSize ;

	
	ChFont=sfMark.txChFont ;
	ChFont.lpLogFont =&sfMark.txLogFont ;
	CFont TextFont;
	TextFont.CreateFontIndirect (ChFont.lpLogFont  );
	SetFont (&TextFont);
	TextFont.Detach ();



	SetCur(sfMark.CurPos );

	


	
	

	bOpen=TRUE;
	return TRUE;

}



int SFEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CEdit::OnCreate(lpCreateStruct) == -1)
		return -1;
	
		

	return 0;
}

BOOL SFEdit::PreCreateWindow(CREATESTRUCT& cs) 
{
		

	return CEdit::PreCreateWindow(cs);
}

void SFEdit::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CEdit::OnShowWindow(bShow, nStatus);
	

	
	
	
}





void SFEdit::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CEdit::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);

	
}

BOOL SFEdit::SetCur(int nPos)
{

	int npos=nPos *0x10000;

	
	SendMessage(WM_VSCROLL,SB_THUMBPOSITION|npos);

	return TRUE;
}

void SFEdit::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class

	CEdit::DoDataExchange(pDX);
}

void SFEdit::OnLoaddef() 
{
	
}

void SFEdit::OnSetFocus(CWnd* pOldWnd) 
{
	CEdit::OnSetFocus(pOldWnd);
	
	
	this->HideCaret ();	
	this->SetSel(-1,-1);
}

void SFEdit::ReSize(int cx,int cy)
{

	::SetWindowPos(this->m_hWnd,HWND_NOTOPMOST   ,0 ,0 ,cx ,cy ,SWP_NOZORDER);
	//this->RedrawWindow ();
	//this->SetFocus ();

}

void SFEdit::OnUpdate() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CEdit::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
//	this->SetSel(-1,-1);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -