📄 htmltotxtdlg.cpp
字号:
// HtmlToTxtDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HtmlToTxt.h"
#include "HtmlToTxtDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHtmlToTxtDlg dialog
CHtmlToTxtDlg::CHtmlToTxtDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHtmlToTxtDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHtmlToTxtDlg)
m_strTxt = _T("");
m_strHtml = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHtmlToTxtDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHtmlToTxtDlg)
DDX_Text(pDX, IDC_EDIT_TXT, m_strTxt);
DDX_Text(pDX, IDC_EDIT1_HTML, m_strHtml);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHtmlToTxtDlg, CDialog)
//{{AFX_MSG_MAP(CHtmlToTxtDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHtmlToTxtDlg message handlers
BOOL CHtmlToTxtDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CHtmlToTxtDlg::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 CHtmlToTxtDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHtmlToTxtDlg::OnButtonBrowse()
{
CFileDialog fileDlg(TRUE,"htm",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"HTML Files(*.html)|*.html|HTML Files(*.htm)|*.htm",this);
CString strTemp;
if(fileDlg.DoModal()==IDOK)
{
m_strHtml=fileDlg.GetPathName();
strTemp=m_strHtml;
}
strTemp.Replace(fileDlg.GetFileName(),"result.txt");
m_strTxt=strTemp;
UpdateData(FALSE);
}
void CHtmlToTxtDlg::OnButtonSave()
{
CFileDialog fileDlg(FALSE,"html",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"TXT Files(*.txt)|*.txt",this);
if(fileDlg.DoModal()==IDOK)
{
m_strTxt=fileDlg.GetPathName();
}
UpdateData(FALSE);
}
void CHtmlToTxtDlg::OnOK()
{
UpdateData(TRUE);
if(m_strHtml.IsEmpty())
{
AfxMessageBox("请选择一个源文件(.html,.htm)");
return;
}
CFile scrFile,dstFile;
if(!scrFile.Open(m_strHtml,CFile::modeRead))
{
AfxMessageBox("打开源文件失败!");
return;
}
if(!dstFile.Open(m_strTxt,CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox("建立目标文件失败!");
return;
}
char buf[1024],tbuf[1024];
int len;
BOOL bStartHtml=FALSE,bInTag=FALSE,bPreCr=FALSE,bPreLf=FALSE;
int num=0;
while(1)
{
len=scrFile.Read(buf,1024);
for(int i=0;i<len;i++)
{
if(!bStartHtml)
{
if(strstr(buf,"<body"))
{
bStartHtml=TRUE;
}
/*
if(buf[i]=='<')
{
bStartHtml=TRUE;
bInTag=TRUE;
//continue;
}
*/
}
else
{
if(buf[i]=='<')
{
bInTag=TRUE;
//continue;
}
if(bInTag)
{
if(buf[i]=='>')
{
bInTag=FALSE;
//continue;
}
}
else
{
if((buf[i]!='\t')&&/*(buf[i]!='\r')&&*/(buf[i]!=' ')/*&&(buf[i]!='\n')*/)
{
if((buf[i]=='\r')||(buf[i]=='\n'))
{
if(!bPreCr)
{
bPreCr=TRUE;
tbuf[num]=buf[i];
num++;
}
else if(!bPreLf)
{
bPreLf=TRUE;
tbuf[num]=buf[i];
num++;
}
}
else
{
bPreCr=FALSE;
bPreLf=FALSE;
tbuf[num]=buf[i];
num++;
}
}
}
}
}
tbuf[num]='\0';
int tLen=strlen(tbuf);
dstFile.Write(tbuf,tLen);
num=0;
memset(tbuf,0,1024);
if(len<1024)
break;
}
scrFile.Close();
dstFile.Close();
AfxMessageBox("转换完毕");
//CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -