📄 workspacehelper.cpp
字号:
/************************************************************************
*
* Resource ID Organiser Add-In
*
* (c) Copyright 2000 by Andy Metcalfe (andy.metcalfe@lineone.net)
* All rights reserved.
*
************************************************************************
*
* Filename : WorkspaceHelper.cpp
*
* Description : CWorkspaceHelper - Visual Studio COM interface wrapper class
*
* Compiler : Microsoft Visual C++ 6.0, Service Pack 3 or 4
*
* Target
* Environment : Windows 98/NT
*
* NOTE:
*
* This software is provided "as is" free for personal use. All
* title and copyrights in and to the software, including but not
* limited to any images, text, etc. incorporated into it, are
* owned by Andy Metcalfe, except where acknowledged otherwise.
*
* Your may freely to use this code in your own products, PROVIDED
* this notice is not removed or modified.
*
*
* Visit http://www.resorg.co.uk for latest updates
*
************************************************************************
*
* MODIFICATION HISTORY:
*
* This is a controlled document. See project configuration
* control tool for latest version and full version history.
*
* $Archive: /Projects/AddIns/ResOrg/ResOrgAddIn/WorkspaceHelper.cpp $
* $Revision: 3 $
* $Date: 2/03/01 13:14 $
* $Author: Andy $
*
* $History: WorkspaceHelper.cpp $
*
* ***************** Version 3 *****************
* User: Andy Date: 2/03/01 Time: 13:14
* Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
* Removed CWorkspaceHelper::GetSymbolFileName() [now a global function in
* ResOrgUtils]
*
* ***************** Version 2 *****************
* User: Andy Date: 29/11/00 Time: 18:37
* Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
* 1. Moved Office2KDlg code to its own DLL
* 2. Added file banners
*
* $Nokeywords: $
*
************************************************************************/
// WorkspaceHelper.cpp : implementation file
//
#include "StdAfx.h"
#include <comdef.h>
#include "WorkspaceHelper.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper
CWorkspaceHelper::CWorkspaceHelper(IApplication* pApplication /*= NULL*/)
{
m_pApplication = NULL;
SetApplication(pApplication);
}
CWorkspaceHelper::~CWorkspaceHelper(void)
{
if (NULL != m_pApplication)
{
SetApplication(NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper operations
BOOL CWorkspaceHelper::SetApplication(IApplication* pApplication)
{
if (NULL != m_pApplication)
{
m_pApplication->Release();
m_pApplication = NULL;
}
if (pApplication != NULL)
{
m_pApplication = pApplication;
m_pApplication->AddRef();
}
return (m_pApplication != NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper Project operations
int CWorkspaceHelper::GetProjectCount(void) const
{
long nProjects = 0;
IProjects* pProjects = NULL;
if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
{
pProjects->get_Count(&nProjects);
pProjects->Release();
}
return (int)nProjects;
}
int CWorkspaceHelper::GetProjectNames(CStringArray& rarrayNames) const
{
rarrayNames.RemoveAll();
CProjectArray arrayProjects;
GetProjects(arrayProjects);
for (int n = 0; n < arrayProjects.GetSize(); n++)
{
IGenericProject* pProject = arrayProjects[n];
if (NULL != pProject)
{
rarrayNames.Add( GetFullName(pProject) );
pProject->Release();
}
}
return rarrayNames.GetSize();
}
int CWorkspaceHelper::GetProjects(CProjectArray& rarrayProjects) const
{
IProjects* pProjects = NULL;
if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
{
int nProjects = GetProjectCount();
for (int n = 1; n <= nProjects; n++) // This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
{
IGenericProject* pProject = NULL;
_variant_t Index( (long)n);
pProjects->Item(Index, &pProject);
if (NULL != pProject)
{
rarrayProjects.Add(pProject);
}
}
pProjects->Release();
}
return rarrayProjects.GetSize();
}
IGenericProject* CWorkspaceHelper::GetProject(const CString& sFullName) const
{
USES_CONVERSION;
IProjects* pProjects = NULL;
IGenericProject* pProject = NULL;
if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
{
CComBSTR bsFullName = T2OLE(sFullName);
_variant_t Index(bsFullName);
pProjects->Item(Index, &pProject);
pProjects->Release();
}
return pProject;
}
IGenericProject* CWorkspaceHelper::GetActiveProject(void) const
{
IGenericProject* pActiveProject = NULL;
m_pApplication->get_ActiveProject( (LPDISPATCH*)&pActiveProject);
return pActiveProject;
}
BOOL CWorkspaceHelper::SetActiveProject(IGenericProject* pActiveProject)
{
BOOL bResult = FALSE;
IProjects* pProjects = NULL;
if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
{
if ( SUCCEEDED( m_pApplication->put_ActiveProject( (LPDISPATCH)pActiveProject) ) )
{
bResult = TRUE;
}
}
return bResult;
}
BOOL CWorkspaceHelper::LoadProject(IGenericProject* pProject)
{
UNREFERENCED_PARAMETER(pProject);
BOOL bResult = FALSE;
return bResult;
}
BOOL CWorkspaceHelper::UnloadProject(IGenericProject* pProject)
{
UNREFERENCED_PARAMETER(pProject);
BOOL bResult = FALSE;
return bResult;
}
CString CWorkspaceHelper::GetName(IGenericProject* pProject)
{
USES_CONVERSION;
CString sName;
if (NULL != pProject)
{
CComBSTR bsName;
if ( SUCCEEDED( pProject->get_Name(&bsName.m_str ) ) )
{
sName = OLE2T(bsName.m_str);
}
}
return sName;
}
CString CWorkspaceHelper::GetFullName(IGenericProject* pProject)
{
USES_CONVERSION;
CString sFullName;
if (NULL != pProject)
{
CComBSTR bsFullName;
if ( SUCCEEDED( pProject->get_FullName(&bsFullName.m_str ) ) )
{
sFullName = OLE2T(bsFullName.m_str);
}
}
return sFullName;
}
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper Document operations
int CWorkspaceHelper::GetDocumentCount(void) const
{
long nDocuments = 0;
IDocuments* pDocuments = NULL;
if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&pDocuments) ) )
{
pDocuments->get_Count(&nDocuments);
pDocuments->Release();
}
return (int)nDocuments;
}
int CWorkspaceHelper::GetDocumentNames(CStringArray& rarrayNames) const
{
rarrayNames.RemoveAll();
CDocumentArray arrayDocuments;
GetDocuments(arrayDocuments);
for (int n = 0; n < arrayDocuments.GetSize(); n++)
{
IGenericDocument* pDocument = arrayDocuments[n];
if (NULL != pDocument)
{
rarrayNames.Add( GetFullName(pDocument) );
pDocument->Release();
}
}
return rarrayNames.GetSize();
}
int CWorkspaceHelper::GetDocuments(CDocumentArray& rarrayDocuments) const
{
USES_CONVERSION;
rarrayDocuments.RemoveAll();
IDocuments* pDocuments = NULL;
if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&pDocuments) ) )
{
int nDocuments = GetDocumentCount();
for (int n = 1; n <= nDocuments; n++) // This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
{
IGenericDocument* pDocument = NULL;
_variant_t Index( (long)n);
pDocuments->Item(Index, (LPDISPATCH*)&pDocument);
if (NULL != pDocument)
{
rarrayDocuments.Add(pDocument);
}
}
pDocuments->Release();
}
return rarrayDocuments.GetSize();
}
IGenericDocument* CWorkspaceHelper::GetDocument(const CString& sFullName) const
{
USES_CONVERSION;
IDocuments* pDocuments = NULL;
IGenericDocument* pDocument = NULL;
if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&pDocuments) ) )
{
CComBSTR bsFullName = T2OLE(sFullName);
_variant_t Index(bsFullName);
pDocuments->Item(Index, (LPDISPATCH*)&pDocument);
pDocuments->Release();
}
return pDocument;
}
CString CWorkspaceHelper::GetName(IGenericDocument* pDocument)
{
USES_CONVERSION;
CString sName;
if (NULL != pDocument)
{
CComBSTR bsName;
if ( SUCCEEDED( pDocument->get_Name(&bsName.m_str ) ) )
{
sName = OLE2T(bsName.m_str);
}
}
return sName;
}
CString CWorkspaceHelper::GetFullName(IGenericDocument* pDocument)
{
USES_CONVERSION;
CString sFullName;
if (NULL != pDocument)
{
CComBSTR bsFullName;
if ( SUCCEEDED( pDocument->get_FullName(&bsFullName.m_str ) ) )
{
sFullName = OLE2T(bsFullName.m_str);
}
}
return sFullName;
}
BOOL CWorkspaceHelper::CloseDocument(const CString& sFileName)
{
BOOL bResult = FALSE;
IGenericDocument* pDocument = GetDocument(sFileName);
if (NULL != pDocument)
{
// RC file is open - close it (save first if need be)
DsSaveStatus Saved;
_variant_t SaveChanges( (long)dsSaveChangesYes);
if ( SUCCEEDED( pDocument->Close(SaveChanges, &Saved) ) )
{
bResult = TRUE;
pDocument->Release();
}
}
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper Resource File operations
CString CWorkspaceHelper::GetResourceFileName(const CString& sProject) const
{
USES_CONVERSION;
CString sResourceFile;
IGenericProject* pProject = GetProject(sProject);
if (NULL != pProject)
{
CString sFullName = GetFullName(pProject);
CProjectFileBuffer prj;
if (!sFullName.IsEmpty())
{
// TO DO: parse the DSP file to get the RC file
CStdioFile f;
CFileException e;
if (f.Open( sFullName,
CFile::modeRead |
CFile::shareDenyWrite,
&e) )
{
// File opened OK
//
// Now create an archive, and an empty text buffer
CArchive ar(&f, CArchive::load);
prj.Serialize(ar);
try
{
ar.Flush();
ar.Close();
f.Flush();
f.Close();
}
catch (CException* e)
{
TCHAR szCause[255];
e->GetErrorMessage(szCause, 255);
CString sMsg;
sMsg.Format( _T("Error closing project file\n\nDetails: %s\n"), szCause);
TRACE1("%s\n", sMsg);
AfxMessageBox(sMsg, MB_OK | MB_ICONSTOP);
e->Delete();
}
}
CStringArray arrayResourceFiles;
prj.GetFiles(arrayResourceFiles, _T(".rc"));
if (arrayResourceFiles.GetSize() > 0)
{
sResourceFile = arrayResourceFiles[0];
}
}
pProject->Release();
}
return sResourceFile;
}
// Closes RC file in the IDE to avoid fuck-ups when symbols are renumbered
BOOL CWorkspaceHelper::CloseResourceFile(const CString& sResourceFileName)
{
BOOL bResult = CloseDocument(sResourceFileName);
if (bResult)
{
// TO DO: Delete the .aps file as well
bResult = TRUE;
}
// Try to delete the .aps file anyway
CNGSplitPath split(sResourceFileName);
CString sApsFileName = split.GetDrive() +
split.GetDirectory() +
split.GetFileName() +
_T(".aps");
if (::FileExists(sApsFileName) )
{
if (!::DeleteFile(sApsFileName))
{
DWORD dwError = GetLastError();
TRACE2("Failed to delete %s: code = %u", sApsFileName, dwError);
}
}
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// CWorkspaceHelper implementation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -