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

📄 duanyuview.cpp

📁 蒙古语固定短语标注! 供少数民族语言研究者参考和使用! 有问题请指教!
💻 CPP
字号:
// duanyuView.cpp : implementation of the CDuanyuView class
//
#include <windows.h>
#include <commdlg.h>
#include <stdlib.h>
#include <stdio.h>

#include "stdafx.h"
#include "duanyu.h"

#include "ProgressDlg.h"

#include "duanyuDoc.h"
#include "CntrItem.h"
#include "duanyuView.h"

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

#define UNTITLED "(untitled)"

static char      szFileName[_MAX_PATH] ;
static char      szTitleName[_MAX_FNAME + _MAX_EXT] ;

static OPENFILENAME ofn ;

RECT         rectdlg; 
RECT         rectmain;

long         temteg=0;


static BOOL    bNeedSave=FALSE;
static UINT    iMsgFindReplace;
CDialog* m_pModeless=NULL;

short g=3;
//static PSTR   pstrBuffer ;
//CProgressDlg pdlg;
/////////////////////////////////////////////////////////////////////////////
// CDuanyuView

IMPLEMENT_DYNCREATE(CDuanyuView, CRichEditView)

BEGIN_MESSAGE_MAP(CDuanyuView, CRichEditView)
	//{{AFX_MSG_MAP(CDuanyuView)
	ON_WM_DESTROY()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_TEMDEG_TMG, OnTemdegTmg)
	ON_WM_CHAR()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDuanyuView construction/destruction

CDuanyuView::CDuanyuView()
{
	// TODO: add construction code here
	//m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pModeless = NULL;

}

CDuanyuView::~CDuanyuView()
{
}

BOOL CDuanyuView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRichEditView::PreCreateWindow(cs);
}

void CDuanyuView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();


	// Set the printing margins (720 twips = 1/2 inch).
	SetMargins(CRect(720, 720, 720, 720));

	           //PopFontInitialize (hwndEdit) ;
			   PopFileInitialize();

               iMsgFindReplace = RegisterWindowMessage (FINDMSGSTRING) ;

                         // Process command line

              /*lstrcpy (szFileName, (PSTR)
                        (LPCREATESTRUCT) lpCreateParams)) ;

               if (strlen (szFileName) > 0)
                    {
                    GetFileTitle (szFileName, szTitleName,
                                  sizeof (szTitleName)) ;

                    if (!PopFileRead (hwndEdit, szFileName))
                         OkMessage (hwnd, "File %s cannot be read!",
                                          szTitleName) ;
                    }

               DoCaption (hwnd, szTitleName) ;*/

}

void CDuanyuView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CRichEditView::OnDestroy();
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
}


/////////////////////////////////////////////////////////////////////////////
// CDuanyuView diagnostics

#ifdef _DEBUG
void CDuanyuView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CDuanyuView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CDuanyuDoc* CDuanyuView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDuanyuDoc)));
	return (CDuanyuDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDuanyuView message handlers

void CDuanyuView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	if(bNeedSave && IDCANCEL==AskAboutSave(szTitleName))
		return;
	if (PopFileOpenDlg ( szFileName, szTitleName))
         {
            if (!PopFileRead ( szFileName))
			{
              //MessageBox("luanqibazaoya!",NULL,MB_OK);
                                                    
              szFileName[0]  = '\0' ;
              szTitleName[0] = '\0' ;
              }
         }
	DoCaptin(szTitleName);
}
void CDuanyuView::PopFileInitialize()
{
	
     static char szFilter[] = "Text Files (*.TXT)\0*.txt\0"  \
                              "ASCII Files (*.ASC)\0*.asc\0" \
                              "All Files (*.*)\0*.*\0\0" ;

     ofn.lStructSize       = sizeof (OPENFILENAME) ;
     ofn.hwndOwner         = NULL ;
     ofn.hInstance         = NULL; ;
     ofn.lpstrFilter       = szFilter ;
     ofn.lpstrCustomFilter = NULL ;
     ofn.nMaxCustFilter    = 0 ;
     ofn.nFilterIndex      = 0 ;
     ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
     ofn.nMaxFile          = _MAX_PATH ;
     ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
     ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT ;
     ofn.lpstrInitialDir   = NULL ;
     ofn.lpstrTitle        = NULL ;
     ofn.Flags             = 0 ;             // Set in Open and Close functions
     ofn.nFileOffset       = 0 ;
     ofn.nFileExtension    = 0 ;
     ofn.lpstrDefExt       = "txt" ;
     ofn.lCustData         = 0L ;
     ofn.lpfnHook          = NULL ;
     ofn.lpTemplateName    = NULL ;
}
BOOL CDuanyuView::PopFileOpenDlg(PSTR  pstrFileName, PSTR pstrTitleName)
{
	 ofn.hwndOwner         =CRichEditView::GetOwner()->GetSafeHwnd();// CRichEditView::m_hWnd;//CRichEditView::GetOwner();
     ofn.lpstrFile         = pstrFileName ;
     ofn.lpstrFileTitle    = pstrTitleName ;
     ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;

     return GetOpenFileName (&ofn) ;
	 
}

BOOL CDuanyuView::PopFileRead(PSTR pstrFileName)
{
	
	
     FILE  *file ;
     long int    iLength ;
     PSTR   pstrBuffer ;
	 
     if (NULL == (file = fopen (pstrFileName, "rb")))
          return FALSE ;

     iLength = PopFileLength (file) ;

     if (NULL == (pstrBuffer = (PSTR) malloc (iLength)))
          {
          fclose (file) ;
          return FALSE ;
          }

     fread (pstrBuffer, 1, iLength, file) ;
     fclose (file) ;
     pstrBuffer[iLength] = '\0' ;
     SetWindowText (pstrBuffer) ;
     //free (pstrBuffer) ;
	 pstrBuffer[0] = '\0' ;

     return TRUE ;
     
//CProgressDlg pdlg;
}

long CDuanyuView::PopFileLength(FILE *file)
{
	
     int iCurrentPos, iFileLength ;

     iCurrentPos = ftell (file) ;

     fseek (file, 0, SEEK_END) ;

     iFileLength = ftell (file) ;

     fseek (file, iCurrentPos, SEEK_SET) ;

     return iFileLength ;

}

short CDuanyuView::AskAboutSave(char *szTitleName)
{
     char szBuffer[64 + _MAX_FNAME + _MAX_EXT] ;
     int  iReturn ;

     wsprintf (szBuffer, "Save current changes in %s?",
               szTitleName[0] ? szTitleName : UNTITLED) ;
	 
     iReturn = MessageBox (szBuffer, NULL, MB_YESNOCANCEL | MB_ICONQUESTION) ;

     if (iReturn == IDYES)
          if (!SendMessage (WM_COMMAND, ID_FILE_SAVE, 0L))
               iReturn = IDCANCEL ;

     return iReturn ;
}
void CDuanyuView::DoCaptin(char *szTitleName)
{
	
     char szCaption[64 + _MAX_FNAME + _MAX_EXT] ;

     wsprintf (szCaption, "%s - %s", "Phrase",
               szTitleName[0] ? szTitleName : UNTITLED) ;

	
	 CRichEditView::GetOwner()->SetWindowText(szCaption);
	
}
void CDuanyuView::OnFileSave() 
{
	// TODO: Add your command handler code here
	/*if (szFileName[0])
          {
            if (PopFileWrite (szFileName))
                {
                   bNeedSave = FALSE ;
                    return  ;
                 }
             else
                 MessageBox("文件不能打开!",NULL,MB_OK);
           return  ;
		}*/

	
	if (PopFileSaveDlg (szFileName, szTitleName))
       {
          DoCaptin (szTitleName) ;

          if (PopFileWrite ( szFileName))
              {
                bNeedSave = FALSE ;
                return  ;
              }
          else
              MessageBox("文件不能写!",NULL,MB_OK);
        }
     return  ;
	
}
BOOL CDuanyuView::PopFileWrite(PSTR pstrFileName)
{
	 CString text;
     FILE  *file ;
     long int    iLength ;
	 long int i;
	 int Begin=1;
     LPSTR   pstrBuffer ;
     CHARRANGE cr;
     if (NULL == (file = fopen (pstrFileName, "w+")))
          return FALSE ;

//GetRichEditCtrl().GetWindowText(text);

iLength=GetRichEditCtrl().GetWindowTextLength();//GetLimitText();
cr.cpMax=iLength;
cr.cpMin=0;
GetRichEditCtrl().SetSel(cr);
text=GetRichEditCtrl().GetSelText();

pstrBuffer = (LPSTR) malloc (iLength + 1);
pstrBuffer=text.GetBuffer(text.GetLength());
for(i=0;i<text.GetLength();i++)
{
    //pstrBuffer+i;
	if((*(pstrBuffer+i)==13)||(*(pstrBuffer+i)==10))
	{
		while(1)
		{
			i++;
			if((*(pstrBuffer+i)!=13)&&(*(pstrBuffer+i)!=10))
			{
				fputc(10,file);
				break;
			}
		}
	}
	fputc(*(pstrBuffer+i),file);
}
    //fwrite (pstrBuffer, 1, text.GetLength(), file);


	 fclose(file);
	 //free (pstrBuffer) ;

   return TRUE ;

}

BOOL CDuanyuView::PopFileSaveDlg(PSTR pstrFileName, PSTR pstrTitleName)
{
	
     ofn.hwndOwner         = CRichEditView::GetOwner()->GetSafeHwnd();
     ofn.lpstrFile         = pstrFileName ;
     ofn.lpstrFileTitle    = pstrTitleName ;
     ofn.Flags             = OFN_OVERWRITEPROMPT ;

     return GetSaveFileName (&ofn) ;

}

void CDuanyuView::OnTemdegTmg() 
{
	// TODO: Add your command handler code here
	 static char    Phrase[50];
	 static char    Phrase1[50];
     static char    Type[5];
     static char    Separator[60]="* ";
	 long int       j=0,i=0,Flag=1;
     char           Charactor,char2;
	 float          fileLength=0L;
	 float          PhrasePos=0;
	
	 
	 FILE           *fp;     
	 CHARRANGE      cr;
	 FINDTEXTEX     ftxt;

	 //RECT         rectdlg; 
	 //RECT         rectmain;
     	  	
     ftxt.chrg.cpMax=GetRichEditCtrl().GetWindowTextLength();
     ftxt.chrg.cpMin=0L;
     ftxt.lpstrText="";
     ftxt.chrgText.cpMax=0L;
     ftxt.chrgText.cpMin=0L;

         //创建对话框
     
	if (m_pModeless == NULL)
	{
		m_pModeless = new CProgressDlg(this);		
		m_pModeless->Create() ;			
	}
	else
		m_pModeless->SetActiveWindow();

        //获取主窗口和对话框窗口大小

	 m_pModeless->GetWindowRect(&rectdlg);	
	 CRichEditView::GetWindowRect(&rectmain);

	    //调整对话框位置

     m_pModeless->SetWindowPos(&wndTopMost, rectmain.right-(rectdlg.right-rectdlg.left)-25,
		 rectmain.bottom-(rectdlg.bottom-rectdlg.top)-10,(rectdlg.right-rectdlg.left),
		 (rectdlg.bottom-rectdlg.top),SWP_SHOWWINDOW );  
	
	   
	   //打开短语库

	if (NULL==(fp = fopen ("text.txt", "rb")))
	{
       MessageBox("数据库不能打开!",NULL,MB_OK);
	   return;
	}

	   //获取文件长度
	while(!feof(fp))
	{	
		if(getc(fp)==13)
			fileLength++;
	}
	   //调整文件指针
	fseek(fp,0L,SEEK_SET);


	    //设定进度条的范围   

    ::SendMessage(m_pModeless->GetDlgItem(IDC_PROGRESS1)->GetSafeHwnd(),PBM_SETRANGE,0,MAKELPARAM(0,fileLength));

	    //主循环开始,
	m_pModeless->aborted=0;

   while(!feof(fp))
	{
      if(m_pModeless->aborted==1)
		  break;

	  Charactor=getc(fp);
	  char2=Charactor;
	  if((Charactor!=10)&&(Charactor!=13))
		{
			if(Charactor==',')
				Flag=0;

			if((Charactor!='"')&&(Flag==1)&&(Charactor!=','))
				{
				      //Phrase[j]=Charactor;
				      //j++;
				  if(Charactor==' ')
				  {
					  Phrase1[j]='=';
					  Phrase[j]=Charactor;
					  j++;
				  }
				  else
				  {
				      Phrase1[j]=Charactor;
					  Phrase[j]=Charactor;
				      j++;
				  }
				}

			if((Charactor!='"')&&(Charactor!=',')&&(Flag==0))
				{
				  Type[i]=Charactor;
				  i++;
				}								
		}
		if(((Charactor==13)||(Charactor==10))&&(char2!=13))
		{	
			PhrasePos++;

                 //更新进度条
			::SendMessage(m_pModeless->GetDlgItem(IDC_PROGRESS1)->GetSafeHwnd(),PBM_SETPOS,PhrasePos,0);

			MSG msg;
			if(PeekMessage((LPMSG)&msg,(HWND)NULL,(WORD)NULL,(WORD)NULL,TRUE))
			{
				TranslateMessage((LPMSG)&msg);
				DispatchMessage((LPMSG)&msg);
			}

			Flag=1;
			Phrase[j]='\0';
            Phrase1[j]='\0';
			Type[i]='\0';


			strcpy(Separator,Phrase1);
								
			//strcat(Separator,Phrase);
			//strcat(Separator," * ");
			//strcat(Separator,Type);
			//strcat(Separator," *");

			ftxt.lpstrText=Phrase;

             while(1)
			{
			    ftxt.chrg.cpMax=GetRichEditCtrl().GetWindowTextLength();
			   
			    if((GetRichEditCtrl().FindText(FR_WHOLEWORD ,&ftxt))==-1)
				{
					break;
				}
			   else
				{	

				   ftxt.chrg.cpMin=ftxt.chrgText.cpMax;
				   cr.cpMin=ftxt.chrgText.cpMin;
				   cr.cpMax=ftxt.chrgText.cpMax;				   	       


				   GetRichEditCtrl().SetSel(cr);				   				   	   
				   GetRichEditCtrl().ReplaceSel(Separator,FALSE);
				   cr.cpMin=ftxt.chrgText.cpMin;
				   cr.cpMax=cr.cpMin+strlen(Separator);
				   GetRichEditCtrl().SetSel(cr);

				}			
											
			}
			ftxt.chrg.cpMin=0;
			i=0;
			j=0;
			Separator[0]='\0';
			//Separator[0]='*';
	        //Separator[1]=' ';
			//Separator[2]='\0';
			
		}
	}
 fclose(fp);
 if(m_pModeless->aborted!=1)
 {
	 m_pModeless->DestroyWindow();
     MessageBox("固定短语标注完毕!",NULL,MB_OK);
 }
	
}

void CDuanyuView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CRichEditView::OnChar(nChar, nRepCnt, nFlags);
	bNeedSave=TRUE;
}

void CDuanyuView::OnSize(UINT nType, int cx, int cy) 
{
	CRichEditView::OnSize(nType, cx, cy);
	//m_pModeless->UpdateWindow();
	// TODO: Add your message handler code here
	
}
BOOL CDuanyuView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


void CDuanyuView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	//::SendMessage(m_pModeless->m_hWnd,WM_PAINT,NULL,NULL);
	//m_pModeless->UpdateWindow();
	
}

⌨️ 快捷键说明

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