因家里不能上网了,所以这个程序的源代码我在网吧直接打上的.其中可能会打错.(打的我手都快断了!)
先建一个对话框应用程序命名为Res.再建一个名为StaticEx的基类为CStatic的静态类。在这个类加一个WM_PAINT的消息。在对话框上放一进程条,四个静态控件,两个按钮。其中两个静态控件ID分别为IDC_CURRENT_FILE用来显示压缩图象的目录;另一个ID为IDC_NUMBER用来显示压缩文件的个数.两个按钮的ID分别为ID_CHOOSE与ID_BALE,分别为这两个静态控件加一个变量。类型都为CStaticEx的。前一个用来选择图片的目录,后一个用来开始压缩.为进程条加一变量m_Progress类型为CProgressCtrl,还有程序中所有末定义的变量为全局变量.以下是源代码.
以下为全局变量:
(注意:这些全局变量我可能记错或者少写了)
CString num,cc,dirna;
BYTE*buf;
BYTE*tmp;
int bufadd[4000];
int len,pop,shulian;
///////////////////////////////////////////
CStaticEx的OnPaint()的代码如下:
void CStaticEx::OnPaint()
{
CPaintDC dc(this);
CString strText;
GetWindowText(strText);
Rectangle(dc,m_ctlRect.left-1,m_ctlRect.top-1,m_ctlRect.right+1,m_ctlRect.bottom+1);
dc.DrawText((LPCSTR)strText,strText.GetLength(),&m_ctlRect,
DT_PATH_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
}
其中m_ctlRect为CStaticEx的一个RECT类型的公有变量.
对话框类的OnInitDialog()的代码如下:
(注意:这是部分的)
RECT rc={0,0,0,0};
CWnd *cwStaticEx,*cwStaticEx;
if((cwStaticEx=GetDlgItem(IDC_CURRENT_FILE)!=NULL)
{
cwStaticEx->GetWindowRect(&rc);
rc.right=rc.right-rc.left;
rc.bottom=rc.bottom-rc.top;
rc.top=0;
rc.left=0;
m_ctlCurrentFile.m_ctlRect=rc;
}
if((cwStaticEx=GetDlgItem(IDC_NUMBER)!=NULL)
{
cwStaticEx->GetWindowRect(&rc);
rc.right=rc.right-rc.left;
rc.bottom=rc.bottom-rc.top;
rc.top=0;
rc.left=0;
m_ctlCurrentFile.m_ctlRect=rc;
}
m_Progress.SetRange(0,200);
m_Progress.SetPos(0);
m_Progress.SetStep(1);
////////////////////////////////////////
void CRes::opendir()
{
BROWSEINFO * m_pbi;
char m_buffer[MAX_PATH];
m_pbi=NULL;
m_pbi=new BROWSEINFO();
::ZeroMemory(m_pbi,sizeof(BROWSEINFO));
m_pbi->hwndOwner=GetSafeHwnd();
m_pbi->pszDisplayName=m_buffer;
m_pbi->ulFlags=BIF_EDITBOX;
ITEMIDLIST *id1=SHBrowseForFolder(m_pbi);
if(id1)
{SHGetPathFromIDList(id1,m_buffer);
dirna=m_buffer;
LPMALLOC lpm;
if(SHGetMalloc(&lpm)==NOERROR) lpm->Free(id1);
shulian=FindFile(dirna,"*.bmp");
}
delete[] m_pbi;
m_ctlCurrentFile.SetWindowText((LPCSTR)dirna);
num.Format("%d",shulian);
m_ctlNumber.SetWindowText((LPCSTR)num);
m_ctlCurrentFile.Invalidate(TRUE);
m_ctlNumber.Invalidate(TRUE);
}
void CRes::OnChoose()
{
opendir();
SetWindowText(dirna);
}
void CRes::OnBale()
{
if(atoi(num)==o)
{AfxMessageBox("没有图片数据!",MB_ICONHAND);
return;
}
CClientDC dc(this);
CString na;
na=dirna.Right(2);
len=0;
UINT nPos;
buf=(BYTE*) new BYTE[15*1024*1024];
tmp=(BYTE*) new BYTE[55000];
bufadd[1]=0;
m_Progress.SetRange(0,shulian);
m_Pos=m_Progress.GetPos();
for(int i=0;i<shulian;i++)
{
cc.Format("%s/c%05d.bmp",dirna,i);
LoadBmp(dc.m_hDC,cc,i);
SetWindowText(cc);
m_Progress.SetIt();
nPos=m_Progress.GetPos();
}
pop=i;
cc.Format("../%s.Bjarne",na);
CFile file;
if(!file.Open(cc,CFile::modeCreate|CFile::modeWrite,NULL) return;
file.Write(buf,len);
file.Close();
AfxMessageBox("完成压缩");
}
void LoadBmp(HDC hdc, CString fileName,int pop)
{
LPBITMAPINFOHEADER bi1,m_lpBi;
HANDLE m_Buf=NULL;
LPVOID lpmem=NULL;
CFile file;
if(!file.Open(fileName,CFile::modeRead)) return;
BITMAPFILEHEADER fileinfo;
file.Read(&fileinfo,sizeof(fileinfo));
if(fileinfo.bfType!=(('M'<<8)+'B')) return;
UINT m_off=fileinfo.bfoffBits;
UINT length=file.GetLength()-sizeof(BITMAPFILEHEANDER);
if(m_Buf!=NULL) {GlobalFree(m_Buf);m_Buf=NULL;}
m_Buf=GlobalAlloc(GMEM_MOVEABLE|GMEM_DISCARDABLE,length);
lpmem =GlobalLock(m_Buf);
//如果你是用VC6.0用ReadHuage如是.NET用回这个
if(length!=file.Read(lpmem,length))
{GlobalUnlock(m_Buf);
GlobalFree(m_Buf);
return;
}
bi1=(BITMAPINFOHEADER *)lpmem;
bi1->biSize=sizeof(BITMAPINFOHEADER);
bi1->biSizeImage=bil->biWidth*bi1->biHeight;
int cols=PaletteSize((LPBYTE) bil);
int len0=bil->biSize+bil->biSizeImage+cols;
if(bil->biSizeImage<10) return;
//压缩图片
m_lpBi=icmode(bi1);
bi1=(BITMAPINFOHEADER*) m_lpBi;
bil->biSize=sizeof(BITMAPINFOHEADER);
int len1=bil->biSize+bi1->biSizeImage+cols;
memcpy(buf+bufadd[pop],bi1,len1);
len+=len1;bufadd[pop+1]=len;
GlobalUnlock(m_Buf);GlobalFree(m_Buf);
cc.Format("%d:%d-%d %d",pop,len0,len1,len);
}
LPBITMAPINFOHEADER icmode(LPBITMAPINFOHEADER bi1)
{HIC hIC;
if(bi1==NULL) {AfxMessageBox("无图片数据");
return NULL;}
int cols=PaletteSize((LPBYTE) bi1);
LPBITMAPINFOHEADER m_lpBi=bi1;
if(bi1->biBitCount<=8)
hIC=ICOpen(mmioFOURCC('V','I','D','C'),mmioFOURCC('m','r','l','e'),
ICMODE_FASTCOMPRESS);
else
hIC=ICOpen(mmioFOURCC('V','I','D','C'),mmioFOURCC('m','s','v','c'),
ICMODE_FASTCOMPRESS);
HANDLE hDID=ICImageCompress(hIC,0,(LPBITMAPINFO) bi1,(LPBYTE)bi1+(WORD)bi1->biSize+cols,
NULL,6500,NULL);
LPBITMAPINFOHEADER lpBI1=(LPBITMAPINFOHEADER)GlobalLock(hDID);
memcpy((LPBYTE)m_lpBi,(LPBYTE)lpBI1,
lpBI1->biSize+lpBI1->biSizeImage+PaletteSize((LPBYTE)lpBI1);
ICClose(hIC);
GlobalUnlock(hDID;
GlobalFree(hDID);
return m_lpBi;
}
WORD PaletteSize(LPBYTE lpDIB)
{WORD wBitCount;
DWORD dwClrUsed;
dwClrUsed=((LPBITMAPINFOHEADER)lpDIB)->biCrlUsed;
if(dwClrUsed)
return (WORD) dwClrUsed*sizeof(RGBQUAD);
wBitCount=((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
switch(wBitCount)
{
case 1: return 2*sizeof(RGBQUAD);
case 4: return 16*sizeof(RGBQUAD);
case 8: return 256*sizeof(RGBQUAD);
default: return 0;
}
}
int FindFile(CString DirName,CString ext)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFindFile;
SetCurrentDirectory(DirName);
hFindFile=FinlFirstFile(ext,&FindFileData);
CString tFile;
int p=0;
if(hFindFile!=INVALID_HANDLE_VALVE)
{do{tFile=FindFileData.FileName;
if((tFile==".")||(tFile=="..")) contiune;
if(!(FindFileData.dwFileAttributes==FILE_ATTRIBUE_DIRECTORY))
{
p++;
}
while(FindNextFile(hFindFile,&FindFileData));
}
return p;
}
[此贴子已经被作者于2005-12-15 15:52:11编辑过]