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

📄 moduledlg.cpp

📁 这是书上的代码
💻 CPP
字号:
//ModuleDlg.cpp : implementation file
#include "stdafx.h"
#include "TestLib.h"
#include "ModuleDlg.h"
#include <stdio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CModuleDlg dialog
#define DLL_NUM 1

CModuleDlg::CModuleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CModuleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CModuleDlg)
	//}}AFX_DATA_INIT
//	m_dllList.RemoveAll();
}


void CModuleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModuleDlg)
	DDX_Control(pDX, IDC_LIST1, m_dllList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CModuleDlg, CDialog)
	//{{AFX_MSG_MAP(CModuleDlg)
	ON_BN_CLICKED(IDC_BUTTON_REGIST, OnButtonRegist)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModuleDlg message handlers CStringList

BOOL CModuleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	CDllInfo	dllInfo;
	POSITION	position;
   
	position = m_pDllList->GetHeadPosition(); 
	m_pDllList->GetNext(position);
	while(position != NULL)
	{
		dllInfo = m_pDllList->GetAt(position);
		int nIndex = m_dllList.AddString(dllInfo.m_dllFunName + CString("  ")
			+ dllInfo.m_dllFileName + CString("  ")
			+ dllInfo.m_dllType);
		m_dllList.SetCheck(nIndex, dllInfo.m_bUsed);

		m_pDllList->GetNext(position);
		m_workDllList.AddTail(dllInfo);
	}
	
	return TRUE;
}

void CModuleDlg::OnButtonRegist() 
{
	CFileDialog fileDlg(TRUE, NULL, "*.ext", NULL, "*.ext");//(动态库)");
	CString		fileName;
	FILE*		fp;
	int			nDllCount;
	CDllInfo	dllInfo;
	int			i;
	char		work[256];
	int			tmpIndex;
	CString		tmp;
	BOOL		findDll;
	POSITION	pos;
	CDllInfo	dInfo;

	if(fileDlg.DoModal()!=IDOK)
		return;
	fileName = fileDlg.GetPathName( );
	if(fileName.IsEmpty())
		return;
	
	if((fp = fopen(fileName, "rb")) == NULL)
	{
		AfxMessageBox("找不到模块信息文件!");
		return;
	}

	fscanf(fp, "%d", &nDllCount);

	for(i = 0;i < nDllCount; i++)
	{
		fscanf(fp,"%s", work);
		dllInfo.m_dllFunName = work;
		dllInfo.m_dllFunName.TrimRight();

		fscanf(fp,"%s", work);
			
		dllInfo.m_dllFileName = work;
		dllInfo.m_dllFileName.TrimRight();
		fscanf(fp,"%s", work);
			
		dllInfo.m_dllType = work;
		dllInfo.m_dllType.TrimRight();//CString
		tmp = dllInfo.m_dllFunName + CString("  ")
				+ dllInfo.m_dllFileName + CString("  ")
				+ dllInfo.m_dllType;

		dllInfo.m_bDelete  = FALSE;
		dllInfo.m_bUsed    = TRUE;
		dllInfo.m_hinstance= NULL;
		findDll = FALSE;
		pos = m_workDllList.GetHeadPosition();
		while ( pos )
		{
			dInfo = m_workDllList.GetNext(pos);
			if ( !dInfo.m_bDelete && dInfo.m_dllFileName == dllInfo.m_dllFileName )
			{
				findDll = TRUE;
				break;
			}
		}
		if (findDll)	//动态库已在列表中
			continue;
		tmpIndex = m_dllList.AddString(tmp);
		m_dllList.SetCheck(tmpIndex, 1);
		m_workDllList.AddTail(dllInfo);
	}
	
	if (fp !=NULL ) fclose(fp);
}


void CModuleDlg::OnOK() 
{
	POSITION	position;
	POSITION	oldPosition;
	int			i;
	CDllInfo	pDllInfo;

	position	= m_pDllList->GetHeadPosition();
	oldPosition = position;
	for(i = 0; i < DLL_NUM;i++)
	{
		m_pDllList->GetNext(position);
	}

	while(position != NULL)
	{
		m_pDllList->RemoveAt(position);
		position = oldPosition; 
		m_pDllList->GetNext(position);
	}

	position = m_workDllList.GetHeadPosition(); 
	while(position != NULL)
	{
		pDllInfo = m_workDllList.GetNext(position);
		m_pDllList->AddTail(pDllInfo);
		//delete m_workDllList.GetNext();
	}
	m_workDllList.RemoveAll();

	CDialog::OnOK();
}

void CModuleDlg::OnButtonDel() 
{
	CString str;
	int nIndex = m_dllList.GetCurSel();	
	if(nIndex < 0)
		return;
	m_dllList.GetText(nIndex, str);
	str = str.SpanExcluding(" ");
	
	CDllInfo dllInfo;
	POSITION position = m_workDllList.GetHeadPosition();
	while(position != NULL)
	{
		dllInfo = m_workDllList.GetAt(position);
		if(str == dllInfo.m_dllFunName)
		{
			if(dllInfo.m_hinstance == NULL)
			{
				POSITION pos = position;
				m_workDllList.GetPrev(position);
				m_workDllList.RemoveAt( pos);
				if(position  == NULL) position = m_workDllList.GetHeadPosition();
				
				//position = pos;
				
			}
			else//设置删除标记
			{//Clist
				dllInfo.m_bDelete = TRUE;
				dllInfo.m_bUsed = FALSE;
				m_workDllList.SetAt(position, dllInfo);
			}
			
		}
		if(position != NULL)
			m_workDllList.GetNext(position);
	}

	
	CStringArray dllArray;
	CUIntArray	 dllUseArray;
	//CListBox
	for(int i = 0;i < m_dllList.GetCount();i++)
	{
		CString str;
		if(i == nIndex) continue;
		m_dllList.GetText( i, str);
		dllArray.Add(str);
		dllUseArray.Add(m_dllList.GetCheck(i));
	}
	m_dllList.ResetContent( );

	for(i = 0;i < dllUseArray.GetSize();i++)
	{
		nIndex = m_dllList.AddString(dllArray.ElementAt(i));
		m_dllList.SetCheck(nIndex,dllUseArray.GetAt(i));
	}
}

void CModuleDlg::OnCancel() 
{
	m_workDllList.RemoveAll();
	CDialog::OnCancel();
}

void CModuleDlg::OnSelchangeList1() 
{
	CString		str;
	CDllInfo	dllInfo;
	POSITION	position;
	BOOL		bTrue = FALSE;
	int			nIndex= m_dllList.GetCurSel();	
	
	if(m_dllList.GetCheck(nIndex) == 1)
		bTrue = TRUE;
	
	m_dllList.GetText(nIndex, str);
	str = str.SpanExcluding(" ");
	position = m_workDllList.GetHeadPosition();

	while(position != NULL)	
	{
		dllInfo = m_workDllList.GetAt(position);
		if(str == dllInfo.m_dllFunName)
		{
			if(bTrue)
			{
				dllInfo.m_bUsed = TRUE;
				m_workDllList.SetAt(position, dllInfo);
			}
			else
			{
				dllInfo.m_bUsed = FALSE;
				m_workDllList.SetAt(position, dllInfo);
			}
		}
		m_workDllList.GetNext(position);
	}	
	
}

⌨️ 快捷键说明

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