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

📄 ebookdlg.cpp

📁 ebook实现上下翻页
💻 CPP
字号:
// ebookDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ebook.h"
#include "ebookDlg.h"
#include "Bookmark.h"
#include "Goto.h"
#include <vector>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int t=0;
DWORD ii;
extern DWORD ebookmark;
Bookmark *mymark;
Goto mygoto;
CString pagecount;
/////////////////////////////////////////////////////////////////////////////
// CEbookDlg dialog
CEbookDlg::CEbookDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEbookDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEbookDlg)
	m_editstr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEbookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEbookDlg)
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
	DDX_Control(pDX, IDC_STATIC1, m_static);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Text(pDX, IDC_EDIT1, m_editstr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEbookDlg, CDialog)
	//{{AFX_MSG_MAP(CEbookDlg)
	ON_WM_PAINT()
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEbookDlg message handlers

BOOL CEbookDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	  SetWindowPos(NULL,0,0,800,480,SWP_SHOWWINDOW );
	  m_edit.SetWindowPos(NULL,28,20,622,355,SWP_SHOWWINDOW );
	  m_edit2.SetWindowPos(NULL,697,90,80,30,SWP_SHOWWINDOW );
      m_static.SetWindowPos(NULL,697,50,80,30,SWP_SHOWWINDOW );
	  SetWindowText(L"电子书");
	  SetWindowPos(NULL,0,0,800,480,SWP_SHOWWINDOW );
	  m_edit.SetWindowPos(NULL,28,20,622,355,SWP_SHOWWINDOW );
	  m_edit2.SetWindowPos(NULL,697,90,80,30,SWP_SHOWWINDOW );
      m_static.SetWindowPos(NULL,697,50,80,30,SWP_SHOWWINDOW );
	  CString state=L"succeed";
	  HWND MyhWnd;
	  COPYDATASTRUCT MyCopyStr;
	  MyCopyStr.dwData = (DWORD)m_hWnd;//用户定义数据
	  MyCopyStr.lpData = state.GetBuffer(state.GetLength());//指向数据的指针
	  MyCopyStr.cbData = state.GetLength()*2;//数据大小
	  MyhWnd = ::FindWindow(NULL,L"电子书界面");
	  if(MyhWnd!=NULL)
	  {
		  ::SendMessage(MyhWnd,WM_COPYDATA,0,(LPARAM)&MyCopyStr);
	  }
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}




LRESULT CEbookDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(message == WM_COPYDATA)//接收消息得到双击的路径名
	{
		txtstr = (LPCTSTR)((PCOPYDATASTRUCT)lParam)->lpData;
		ReadFile(txtstr);
	}
	if(message==ebookmark)//接受书签的数据进行转跳
	{
		char*p3;
		p3=new char[20];
		p3=(char*)lParam;
		t=atoi(p3)-1;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		CString page;
		page.Format(L"第%d页",t+1);
		m_edit2.SetWindowText(page);
	    mymark->GetPage(t+1);
	}

	return CDialog::WindowProc(message, wParam, lParam);
}

void CEbookDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CDC memory;
	mycbitmap=new CBitmap;
	mycbitmap->LoadBitmap(IDB_BITMAP1);
	memory.CreateCompatibleDC(&dc);
	memory.SelectObject(mycbitmap);
	RECT myrect;
	GetWindowRect(&myrect);
	dc.BitBlt(0,0,myrect.right-myrect.left,myrect.bottom-myrect.top,&memory,0,0,SRCCOPY);
	
	// Do not call CDialog::OnPaint() for painting messages
}

BOOL CEbookDlg::InRect(int left, int top, int right, int bottom, int px, int py)
{

	if((left < 0) || (left > 800))
		return FALSE;
	if((right < 0) || (right > 800))
		return FALSE;
	if((top < 0) || (top > 480))
		return FALSE;
	if((bottom < 0) || (bottom > 480))
		return FALSE;
	if((px < 0) || (px > 800))
		return FALSE;
	if((py < 0) || (py > 480))
		return FALSE;
	
	if((px>=left)&&(px<=right) && (py>=top)&&(py<=bottom))
		return TRUE;
	return FALSE;
}

void CEbookDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (InRect(712,140,746,170,point.x,point.y))
	{
		OnButton1();
	}
	if (InRect(709,221,750,244,point.x,point.y))
	{
		OnButton2();
	}
	if (InRect(710,300,746,323,point.x,point.y))
	{
		OnButton3();
	}
	if (InRect(710,375,751,400,point.x,point.y))
	{
		OnButton4();
	}
	if (InRect(158,401,193,428,point.x,point.y))
	{
		OnButton5();
	}
	if (InRect(258,401,297,426,point.x,point.y))
	{
		OnButton6();
	}
	if (InRect(371,395,412,426,point.x,point.y))
	{
		OnButton7();
	}
	if (InRect(480,401,518,422,point.x,point.y))
	{
		OnButton8();
	}
	if (InRect(66,401,103,427,point.x,point.y))
	{
		OnButton9();
	}
	
	CDialog::OnLButtonUp(nFlags, point);
}
//********读文件*********//
void CEbookDlg::ReadFile(CString a)
{
	CString pagecount;
	CString page;
	mystring=a;
	fp=new CFile;//读文件可以用CFile,写文件不行
	fp->Open(mystring,fp->modeRead,NULL);//打开文件
	char *p ;
    ii=fp->SeekToEnd( ); //把指针移到最后返回问文件的字节数
	p=new char[ii+1];
	memset(p,0,ii+1);
	fp->SeekToBegin();//把指针移到最前
    fp->Read(p,ii);
	m_editstr=p;
    SetDlgItemText(IDC_EDIT1,m_editstr);//在编辑框进行显示
	fp->Close( );
	pagecount.Format(L"共%d页",ii/992+1);//把每页定义为992字节,计算文件有多少页
	m_static.SetWindowText(pagecount);//把共有多少页显示出来
	page.Format(L"第%d页",t+1);
	m_edit2.SetWindowText(page);//显示当前文件为第几页
	mymark=new Bookmark();
	mymark->GetPage(t+1);//把页数传到书签
}
//*******上行********//
void CEbookDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	m_edit.SendMessage(EM_SCROLL,SB_LINEUP,0);//进行上翻一行直接发消息
	
}
//*******下行********//
void CEbookDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_edit.SendMessage(EM_SCROLL,SB_LINEDOWN,0);//进行下翻一行直接发消息
	
}
//*******上页********//
void CEbookDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	if(t>=1)
	{
		
		t--;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		CString page;
		page.Format(L"第%d页",t+1);
		m_edit2.SetWindowText(page);
		mymark->GetPage(t+1);
	}

	
}
//*******下页********//
void CEbookDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	if(t<ii/992)
	{
		t++;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		CString page;
		page.Format(L"第%d页",t+1);
		m_edit2.SetWindowText(page);
    	mymark->GetPage(t+1);
	}
}
//********转跳*******//
void CEbookDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	if(mygoto.DoModal()==IDOK)
	{
		CString mygo;
		int b;
		char *a;
		a=new char[10];
		mygo=mygoto.m_goedit;
		mygoto.m_goedit.Empty();
		wcstombs(a,mygo,10);//把String转换为字符型
		b=atoi(a);//字符型变为整型
		t=b-1;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);//按照跳转偏移量读文件
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		m_edit2.SetWindowText(L"第"+mygo+L"页");
		mymark->GetPage(b);//跳转页数给书签
		
		
			
		
	}
}
//********全屏**********//
void CEbookDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	m_edit.SetWindowPos(NULL,0,0,800,480,SWP_SHOWWINDOW );
}
//********书签**********//
void CEbookDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	mymark=new Bookmark();
	mymark->Create(IDD_DIALOG1);
	mymark->SetWindowText(L"书签");
	mymark->ShowWindow(TRUE);
	
	
}
//********退出**********//
void CEbookDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	DestroyWindow();
		
	
}
//********返回**********//
void CEbookDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	HWND MyhWnd;
	MyhWnd = ::FindWindow(NULL,L"电子书界面");
	::SetForegroundWindow(MyhWnd);
}



//*********为封装新的EDIT消息,建立的函数实现下翻*********//
void CEbookDlg::Down()
{
	if(t<ii/992)
	{
		t++;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		CString page;
		page.Format(L"第%d页",t+1);
		m_edit2.SetWindowText(page);
		mymark->GetPage(t+1);
	}
}

void CEbookDlg::Up()
{
	if(t>=1)
	{
		
		t--;
		fp=new CFile;
		fp->Open(mystring,fp->modeRead,NULL);
		char *p ;
		p=new char[ii-t*992+1];
		memset(p,0,ii-t*992+1);
		fp->Seek(t*992,CFile::begin);
		fp->Read(p,ii-t*992);
		m_editstr=p;
		SetDlgItemText(IDC_EDIT1,m_editstr);
		fp->Close( );
		CString page;
		page.Format(L"第%d页",t+1);
		m_edit2.SetWindowText(page);
		mymark->GetPage(t+1);
	}
}

⌨️ 快捷键说明

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