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

📄 statprojectsdlg.cpp

📁 简单的原代码统计
💻 CPP
字号:
// StatProjectsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StatProjects.h"
#include "StatProjectsDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatProjectsDlg dialog

CStatProjectsDlg::CStatProjectsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStatProjectsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStatProjectsDlg)
	m_strStat = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CStatProjectsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStatProjectsDlg)
	DDX_Control(pDX, IDC_LISTFILE, m_ListFile);
	DDX_Text(pDX, IDC_STAT, m_strStat);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStatProjectsDlg, CDialog)
	//{{AFX_MSG_MAP(CStatProjectsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatProjectsDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_ListFile.InsertColumn(0,"文件名", LVCFMT_LEFT, 300);
	m_ListFile.InsertColumn(1,"行数", LVCFMT_LEFT, 60);
	m_ListFile.InsertColumn(2,"字节数", LVCFMT_LEFT, 90);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CStatProjectsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CStatProjectsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CStatProjectsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
#include "DirDialog.h"
void CStatProjectsDlg::OnOK() 
{
	CDirDialog dlg;
	CString strPath;
	if(dlg.DoBrowse() == IDOK)
	{
		strPath = dlg.m_strPath;
	}
	else return;
	m_nCountBits = 0;
	m_nCountLine = 0;
	m_ListFile.DeleteAllItems();
	StatFolder(strPath);
	m_strStat.Format("共计%d行,%d字节", m_nCountLine, m_nCountBits );
	UpdateData(FALSE);
}


BOOL CStatProjectsDlg::StatFolder(LPCTSTR lpszPath)
{
	CString strPath(lpszPath);
	CFileFind find;
	CString s,s1,s2,s3,s4,s5;
	s1.Format("%s\\*.*",strPath);
	if(!find.FindFile(s1,0)) return TRUE;	
	int ret,quit;
	ret = find.FindNextFile();
	quit = 0;
	int nLine, nBits;
	nLine = 0;
	nBits = 0;
	while(1)
	{	
		if(ret==0)  quit=1;		 
		s3=find.GetFileName();
		if(s3!="."&&s3!="..")
		{
			s4.Format("%s\\%s",strPath,s3);	 
			if(find.IsDirectory())
			{
				StatFolder(s4);
			}
			else
			{
				BOOL bStat = FALSE;
				s = s4.Right(2);
				s.MakeUpper();
				if(s.Compare(".H") == 0) bStat = TRUE;
				s = s4.Right(4);
				s.MakeUpper();
				if(s.Compare(".CPP") == 0) bStat = TRUE;
				
				if(bStat)
				{
					nLine = nBits = 0;
					if(StatFile(s4, &nLine, &nBits))
					{
						m_nCountLine += nLine;
						m_nCountBits += nBits;
						int nItem  = m_ListFile.GetItemCount();
						s.Format("%d", nLine);
						m_ListFile.InsertItem(nItem, s4);
						m_ListFile.SetItemText(nItem,1, s);
						if(nBits > 10000000)
							s.Format("%.3fM", nBits/1000000.0);
						else if(nBits > 10000)
							s.Format("%.3fK", nBits/1000.0);
						else
							s.Format("%d", nBits);
						m_ListFile.SetItemText(nItem,2, s);
					}
				}
				
			}	
		}
	 	if(quit) break;
		ret=find.FindNextFile();	 
	}
	find.Close();
	return TRUE;
}

BOOL CStatProjectsDlg::StatFile(LPCTSTR lpszFile, int* pnLine, int* pnBits)
{
	int nLine, nBits;
	nLine = 1;
	nBits = 0;
	FILE* fp;
	fp=fopen(lpszFile, "r");
	if(fp == NULL) return FALSE;
	char pBuff[1024];
	memset(pBuff,0,1024);
	int nLen=0;
	while((nLen = fread(pBuff,1,1024, fp)) > 0)
	{
		for(int i=0;i<1024;i++) if(pBuff[i] == '\n') nLine++;
		nBits += nLen;
	}
	fclose(fp);
	*pnLine = nLine;
	*pnBits = nBits;
	return TRUE;
}

⌨️ 快捷键说明

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