才学SDK时写的,粗糙难免,见者无怪!
/////////////FileCut.c
#include"FileCut.h"
#include"resource.h"
BOOL SelectFile(OPENFILENAME* ofn,int Mode)
{
if(Mode==FILE_COM)
ofn->Flags=OFN_ALLOWMULTISELECT;
else if(Mode==FILE_CUT)
ofn->Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
else
exit(1);
return TRUE;
}
BOOL CutFileView(HWND hDlg,OPENFILENAME* ofn,FILENAME szFileName)
{
LONGLONG llFileSize;
HANDLE hFile;
// TCHAR szBuffer[32];
TCHAR *nextname = &ofn->lpstrFile[ofn->nFileOffset];
FormatName(ofn,szFileName);
lstrcat(szFileName,nextname);
// MessageBox(hDlg,szFileName,"filename",MB_OK);
//OpenFile
hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONERROR);
return FALSE;
}
//GetFileSize
FileSize(hFile,&llFileSize);
// sprintf(szBuffer,"%llu",llFileSize);
// MessageBox(hDlg,szBuffer,"msg",MB_OK);
if(ShowInformation(hDlg,ofn,llFileSize))
{
return TRUE;
}
else
{
MessageBox(hDlg,TEXT("SHOW INFORMATION ERROR "),TEXT("SHOW ERROR"),MB_OK);
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}
BOOL FileCom(HWND hDlg,OPENFILENAME *ofn)
{
int iFileNum,iReadFileNum;
int i=0,j,k,iTemp,iLength;
ARRFNAME arrName;
TCHAR szBuffer[2048];
TCHAR * nextname;
char* szFileBuffer;
TCHAR szSaveName[1024];
int iPos;
int iFlag=0;
char szInt[20];
LONGLONG llFileSize,llFileSizeFirst;
OPENFILENAME oFn;
HANDLE hFile,hDesFile;
TCHAR szFile[512];
TCHAR szFileTitle[512];
/////////////////////////////////////////////////////////////////
szFileBuffer=(char*)calloc(1,sizeof(char)*MB);
/////////////////////////////////////////////////////////////////
memset(&oFn,0,sizeof(oFn));
oFn.lpstrFilter=TEXT("All File(*.cut)/0*.*/0");
oFn.lpstrFile=szFile;
oFn.lpstrFile[0] = TEXT('/0');
oFn.nMaxFile=sizeof(szFile);
oFn.lpstrFileTitle=szFileTitle;
oFn.nMaxFileTitle=sizeof(szFileTitle);
oFn.lStructSize=sizeof(OPENFILENAME);
oFn.nFilterIndex=1;
oFn.hwndOwner=hDlg;
oFn.Flags =OFN_OVERWRITEPROMPT;
oFn.lpstrTitle=TEXT("选择保存位置");
/////////////////////////////////////////////////////////////////
nextname= &ofn->lpstrFile[ofn->nFileOffset];
// MessageBox(hDlg,nextname,"nextname",MB_OK);
iFileNum=GetFileNum(ofn);
sprintf(szFileBuffer,"%d",iFileNum);
// MessageBox(hDlg,szFileBuffer,"iFileNum",MB_OK);
FormatName(ofn,szBuffer);
iPos=lstrlen(szBuffer);
arrName=(ARRFNAME)calloc(1,sizeof(TCHAR*)*iFileNum);
for(i=0;i
{
arrName[i]=(TCHAR*)calloc(1,sizeof(TCHAR)*1024);
}
for(j=0,i=0;j<=(int)strlen(nextname);j++)
{
if(nextname[j]==' '||nextname[j]=='/0')
{
lstrcpyn(arrName[i],&nextname[iFlag],j-iFlag+1);
i++;
iFlag=j+1;
}
}
for(i=0;i
{
lstrcat(szBuffer,arrName[i]);
lstrcpy(arrName[i],szBuffer);
szBuffer[iPos]='/0';
// MessageBox(hDlg,arrName[i],"arrName[i]",MB_OK);
}
if(SelectFile(&oFn,FILE_CUT))
{
if(GetSaveFileName(&oFn)==TRUE)
{
// MessageBox(NULL,oFn.lpstrFile,"szSaveName",MB_OK);
FormatName(&oFn,szSaveName);
// MessageBox(NULL,szSaveName,"szSaveName1",MB_OK);
lstrcat(szSaveName,oFn.lpstrFileTitle);
// MessageBox(NULL,szSaveName,"szSaveName2",MB_OK);
hDesFile=CreateFile(szSaveName,GENERIC_WRITE,FILE_SHARE_READ,
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hDesFile==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONINFORMATION);
return FALSE;
}
}
}
sprintf(szBuffer,"%d",i);
iLength=strlen(szBuffer);
for(i=0;i
{
hFile=CreateFile(arrName[i],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONINFORMATION);
return FALSE;
}
memset(szInt,0,sizeof(szInt));
ReadFile(hFile,szInt,iLength,&iTemp,NULL);
// MessageBox(hDlg,szInt,"j",MB_OK);
j=atoi(szInt);
if(j>iFileNum)
{
MessageBox(hDlg,"ERROR FILE ","message",MB_OK);
return FALSE;
}
else if(j
{
FileSize(hFile,&llFileSizeFirst);
llFileSizeFirst--;
llFileSize=llFileSizeFirst;
// sprintf(szBuffer,"%llu",llFileSize);
// MessageBox(hDlg,szBuffer,"llFileSizeFirst",MB_OK);
SetFilePointer(hDesFile,(int)llFileSize*(j-1),NULL,FILE_BEGIN);
}
else if(j=iFileNum)
{
FileSize(hFile,&llFileSize);
llFileSize--;
// sprintf(szBuffer,"%llu",llFileSize);
// MessageBox(hDlg,szBuffer,"llFileSize",MB_OK);
SetFilePointer(hDesFile,(int)llFileSizeFirst*(j-1),NULL,FILE_BEGIN);
}
SetFilePointer(hFile,iLength,NULL,FILE_BEGIN);
for(k=0;k<=llFileSize/MB;k++)
{
ReadFile(hFile,szFileBuffer,sizeof(char)*MB,&iReadFileNum,NULL);
WriteFile(hDesFile,szFileBuffer,iReadFileNum,&iTemp,NULL);
}
CloseHandle(hFile);
}
CloseHandle(hDesFile);
return TRUE;
}
BOOL CheckStrInt(TCHAR *szBuffer)
{
int i,len;
TCHAR ch[2]={'0','9'};
len=lstrlen(szBuffer);
for(i=0;i
{
if(szBuffer[i]>ch[1]||szBuffer[i]
return FALSE;
}
return TRUE;
}
BOOL ShowInformation(HWND hDlg,OPENFILENAME* ofn,LONGLONG llFileSize)
{
TCHAR FirstInfo[]=TEXT("---欢迎使用文件切割器1.0版---/r/n作者:宛合宝/r/n时间:2010/11/11/r/n");
TCHAR szBuffer[1024];
int iFileNum;
if(!llFileSize)
{
SetDlgItemText(hDlg,IDC_FILEINFO,FirstInfo);
return TRUE;
}
else
{
iFileNum=GetFileNum(ofn);
sprintf(szBuffer,TEXT("文件数:%d/r/n文件名:%s/r/n文件大小:%.2f MB/r/n"),
iFileNum,ofn->lpstrFileTitle,(double)llFileSize/MB);
SetDlgItemText(hDlg,IDC_FILEINFO,szBuffer);
return TRUE;
}
return FALSE;
}
VOID FileSize(HANDLE hFile,LONGLONG* llFileSize)
{
ULARGE_INTEGER ulisize={0};
ulisize.LowPart=GetFileSize(hFile,&ulisize.HighPart);
if(ulisize.LowPart==INVALID_FILE_SIZE&&GetLastError()!=NO_ERROR)
{
MessageBox(NULL,"GetSize error","Msg",MB_OK);
}
(*llFileSize)=((LONGLONG)ulisize.HighPart<<32) |ulisize.LowPart;
}
VOID FormatName(OPENFILENAME* ofn,FILENAME szFileName)
{
TCHAR *szTemp;
TCHAR *p,*q;
int iFlag,i=0;
szTemp=(TCHAR*)calloc(1,sizeof(TCHAR)*512);
//格式化文件名
iFlag=ofn->nFileOffset;
p=ofn->lpstrFile;
q=szTemp;
while(*p&&i++
{
if(*p=='//')
{
*q++='//';
*q++=*p++;
}
else
{
*q++=*p++;
}
}
q='/0';
p=szFileName;
q=szTemp;
while(*p++=*q++);
free(szTemp);
szTemp=NULL;
}
int GetFileNum(OPENFILENAME *ofn)
{
int num=1;
//判断选择文件数目
TCHAR *nextname = ofn->lpstrFileTitle;
while(*nextname)
{
if(*nextname==' ')
{
num++;
nextname++;
}
else
{
nextname++;
}
}
return num;
}
BOOL FileCut(HWND hDlg,FILENAME szFileName)
{
int iCutSize,iBlockNum;
TCHAR szBuffer[2048];
LONGLONG llFileSize,llTemp;
HANDLE hFile;
// OPENFILENAME ofn;
THREADINFO ti;
unsigned long hThread;
//////////////////////////////////////////////////////
hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONINFORMATION);
return FALSE;
}
FileSize(hFile,&llFileSize);
CloseHandle(hFile);
if(!GetDlgItemText(hDlg,IDC_CUTFILESIZE,szBuffer,sizeof(szBuffer)))
{
MessageBox(hDlg,TEXT("GetCutFileSize error"),"msg",MB_OK);
return FALSE;
}
// MessageBox(hDlg,szBuffer,TEXT("提示"),MB_ICONINFORMATION);
if(CheckStrInt(szBuffer))
{
iCutSize=atoi(szBuffer);
iCutSize*=MB;
}
else
{
MessageBox(hDlg,TEXT("输入错误!请重新输入"),TEXT("提示"),MB_ICONINFORMATION);
return 0;
}
iBlockNum=(int)(llFileSize/iCutSize);
llTemp=iBlockNum*iCutSize;
if(llFileSize-llTemp!=0)
{
iBlockNum++;
}
wsprintf(szBuffer,TEXT("被切割的块数为:%d"),iBlockNum);
MessageBox(hDlg,szBuffer,"信息",MB_ICONINFORMATION);
ti.hDlg=hDlg;
ti.iCutSize=iCutSize;
ti.iBlockNum=iBlockNum;
ti.llFileSize=llFileSize;
ti.szFileName=szFileName;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,&ti,0,&hThread);
return TRUE;
}
VOID ThreadProc(THREADINFO* ti)
{
unsigned int iReadFileNum,iTemp;
HWND hDlg;
int iBlockNum,iCutSize;
HANDLE *hDesFile;
HWND hwndPB;
TCHAR ch[6]="a.cut";
TCHAR* szFileName;
unsigned int i,j=0,iFlagPos;
TCHAR* szFileBuffer;
OPENFILENAME ofn;
LONGLONG llFileSize;
HANDLE hFile;
TCHAR szSaveName[1024];
TCHAR szFile[512];
TCHAR szFileTitle[512];
static int pos=0;
DWORD dwStart,dwTemp,dwEnd,dwNeed;
TCHAR szBuffer[256];
long int tmHour,tmMin,tmSec,tmMsec;
long int iSize;
szFileName=ti->szFileName;
llFileSize=ti->llFileSize;
iBlockNum=ti->iBlockNum;
hDlg=ti->hDlg;
iCutSize=ti->iCutSize;
memset(&ofn,0,sizeof(ofn));
ofn.lpstrFilter=TEXT("All File(*.cut)/0*.*/0");
ofn.lpstrFile=szFile;
ofn.lpstrFile[0] = TEXT('/0');
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=szFileTitle;
ofn.nMaxFileTitle=sizeof(szFileTitle);
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.nFilterIndex=1;
ofn.hwndOwner=hDlg;
ofn.Flags =OFN_OVERWRITEPROMPT;
ofn.lpstrTitle=TEXT("选择保存位置");
////////////////////////////////////////////////////////////////////////
hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONINFORMATION);
return ;
}
////////////////////////////////////////////////////////////////////////
hwndPB=GetDlgItem(hDlg,IDC_PROGRESS);
SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, llFileSize/MB));
SendMessage(hwndPB, PBM_SETSTEP, (WPARAM) 1, 0);
////////////////////////////////////////////////////////////////////////
if(GetSaveFileName(&ofn))
{
MessageBox(NULL,ofn.lpstrFile,"szFileName",MB_OK);
FormatName(&ofn,szSaveName);
// MessageBox(NULL,szSaveName,"szSaveName1",MB_OK);
lstrcat(szSaveName,ofn.lpstrFileTitle);
// MessageBox(NULL,szSaveName,"szSaveName2",MB_OK);
iFlagPos=lstrlen(szSaveName);
szFileBuffer=(char*)calloc(1,sizeof(char)*MB);
hDesFile=(HANDLE*)calloc(1,sizeof(HANDLE)*iBlockNum);
dwStart=GetTickCount();
iSize=iBlockNum*(iCutSize/MB)-1;
while(iBlockNum--)
{
ch[0]='a'+j++;
szSaveName[iFlagPos]='/0';
lstrcat(szSaveName,ch);
hDesFile[iBlockNum]=CreateFile(szSaveName,GENERIC_WRITE,FILE_SHARE_READ,
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hDesFile[iBlockNum]==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,TEXT("OpenFile Error"),TEXT("ERROR"),MB_ICONINFORMATION);
return ;
}
sprintf(szFileBuffer,"%d",j);
WriteFile(hDesFile[iBlockNum],szFileBuffer,lstrlen(szFileBuffer),&iTemp,NULL);
//SetFilePointer(hFile,0,NULL,FILE_BEGIN);
for(i=0;i
{
dwTemp=GetTickCount();
ReadFile(hFile,szFileBuffer,sizeof(char)*MB,&iReadFileNum,NULL);
WriteFile(hDesFile[iBlockNum],szFileBuffer,iReadFileNum,&iTemp,NULL);
SendMessage(hwndPB, PBM_SETPOS, (WPARAM)(++pos), 0);
dwEnd=GetTickCount();
dwNeed =(iSize--)*(dwEnd-dwTemp);
tmHour=dwNeed/(100*60*60);
tmMin=(dwNeed-tmHour*100*60*60)/(100*60);
tmSec=(dwNeed-tmHour*100*60*60-tmMin*100*60)/100;
tmMsec=dwNeed-tmHour*100*60*60-tmMin*100*60-tmSec*100;
sprintf(szBuffer,"%2d:%2d:%2d",tmMin,tmSec,tmMsec);
SetDlgItemText(hDlg,IDC_TIME,szBuffer);
}
CloseHandle(hDesFile[iBlockNum]);
}
dwNeed =GetTickCount()-dwStart;
tmHour=dwNeed/(100*60*60);
tmMin=(dwNeed-tmHour*100*60*60)/(100*60);
tmSec=(dwNeed-tmHour*100*60*60-tmMin*100*60)/100;
tmMsec=dwNeed-tmHour*100*60*60-tmMin*100*60-tmSec*100;
sprintf(szBuffer,"%2d:%2d:%2d",tmMin,tmSec,tmMsec);
SetDlgItemText(hDlg,IDC_TIME,szBuffer);
MessageBox(hDlg,TEXT("切割成功,请关闭窗口!"),TEXT("提示"),MB_ICONINFORMATION);
}
CloseHandle(hFile);
ExitThread(0);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileCutMain.c
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include"FileCut.h"
#include"resource.h"
BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int nShowCmd)
{
MSG msg;
HWND hDlg;
hDlg=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DLGMAIN),NULL,DlgProc);
// ShowWindow(hDlg,nShowCmd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL CALLBACK DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static OPENFILENAME ofn;
static TCHAR szFile[512];
static TCHAR szFileTitle[512];
static TCHAR szFileName[2018];
INITCOMMONCONTROLSEX ict;
// LONGLONG llFileSize;
static HWND hwndPB;
ict.dwICC=ICC_PROGRESS_CLASS;
ict.dwSize=sizeof(ict);
memset(&ofn,0,sizeof(ofn));
ofn.lpstrFilter=TEXT("All File(*.*)/0*.*/0");
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=szFileTitle;
ofn.nMaxFileTitle=sizeof(szFileTitle);
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.nFilterIndex=1;
ofn.hwndOwner=hDlg;
switch(uMsg)
{
case WM_INITDIALOG:
ShowInformation(hDlg,NULL,0);
// InitCommonControlsEx(&ict);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_CUTVIEW:
{
memset(szFileName,0,sizeof(szFileName));
if(SelectFile(&ofn,FILE_CUT))
{
if(GetOpenFileName(&ofn)==TRUE)
{
if(CutFileView(hDlg,&ofn,szFileName))
{
MessageBox(hDlg,TEXT("请输入切割文件大小"),TEXT("提示:"),MB_ICONINFORMATION);
return TRUE;
}
else
{
MessageBox(hDlg,TEXT("FILE CUT ERROR"),TEXT("ERROR"),MB_ICONERROR);
return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
MessageBox(hDlg,TEXT("Can't SelectFile!"),TEXT("MSG"),MB_ICONERROR);
return FALSE;
}
}
return FALSE;
case IDC_FILECOM:
{
memset(szFileName,0,sizeof(szFileName));
if(SelectFile(&ofn,FILE_COM))
{
if(GetOpenFileName(&ofn)==TRUE)
{
if(FileCom(hDlg,&ofn))
{
MessageBox(hDlg,TEXT("FILECOM OK!"),TEXT("FILECOM"),MB_OK|MB_ICONINFORMATION);
return TRUE;
}
else
return FALSE;
}
else
{
return FALSE;
}
}
else
{
MessageBox(hDlg,TEXT("Can't SelectFile!"),TEXT("MSG"),MB_ICONERROR);
return FALSE;
}
}
return TRUE;
case IDC_FILECUT:
if(FileCut(hDlg,szFileName))
{
return TRUE;
}
return FALSE;
default:
return FALSE;
}
case WM_CLOSE:
PostQuitMessage(0);
DestroyWindow(hDlg);
hDlg=NULL;
return TRUE;
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileCut.h
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef FILECUT_H
#ifndef MB__
#define MB__
#define MB (1024*1024*sizeof(char))
#endif //endif MB__
#include
#include
#include
#include
#include
#include
#include
#include
#define FILE_CUT 1
#define FILE_COM 2
#define TH_THREADOVER WM_USER+10
typedef TCHAR* FILENAME;
typedef FILENAME* ARRFNAME;
typedef struct
{
HWND hDlg;
int iCutSize;
int iBlockNum;
LONGLONG llFileSize;
TCHAR* szFileName;
}THREADINFO;
extern int GetFileNum(OPENFILENAME *ofn);
extern BOOL CheckStrInt(TCHAR* szBuffer);
extern BOOL SelectFile(OPENFILENAME* ofn,int Mode);
extern BOOL CutFileView(HWND hDlg,OPENFILENAME* ofn,FILENAME szFileName);
extern BOOL FileCom(HWND hDlg,OPENFILENAME *ofn);
extern VOID FileSize(HANDLE hFile,LONGLONG* llFileSize);
extern VOID FormatName(OPENFILENAME *ofn,FILENAME szFileName);
extern BOOL ShowInformation(HWND hDlg,OPENFILENAME *ofn,LONGLONG FileSize);
extern BOOL FileCut(HWND hDlg,FILENAME szFileName);
extern VOID ThreadProc(THREADINFO *ThreadInfo);
#endif