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

📄 chkline32dlg.cpp

📁 用C实现的统计一个目录下的所有源程序共有多少行
💻 CPP
字号:
// ChkLine32Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ChkLine32.h"
#include "ChkLine32Dlg.h"
#include "CheckDir.h"
#include "SelectFolderDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChkLine32Dlg dialog

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

void CChkLine32Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChkLine32Dlg)
	DDX_Text(pDX, IDC_STATIC_SRC_DIR, m_strDir);
	DDX_CBString(pDX, IDC_COMBO1, m_strSrcFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChkLine32Dlg, CDialog)
	//{{AFX_MSG_MAP(CChkLine32Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	ON_BN_CLICKED(IDC_BTN_COPY_FILES, OnBtnCopyFiles)
	ON_BN_CLICKED(IDC_BTN_COPY_LINES, OnBtnCopyLines)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChkLine32Dlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	m_AboutIcon.SubclassDlgItem (IDC_STATIC_ABOUT, this);

	m_staticSorceDir.SubclassDlgItem (IDC_STATIC_SRC_DIR, this);
	m_staticSorceDir.SetPath (TRUE);

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CChkLine32Dlg::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 CChkLine32Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CChkLine32Dlg::OnCancel() 
{
	CDialog::OnCancel();
}

void CChkLine32Dlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();

	if (m_strDir.IsEmpty()) {
		AfxMessageBox("Please specify a valid source directory.");
		GotoDlgCtrl(GetDlgItem(IDC_BTN_BROWSE));
		return;
	}

	CWaitCursor cr;

	UpdateData();

	DoSearch(this);

	SetDlgItemInt(IDC_STATIC_FILES, g_dwFiles, FALSE);
	SetDlgItemInt(IDC_STATIC_LINES, g_dwLines, FALSE);
	SetDlgItemText(IDC_STATIC_INFO, "End.");
}

void CChkLine32Dlg::OnBtnBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	char szDir[MAX_PATH];
	lstrcpy(szDir, m_strDir);
	if (*szDir == '\0')
		lstrcpy(szDir, "C:\\");

	CSelectFolderDialog dlg(TRUE, szDir, OFN_HIDEREADONLY,
											"All Files(*.*)|*.*||",
											this);

	if (dlg.DoModal() != IDOK)
		return;

	m_strDir = dlg.GetSelectedPath();
	UpdateData(FALSE);
}

void CChkLine32Dlg::OnBtnCopyFiles() 
{
	// TODO: Add your control notification handler code here
	HandleCopyText(GetDlgItem(IDC_STATIC_FILES)->m_hWnd);
}

void CChkLine32Dlg::OnBtnCopyLines() 
{
	// TODO: Add your control notification handler code here
	HandleCopyText(GetDlgItem(IDC_STATIC_LINES)->m_hWnd);
}

// Refer to SDK samples Cliptext 
void CChkLine32Dlg::HandleCopyText (HWND hwndTarget)
{
	LRESULT nLen = ::SendMessage(hwndTarget, WM_GETTEXTLENGTH, 0, 0L);
	if (nLen == 0) return;
    
	HANDLE hText = NULL;
    if (!(hText =
		GlobalAlloc(GMEM_MOVEABLE, nLen + 1)))
    {
		return;
    }

    LPSTR lpszText;
    if (!(lpszText = (LPSTR)GlobalLock(hText)))
    {
        return;
    }

	::SendMessage(hwndTarget, WM_GETTEXT, (WPARAM)(nLen + 1), (LPARAM)(lpszText));
    GlobalUnlock(hText);

	HANDLE hData;                // handles to clip data
	LPSTR lpData;               // pointers to clip data
	if (hText != NULL)
	{
	    // Allocate memory and copy the string to it
        if (!(hData
             = GlobalAlloc(GMEM_MOVEABLE, GlobalSize (hText))))
        {
			GlobalFree (hText);
            return;
        }
        if (!(lpData = (LPSTR)GlobalLock(hData)))
        {
			GlobalFree (hText);
			GlobalFree (hData);
            return;
        }
        if (!(lpszText = (LPSTR)GlobalLock (hText)))
        {
			GlobalFree (hText);
			GlobalFree (hData);
            return;
        }

	    lstrcpy(lpData, lpszText);
	    GlobalUnlock(hData);
	    GlobalUnlock (hText);
	
	    // Clear the current contents of the clipboard, and set
	    // the data handle to the new string.
	
		HWND    hDDBWnd = (AfxGetApp()->m_pMainWnd)->m_hWnd;
		if (::OpenClipboard(hDDBWnd))
	    {
	        EmptyClipboard();
	        SetClipboardData(CF_TEXT, hData);
	        CloseClipboard();
	    }
	}
	GlobalFree (hText);
}

⌨️ 快捷键说明

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