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

📄 scanviewerdoc.cpp

📁 看图软件
💻 CPP
字号:
// ScanViewerDoc.cpp :  CScanViewerDoc 类的实现
//

#include "stdafx.h"
#include "ScanViewer.h"

#include "ScanViewerDoc.h"
#include ".\scanviewerdoc.h"

#include <comdef.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



// CScanViewerDoc

IMPLEMENT_DYNCREATE(CScanViewerDoc, CDocument)

BEGIN_MESSAGE_MAP(CScanViewerDoc, CDocument)
	
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_TOOLS_FILE_ASSOCIATIONS, OnToolsFileAssociations)
	ON_UPDATE_COMMAND_UI(ID_TOOLS_FILE_ASSOCIATIONS, OnUpdateToolsFileAssociations)	
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_TOOLS_FOLDER_ASSOCIATIONS, OnToolsFolderAssociations)
	ON_UPDATE_COMMAND_UI(ID_TOOLS_FOLDER_ASSOCIATIONS, OnUpdateToolsFolderAssociations)
END_MESSAGE_MAP()


// CScanViewerDoc 构造/析构

CScanViewerDoc::CScanViewerDoc()
: CCMenu("Open in ScanViewer","Open Folder in ScanViewer",".jpg")
, m_bIsFolder(FALSE)
{
	// TODO: 在此添加一次性构造代码	

}

CScanViewerDoc::~CScanViewerDoc()
{
}

BOOL CScanViewerDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)

	return TRUE;
}




// CScanViewerDoc 序列化

void CScanViewerDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
	}
	else
	{
		// TODO: 在此添加加载代码
	}
}


// CScanViewerDoc 诊断

#ifdef _DEBUG
void CScanViewerDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CScanViewerDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CScanViewerDoc 命令



//idcomcn2007
//0:53 2007-12-12
//打开现有文档\n打开
BOOL CScanViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	//检查是否为目录
	if (PathIsDirectory(lpszPathName))        
	{		
		m_bIsFolder=TRUE;		
		return TRUE;
	}

	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// TODO:  在此添加您专用的创建代码	

	return TRUE;
}



//idcomcn2008
//15:02 2008-2-21
//New document
void CScanViewerDoc::OnFileNew()
{
	// TODO: 在此添加命令处理程序代码
	m_bIsFolder=FALSE;
	this->OnNewDocument();		
	UpdateAllViews(NULL);           		
}



//idcomcn2007
//15:21 2007-12-24
//用新名称保存活动文档\n另存为
void CScanViewerDoc::OnFileSaveAs()
{
	// TODO: 在此添加命令处理程序代码
	CString strPathName=GetPathName();
	if (strPathName.IsEmpty()) return;

	CImage m_Image;
	HRESULT hResultOpen = m_Image.Load(GetPathName());	
	if (FAILED(hResultOpen)) 
	{
		if (!m_Image.IsNull())	m_Image.Destroy();	
		AfxMessageBox("调用图像文件失败!");
		m_Image.Destroy();
		return;
	}
	
	CString strFilter;
	strFilter = "Bitmap (*.bmp;*.dib)|*.BMP;*.DIB|JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|GIF (*.GIF)|*.GIF|TIFF (*.TIF;*.TIFF)|*.TIF;*.TIFF|PNG (*.PNG)|*.PNG||";	
	CFileDialog dlg(FALSE,NULL,strPathName,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,strFilter);

	
	//指定扩展名
	CString strFileName=strPathName;
	DWORD nFilterLoad=1;
	CString strEN=(strFileName.Mid(strFileName.ReverseFind('.'))).MakeLower();
	if ((StrCmp(strEN,".bmp")==0)||StrCmp(strEN,".dib")==0) nFilterLoad=1;
	if (StrCmp(strEN,".jfif")==0||StrCmp(strEN.Left(3),".jp")==0) nFilterLoad=2;
	if (StrCmp(strEN,".gif")==0) nFilterLoad=3;
	if ((StrCmp(strEN,".tif")==0)||StrCmp(strEN,".tiff")==0) nFilterLoad=4;
	if (StrCmp(strEN,".png")==0) nFilterLoad=5;
	dlg.m_pOFN->nFilterIndex=nFilterLoad;

	HRESULT hResult = (int)dlg.DoModal();
	if (hResult != IDOK) 
	{
		if (!m_Image.IsNull())	m_Image.Destroy();	
		return;
	}

    // Add the appropriate extension if the user didn't type one	
	CString strExtension;
	strFileName = dlg.m_ofn.lpstrFile;
	switch (dlg.m_ofn.nFilterIndex)
	{
	case 1:
		strExtension = "bmp";
		break;
	case 2:
		strExtension = "jpg";
		break;
	case 3:
		strExtension = "gif";
		break;
	case 4:
		strExtension = "tif";
		break;
	case 5:
		strExtension = "png";
		break;
	default:
		break;
	}
	strFileName=strFileName.Left(strFileName.ReverseFind('\\')+1);
	strFileName=strFileName+dlg.GetFileTitle()+"."+strExtension;


	//检查文件在磁盘上是否存在
	CFileStatus Status;
	if (CFile::GetStatus(strFileName,Status))
	{
		int iSelectOKOKCANCEL=AfxMessageBox(strFileName+"已存在。\n要替换它吗?",MB_YESNO|MB_ICONSTOP);	
		if (iSelectOKOKCANCEL==IDNO) 
		{
			if (!m_Image.IsNull())	m_Image.Destroy();	
			return;
		}
	}

	// the extension on the file name will determine the file type that is saved	
	hResult = m_Image.Save(strFileName);
	if (FAILED(hResult)) 
	{
		if (!m_Image.IsNull())	m_Image.Destroy();	
		CString fmt;
		fmt.Format("保存图像文件失败:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
		AfxMessageBox(fmt);
		return;
	}
	if (!m_Image.IsNull())	m_Image.Destroy();	
}




//idcomcn2008
//8:45 2008-4-25
void CScanViewerDoc::OnUpdateEditPaste(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	BOOL bBitmap=IsClipboardFormatAvailable(CF_BITMAP);
	pCmdUI->Enable(bBitmap);
}

//idcomcn2008
//16:44 2008-4-23
//保存剪贴板中的位图到文件
void CScanViewerDoc::OnEditPaste()
{
	// TODO: 在此添加命令处理程序代码
	if (!IsClipboardFormatAvailable(CF_BITMAP)) 	return; 
	
	HBITMAP    hBitmap;	
	if (OpenClipboard(NULL)) 
	{
		hBitmap=HBITMAP(GetClipboardData(CF_BITMAP));
		//关闭剪贴板
		CloseClipboard();
	}
	else return;

	char *path=new char[MAX_PATH];
	GetTempPath(MAX_PATH,path);
	CString strPath;
	strPath.Format("%sMyPrint.bmp",path);	
	delete [] path;
    
	CImage cimg;                                                     //create image goes here
	cimg.Attach(hBitmap);	
	HRESULT hResult=cimg.Save(strPath);                              //保存目标位图
	DeleteObject(hBitmap);	
	cimg.Destroy();

	if (SUCCEEDED(hResult)) 
	{		
		AfxGetApp()->OpenDocumentFile(strPath);
		UpdateAllViews(NULL); 
	}
}



//更新文件关联
void CScanViewerDoc::OnUpdateToolsFileAssociations(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	if (CCMenu.IsNullForFile()) pCmdUI->SetCheck(0);
	else pCmdUI->SetCheck(1);
}


//idcomcn2008
//1:02 2008-4-10
//Add and delete ContextMenu for file
void CScanViewerDoc::OnToolsFileAssociations()
{
	// TODO: 在此添加命令处理程序代码	
	TCHAR cImgExtName[5][5]={".bmp",".jpg",".gif",".tif",".png"};
	if (CCMenu.IsNullForFile())
	{
		///////////////////////////////////////////////////////////////////Add ContextMenu//////////////////////////////////////////////////////////////////////////		
		for (int ia=0;ia<5;ia++)
		{
			//AfxMessageBox(cImgExtName[ia]);
			CCMenu.AddContextMenuForFile(cImgExtName[ia]);
		}
		///////////////////////////////////////////////////////////////////Add ContextMenu//////////////////////////////////////////////////////////////////////////
	}
	else
	{
		int iSelectOKOKCANCEL;
		iSelectOKOKCANCEL=AfxMessageBox("delete Associations File?",MB_YESNO|MB_ICONINFORMATION);
		if (iSelectOKOKCANCEL==IDYES) 
		{
			///////////////////////////////////////////////////////////////////delete ContextMenu//////////////////////////////////////////////////////////////////////////
			for (int ia=0;ia<5;ia++)
			{
				//AfxMessageBox(cImgExtName[ia]);
				CCMenu.DelContextMenuForFile(cImgExtName[ia]);
			}
			///////////////////////////////////////////////////////////////////delete ContextMenu//////////////////////////////////////////////////////////////////////////
		}			
	}
}



//更新文件夹关联
void CScanViewerDoc::OnUpdateToolsFolderAssociations(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	if (CCMenu.IsNullForFolder()) pCmdUI->SetCheck(0);
	else pCmdUI->SetCheck(1);
}



//idcomcn2008
//1:02 2008-4-10
//Add and delete ContextMenu for folder
void CScanViewerDoc::OnToolsFolderAssociations()
{
	// TODO: 在此添加命令处理程序代码
	if (CCMenu.IsNullForFolder())
	{
		///////////////////////////////////////////////////////////////////Add ContextMenu//////////////////////////////////////////////////////////////////////////		

		CCMenu.AddContextMenuForFolder();

		///////////////////////////////////////////////////////////////////Add ContextMenu//////////////////////////////////////////////////////////////////////////
	}
	else
	{
		int iSelectOKOKCANCEL;
		iSelectOKOKCANCEL=AfxMessageBox("delete Associations Folder?",MB_YESNO|MB_ICONINFORMATION);
		if (iSelectOKOKCANCEL==IDYES) 
		{
			///////////////////////////////////////////////////////////////////delete ContextMenu//////////////////////////////////////////////////////////////////////////
	
			CCMenu.DelContextMenuForFolder();

			///////////////////////////////////////////////////////////////////delete ContextMenu//////////////////////////////////////////////////////////////////////////
		}			
	}
}

⌨️ 快捷键说明

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