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

📄 createrdlg.cpp

📁 智能阅读器       阅读器(运行于WINCE平台):阅读器满足一般现实中阅读书籍时的需求
💻 CPP
字号:
// CreaterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Creater.h"
#include "CreaterDlg.h"
#include "NameBox.h"
#include "NumBox.h"
#include "FileLoader.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCreaterDlg dialog

CCreaterDlg::CCreaterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCreaterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCreaterDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCreaterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCreaterDlg)
	DDX_Control(pDX, IDC_DELCHAPTER, m_delchapter);
	DDX_Control(pDX, IDC_BUTTON3, m_autodiv);
	DDX_Control(pDX, IDC_CHECK2, m_auto);
	DDX_Control(pDX, IDC_NEWCHAPTER, m_newchapter);
	DDX_Control(pDX, IDC_FILENAME, m_filename);
	DDX_Control(pDX, IDC_TEXT, m_text);
	DDX_Control(pDX, IDC_CHAPTER, m_chapter);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCreaterDlg, CDialog)
	//{{AFX_MSG_MAP(CCreaterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADDFILE, OnAddfile)
	ON_BN_CLICKED(IDC_NEWCHAPTER, OnNewChapter)
	ON_LBN_DBLCLK(IDC_CHAPTER, OnDblclkChapter)
	ON_LBN_SELCHANGE(IDC_CHAPTER, OnSelchangeChapter)
	ON_BN_CLICKED(IDC_CHECK2, OnCheck2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_DELCHAPTER, OnDelchapter)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_ClearChapter, OnClearChapter)
	ON_BN_CLICKED(IDC_CREAT, OnCreat)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCreaterDlg message handlers

BOOL CCreaterDlg::OnInitDialog()
{
	int i;
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	tot_chapter = 0;
	for(i=0 ;i<10000 ;i++ )
	{
		cn_pos[i] = -1;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCreaterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCreaterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCreaterDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCreaterDlg::OnAddfile() 
{
	// TODO: Add your control notification handler code here
	CFile	file;
	char	tmp_text[file_length];
	int		line,i,l;
	CFileDialog filedialog(true,"txt",NULL,OFN_HIDEREADONLY,"Source Files (*.txt)|*.txt|All Files (*.*)|*.*||");	
	filedialog.DoModal();
	if(filedialog.GetPathName().GetLength() > 0)
	{
		m_filename.SetWindowText(filedialog.GetPathName());
		if(file.Open(filedialog.GetPathName(),CFile::modeRead))
		{
			if(file.GetLength()<file_length-2)
			{
				l = file.GetLength();
				line = m_text.GetFirstVisibleLine();
				memset(tmp_text,0,file_length);
				file.Read(tmp_text,file_length);
				if(tmp_text[l-1]!= '\n')
				{
					tmp_text[l] = 13;
					tmp_text[l+1] = 10;
					tmp_text[l+2] = 0;
				}
				text.operator +=(tmp_text);
				m_text.SetWindowText(text);
				m_text.LineScroll(line);
				if(m_auto.GetCheck())
				{				
					i = m_chapter.GetCount();
					EmptyInfo(false);
					AutoPartition(i);
				}
			}
			else
			{
				::AfxMessageBox("文件不能超过500k。");
			}
		}
	}
}

void CCreaterDlg::OnNewChapter() 
{
	// TODO: Add your control notification handler code here
	int line,pos,dif,i,th,tmp;
	line = m_text.GetFirstVisibleLine();
	if(text.GetLength() == 0)
	{
		::AfxMessageBox("还没导入文件。");
	}
	else
	{
		if(cn_pos[line/min_interval] == -1)
		{
			NameBox namebox;
			namebox.DoModal();
			if(namebox.m_name.GetLength()>0)
			{		
				tmp = line/min_interval;
				if(tot_chapter<max_chapter)
				{
					
					dif = line%min_interval;
					cn_pos[line/min_interval] = dif*1000+tot_chapter;
					chapter_name[tot_chapter].operator =(namebox.m_name);
					for(i=0,th = 0 ;i<tmp ;i++ )
						if(cn_pos[i] != -1)
							th++;
					m_chapter.InsertString(th,chapter_name[tot_chapter]);
					tot_chapter++;
				}
				else
				{
					::AfxMessageBox("章节过多。");
				}
			}	
		}
		else
		{
			::AfxMessageBox("此处附近已经生成过章节.");
		}
	}
}

void CCreaterDlg::OnDblclkChapter() 
{
	// TODO: Add your control notification handler code here
	int i,line,k,j,th;
	NameBox namebox;
	namebox.DoModal();
	if(namebox.m_name.GetLength()>0)
	{
		line = m_text.GetFirstVisibleLine();
		if(cn_pos[line/min_interval] == -1)
		{
			::AfxMessageBox("errror!!");
		}
		else
		{
			chapter_name[cn_pos[line/min_interval]%1000].operator =(namebox.m_name);
			for(i=0 ;i<m_chapter.GetCount() ;i++  )
				if(m_chapter.GetSel(i) == 1)
				{
					m_chapter.DeleteString(i);
					m_chapter.InsertString(i,chapter_name[cn_pos[line/min_interval]%1000]);
					break;
				}
		}
	}
}

void CCreaterDlg::OnSelchangeChapter() 
{
	// TODO: Add your control notification handler code here
	int i,line,k,j,th;
	for(i=0 ;i<m_chapter.GetCount() ;i++  )
		if(m_chapter.GetSel(i) == 1)
		{
			for(j=0,th = -1 ;j<max_chapter ;j++ )
				if(cn_pos[j] != -1)
				{
					th++;
					if(th == i)
					{
						line = m_text.GetFirstVisibleLine();
						k =	cn_pos[j]/1000+j*min_interval;
						m_text.LineScroll(k-line);
						break;
					}
				}
			break;
		}
}

void CCreaterDlg::OnCheck2() 
{
	// TODO: Add your control notification handler code here
	int i;
	int state;	

	if(text.GetLength() > 0)
	{
		state = m_auto.GetCheck();
		EmptyInfo(false);
		if(state)
		{
			m_newchapter.EnableWindow(false);	
			m_delchapter.EnableWindow(false);
			m_autodiv.EnableWindow(true);
		}
		else
		{
			m_newchapter.EnableWindow(true);
			m_delchapter.EnableWindow(true);
			m_autodiv.EnableWindow(false);
		}
	}
	else
	{
		m_auto.SetCheck(0);
	}
}

void CCreaterDlg::OnEditchangeCombo2() 
{
	// TODO: Add your control notification handler code here
	
}

void CCreaterDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	int i,maxc,num;
	int minp,maxp,tol_line;
	char buf[20];
	NumBox cnumbox;
	if(text.GetLength() > 0)
	{
		m_text.GetScrollRange(SB_VERT,&minp,&maxp);
		tol_line = maxp;
		maxc = tol_line/min_interval;
		EmptyInfo(false);
		cnumbox.DoModal();
		if(cnumbox.m_num.GetLength()>0)
		{
			sscanf(cnumbox.m_num,"%d",&num);
			if(num>0 && num<maxc+1)
			{
				AutoPartition(num);
			}
			else
			{
				sprintf(buf,"请输入数字,此文章最多只能分成%d段.",maxc);
				::AfxMessageBox(buf);
			}
		}
	}
	else
	{
		::AfxMessageBox("还没导入文件。");
		m_auto.SetCheck(0);
	}
}

void CCreaterDlg::AutoPartition(int n)
{
	int i;
	int minp,maxp,tol_line;
	int interval,addition,line,ti,ta;
	char buf[20];
	double tmp;
	m_text.GetScrollRange(SB_VERT,&minp,&maxp);
	tol_line = maxp;
	interval = tol_line/n;
	tmp = (double)tol_line/(double)n;
	tmp -= interval;
	if(tmp != 0)
	{
		addition = 1/tmp+1;
	}
	else
	{
		addition = 0;
	}
	line = 0;
	ta = 1;
	while(line<tol_line && tot_chapter < n)
	{		
		sprintf(buf,"第%d章",tot_chapter+1);
		m_chapter.InsertString(tot_chapter,buf);
		chapter_name[tot_chapter].operator = (buf);
		cn_pos[line/min_interval] = tot_chapter+(line%min_interval)*1000;
		tot_chapter++;
		line += interval;
		i++;
		if(addition)
		{
			ta--;
			if(ta == 0)
			{
				ta = addition;
				line +=1;
			}
		}
	}
	sprintf(buf,"每章节平均%d行左右。",tol_line/n);
	::AfxMessageBox(buf);
}

void CCreaterDlg::EmptyInfo(bool tag)
{
	int i;
	tot_chapter = 0;
	for(i=0 ;i<max_chapter ;i++ )
		cn_pos[i] = -1;
	m_chapter.ResetContent();
	if(tag)
	{
		text.Empty();
		m_text.SetWindowText("");
	//	m_text.Clear();
	}
}

void CCreaterDlg::OnDelchapter() 
{
	// TODO: Add your control notification handler code here
	int i,th,j;
	if(text.GetLength() == 0)
	{
		::AfxMessageBox("还没导入文件。");
	}
	else
	{
		for(i=0 ;i<m_chapter.GetCount() ;i++  )
			if(m_chapter.GetSel(i) == 1)
				break;		
		for(j=0,th = 0 ;j<max_chapter ;j++ )
			if(cn_pos[j] != -1)
			{
				th++;			
				if(th == i+1)
				{
					cn_pos[j] = -1;
					m_chapter.DeleteString(i);
					break;
				}
			}
	}
}

void CCreaterDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	EmptyInfo(true);
	m_filename.SetWindowText("");
}

void CCreaterDlg::OnClearChapter() 
{
	// TODO: Add your control notification handler code here
	EmptyInfo(false);
}

void CCreaterDlg::OnCreat() 
{
	// TODO: Add your control notification handler code here
	int l,i,j,t,k;
	char buf[1000];
	LPWSTR ttt;
	CFileDialog saver(false,"pbd",NULL,OFN_HIDEREADONLY,"pbd");
	CFile file;
	saver.DoModal();
//	saver.set
	if(saver.GetPathName().GetLength() > 0)
	{
		t = 0;		
		l = text.GetLength();
		sprintf(buf,"%d",l);
		k = strlen(buf);
		buf[k] = '\n';
		buf[k+1] = 0;
//		buf[k] = 13;
//		buf[k+1] = 10;
//		buf[k+2] = 0;
		
		while(t != strlen(buf))
		{
			t = strlen(buf);
			sprintf(buf,"%d",l+t);
			k = strlen(buf);
			buf[k] = '\n';
			buf[k+1] = 0;
//			buf[k] = 13;
//			buf[k+1] = 10;
//			buf[k+2] = 0;
		}
		text.Insert(0,buf);
		for(i=0,j=0 ;i<max_chapter ;i++ )
			if(cn_pos[i]!=-1)
				j++;
		sprintf(buf,"%d",j);
		k = strlen(buf);
		buf[k] = '\n';
		buf[k+1] = 0;
//		buf[k] = 13;
//		buf[k+1] = 10;
//		buf[k+2] = 0;
		text.operator += (buf);
		l = text.GetLength();
		for(i=0 ;i<max_chapter ;i++ )
			if(cn_pos[i]!=-1)
			{				
				sprintf(buf,"%d %s",i*min_interval+cn_pos[i]/1000,chapter_name[cn_pos[i]%1000]);
				k = strlen(buf);
				buf[k] = '\n';
				buf[k+1] = 0;
//				buf[k] = 13;
//				buf[k+1] = 10;
//				buf[k+2] = 0;
				text.operator += (buf);
			}
		text.operator +=("2\n");
		text.operator +=("10 riysh\n");
		text.operator +=("50 riyshagain\n");
		l = text.GetLength();
		if(file.Open(saver.GetPathName(),CFile::modeWrite|CFile::modeCreate))
		{
			ttt = new WCHAR[l*2];
			int a=MultiByteToWideChar(CP_ACP,0,text,l,ttt,l*2);
			file.Write(ttt,l*2);
		}
		else
		{
			::AfxMessageBox("打开写文件错误。");
		}
	}
}
//ret = WideCharToMultiByte(g_iCodePageList[k], 0, (unsigned short *)wbuff, 1, buff, 2, 0,0)     
void CCreaterDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedialog(true);
	
	filedialog.DoModal();
	if(filedialog.GetPathName().GetLength() > 0)
	{
		FileLoader	test(filedialog.GetPathName());		
	}
}

⌨️ 快捷键说明

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