📄 badcopydlg.cpp
字号:
// BadCopyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BadCopy.h"
#include "BadCopyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CBadCopyDlg *CBadCopyDlg::lpThis=NULL;
bool CBadCopyDlg::bThread=false;
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()
/////////////////////////////////////////////////////////////////////////////
// CBadCopyDlg dialog
CBadCopyDlg::CBadCopyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBadCopyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBadCopyDlg)
m_From = _T("");
m_To = _T("");
m_Text = _T("");
m_AllBit = _T("");
m_PosBit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBadCopyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBadCopyDlg)
DDX_Control(pDX, IDC_BUTTON_COPY, m_CopyFile);
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
DDX_Text(pDX, IDC_EDIT_FORM, m_From);
DDX_Text(pDX, IDC_EDIT_TO, m_To);
DDX_Text(pDX, IDC_STATIC_COPY, m_Text);
DDX_Text(pDX, IDC_STATIC_ALL, m_AllBit);
DDX_Text(pDX, IDC_STATIC_POS, m_PosBit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBadCopyDlg, CDialog)
//{{AFX_MSG_MAP(CBadCopyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_BROWSE_FROM, OnBrowseFrom)
ON_BN_CLICKED(ID_BROWSE_TO, OnBrowseTo)
ON_BN_CLICKED(IDC_BUTTON_COPY, OnButtonCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBadCopyDlg message handlers
BOOL CBadCopyDlg::OnInitDialog()
{
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
return TRUE; // return TRUE unless you set the focus to a control
}
void CBadCopyDlg::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 CBadCopyDlg::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 CBadCopyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBadCopyDlg::OnBrowseFrom()
{
//Get while copy file path
UpdateData(TRUE);
CFileDialog OpenDlg(TRUE,NULL,NULL,0,"*.*|*.*|\0",NULL);
if(OpenDlg.DoModal())
m_From=OpenDlg.GetPathName();
FileExt=OpenDlg.GetFileExt();
UpdateData(FALSE);
}
void CBadCopyDlg::OnBrowseTo()
{
//Set new file path
UpdateData(TRUE);
CString sExt;
if(FileExt.GetLength()>=1)
{
sExt="*.";
sExt+=FileExt;
sExt+="|";
sExt+=sExt;
}
sExt+="*.*|*.*|\0";
CFileDialog OpenDlg(FALSE,FileExt,NULL,0,sExt,NULL);
if(OpenDlg.DoModal())
m_To=OpenDlg.GetPathName();
UpdateData(FALSE);
}
DWORD CBadCopyDlg::CopyThread(LPVOID lpPram)
{
CFile fp,fpp;
CString sErr;
if(!fp.Open(lpThis->m_From,CFile::modeRead,NULL))
{
sErr="Open File ";
sErr+=lpThis->m_From;
sErr+=" Error!";
AfxMessageBox(sErr,16);
return 0;
}
if(!fpp.Open(lpThis->m_To,CFile::modeWrite|CFile::modeRead,NULL))
if(!fpp.Open(lpThis->m_To,CFile::modeCreate|CFile::modeWrite|CFile::modeRead,NULL))
{
sErr="Open File ";
sErr+=lpThis->m_To;
sErr+=" Error!";
AfxMessageBox(sErr,16);
return 0;
}
int size=0;
double how;
UINT oldsize=fpp.GetLength();
UINT copysize=fp.GetLength();
if(oldsize && AfxMessageBox("发现复制的断点\n是否续传!",36)==IDYES)
{
copysize-=oldsize;
if(copysize%512==0) how=copysize/512;
else how=copysize/512+1;
fp.Seek(oldsize,0);
fpp.Seek(oldsize,0);
}
else
{
if(copysize%512==0) how=copysize/512;
else how=copysize/512+1;
}
long lAll=copysize/1024;
lpThis->m_AllBit.Format("共: %u KB",lAll);
float pos=0;
long lPos=0;
char *buf=new char[513];
for(double i=1;i<=how;i++)
{
if(bThread==false)
{
fp.Close();
fpp.Close();
delete buf;
return -1;
}
pos=i/how*100;
lPos=i/2;
lpThis->m_PosBit.Format("已拷贝: %u KB 剩余: %u KB",lPos,lAll-lPos);
memset(buf,NULL,513);
size=fp.Read(buf,512);
fpp.Write(buf,size);
lpThis->m_Progress.SetPos((int)pos);
lpThis->Update();
}
if(fpp.GetLength()==fp.GetLength())
AfxMessageBox("文件拷贝完成!",64);
else AfxMessageBox("拷贝文件时出错!",16);
fp.Close();
fpp.Close();
lpThis->m_Progress.SetPos(0);
lpThis->m_CopyFile.SetWindowText("复制");
lpThis->m_Text="";
lpThis->m_AllBit="共:KB";
lpThis->m_PosBit="已拷贝:KB 剩余:KB";
lpThis->Update();
delete buf;
return 0;
}
void CBadCopyDlg::OnButtonCopy()
{
//Copy file
UpdateData(TRUE);
CString sText;
m_CopyFile.GetWindowText(sText);
if(sText=="复制")
{
UpdateData(TRUE);
m_Progress.SetPos(0);
m_CopyFile.SetWindowText("取消");
m_Text="开始复制……";
Update();
DWORD tid=0;
lpThis=this;
bThread=true;
HANDLE hd=CreateThread(NULL,0,CopyThread,(LPVOID)this,0,&tid);
CloseHandle(hd);
}
else
{
bThread=false;
m_Progress.SetPos(0);
m_CopyFile.SetWindowText("复制");
//AfxMessageBox("已经取消复制",16);
m_Text="";
lpThis->m_Text="";
lpThis->m_AllBit="";
lpThis->m_PosBit="";
Update();
}
}
void CBadCopyDlg::Update()
{
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -