📄 imagecamdoc.cpp
字号:
nNumColors = 1 << lpbi->biBitCount; // standard size table
}
// fill in some default values if they are zero
if (lpbi->biClrUsed == 0)
lpbi->biClrUsed = nNumColors;
if (lpbi->biSizeImage == 0)
lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) + 31) & ~31) >> 3) * lpbi->biHeight;
}
CATCH (CException, e)
{
GlobalFreePtr(lpbi);
WaitCursorEnd();
return FALSE;
}
END_CATCH
// create CDib with DIB buffer
m_pDib->Destroy();
BOOL bSuccess=m_pDib->Create((LPBYTE)lpbi);
GlobalFreePtr(lpbi);
WaitCursorEnd();
pFile.Close();
return bSuccess;
}
BOOL CimageCAMDoc::WriteTmpFile()//int i,CFile *pFile,CDib* m_pDibPar) //功能:判断是否需要创建临时文件的函数
{
if(!SetCuurentDirectory())
return FALSE;
isundodone=FALSE;
if(!WriteFile())
return FALSE;
tfilestr[fileindex%4].isfilerecover=FALSE;
fileindex=fileindex+1;
return TRUE;
}
BOOL CimageCAMDoc::SetCuurentDirectory()
{
if(setszCurDir.IsEmpty())
{ AfxMessageBox("无法获得当前的程序目录,系统异常!!");}
//setszCurDir="c:\\imageCAMtamp";
if(::SetCurrentDirectory(setszCurDir))
return TRUE;
else
{
SECURITY_ATTRIBUTES *sec_attr=new SECURITY_ATTRIBUTES;
sec_attr->nLength=sizeof(SECURITY_ATTRIBUTES);
sec_attr->lpSecurityDescriptor=NULL;
sec_attr->bInheritHandle=TRUE;
if(::CreateDirectory(setszCurDir,sec_attr))
return TRUE;
else
{
CString str;
str.Format("无%s目录;请关闭系统,创建此目录!!",setszCurDir);
AfxMessageBox(str);
return FALSE;}
}
return TRUE;
}
void CimageCAMDoc::OnEditUndo()
{
if(m_pDib!=NULL)
{
EditUndo();
isundodone=TRUE;
CMDIFrameWnd *pFrame =(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// Get the active MDI child window.
CMDIChildWnd *pChild =
(CMDIChildWnd *) pFrame->GetActiveFrame();
// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
// Get the active view attached to the active MDI child// window.
CimageCAMView *pView = (CimageCAMView *) pChild->GetActiveView();
::SendMessage(pView->m_hWnd,WM_REALIZEPAL,(WPARAM)pView->m_hWnd,0);// realize the new palette
UpdateAllViews(NULL);
}
}
void CimageCAMDoc::OnEditRedo()
{
tfilestr[fileindex%4].isfilerecover=FALSE;
fileindex=fileindex+1;//前移一位,以获取文件
// tfilestr[fileindex%4].isfilerecover=FALSE;
ReadFile();
m_pDib->UpdateInternal();
SetModifiedFlag(TRUE);
CMDIFrameWnd *pFrame =(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// Get the active MDI child window.
CMDIChildWnd *pChild =
(CMDIChildWnd *) pFrame->GetActiveFrame();
// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
// Get the active view attached to the active MDI child// window.
CimageCAMView *pView = (CimageCAMView *) pChild->GetActiveView();
::SendMessage(pView->m_hWnd,WM_REALIZEPAL,(WPARAM)pView->m_hWnd,0);// realize the new palette
UpdateAllViews(NULL);
}
void CimageCAMDoc::OnUpdateEditRedo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if((isundodone)&&(tfilestr[(fileindex+1)%4].isfilerecover==TRUE))
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
BOOL CimageCAMDoc::WriteFile()
{
WaitCursorBegin();
CString tfilename=tfilestr[fileindex%4].FileName;
CFile pFile=tfilestr[fileindex%4].Fundo;
BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
LPBITMAPINFOHEADER lpBI; // Pointer to DIB info structure
DWORD dwDIBSize;
HDIB hNewDib=NULL;
hNewDib = CopyHandle(m_pDib->m_hDib);
if (! hNewDib)
{
WaitCursorEnd();
return FALSE;
}
// Get a pointer to the DIB memory, the first of which contains
// a BITMAPINFO structure
lpBI = (LPBITMAPINFOHEADER)GlobalLock(hNewDib);
if (!lpBI)
{
GlobalUnlock(hNewDib);
WaitCursorEnd();
return FALSE;
}
// Check to see if we're dealing with an OS/2 DIB. If so, don't
// save it because our functions aren't written to deal with these
// DIBs.
if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
{
GlobalUnlock(hNewDib);
WaitCursorEnd();
return FALSE;
}
// Fill in the fields of the file header
// Fill in file type (first 2 bytes must be "BM" for a bitmap)
bmfHdr.bfType = DIB_HEADER_MARKER; // "BM"
// Calculating the size of the DIB is a bit tricky (if we want to
// do it right). The easiest way to do this is to call GlobalSize()
// on our global handle, but since the size of our global memory may have
// been padded a few bytes, we may end up writing out a few too
// many bytes to the file (which may cause problems with some apps,
// like HC 3.0).
//
// So, instead let's calculate the size manually.
//
// To do this, find size of header plus size of color table. Since the
// first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER conains
// the size of the structure, let's use this.
// Partial Calculation
dwDIBSize = *(LPDWORD)lpBI + PaletteSize((LPBYTE)lpBI);
// Now calculate the size of the image
// It's an RLE bitmap, we can't calculate size, so trust the biSizeImage
// field
if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4))
dwDIBSize += lpBI->biSizeImage;
else
{
DWORD dwBmBitsSize; // Size of Bitmap Bits only
// It's not RLE, so size is Width (DWORD aligned) * Height
dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) *
lpBI->biHeight;
dwDIBSize += dwBmBitsSize;
// Now, since we have calculated the correct size, why don't we
// fill in the biSizeImage field (this will fix any .BMP files which
// have this field incorrect).
lpBI->biSizeImage = dwBmBitsSize;
}
// Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
// Now, calculate the offset the actual bitmap bits will be in
// the file -- It's the Bitmap file header plus the DIB header,
// plus the size of the color table.
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize +
PaletteSize((LPBYTE)lpBI);
TRY
{
pFile.Open(tfilename,CFile::modeReadWrite);
}
CATCH (CException, e)
{
WaitCursorEnd();
return FALSE;
}
END_CATCH
TRY
{
// Write the file header
pFile.Write(&bmfHdr, sizeof(BITMAPFILEHEADER));
// write DIB buffer
pFile.WriteHuge(lpBI, dwDIBSize);
}
CATCH (CException, e)
{
GlobalUnlock(hNewDib);
GlobalFree(hNewDib);
WaitCursorEnd();
return FALSE;
}
END_CATCH
GlobalUnlock(hNewDib);
GlobalFree(hNewDib);
pFile.Close();
WaitCursorEnd();
return TRUE;
}
void CimageCAMDoc::OnFileSaveAs()
{
// TODO: Add your command handler code here
CPreviewFileDlg FileDlg(FALSE, "*.bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szSaveFilter);
switch (m_pDib->GetBitCount())
{
case 1:
FileDlg.m_ofn.nFilterIndex = 1;
break;
case 4:
FileDlg.m_ofn.nFilterIndex = 2;
break;
case 8:
FileDlg.m_ofn.nFilterIndex = 3;
break;
case 24:
default:
FileDlg.m_ofn.nFilterIndex = 4;
break;
}
if (FileDlg.DoModal() == IDOK)
{
switch (FileDlg.m_ofn.nFilterIndex)
{
case 1:
m_pDib->ConvertFormat(1);
break;
case 2:
m_pDib->ConvertFormat(4);
break;
case 3:
m_pDib->ConvertFormat(8);
break;
case 4:
default:
m_pDib->ConvertFormat(24);
break;
}
OnSaveDocument(FileDlg.m_ofn.lpstrFile);
SetPathName(FileDlg.m_ofn.lpstrFile);
UpdateAllViews(NULL);
SetModifiedFlag(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -