?? wordlinedlg.cpp
字號:
// WordLineDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WordLine.h"
#include "WordLineDlg.h"
#include <direct.h>
#include <io.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int CountWords(HANDLE hFile);
int CountLines(HANDLE hFile);
BOOL ShellGetPathCount(HANDLE hDlg, char lpszPath[]);
bool Stop=false;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CWordLineDlg dialog
CWordLineDlg::CWordLineDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWordLineDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWordLineDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWordLineDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWordLineDlg)
DDX_Control(pDX, IDC_FILES, m_TFiles);
DDX_Control(pDX, IDC_MARKEDFILES, m_Files);
DDX_Control(pDX, IDC_STOPCOUNT, m_Stop);
DDX_Control(pDX, IDC_COUNT, m_Start);
DDX_Control(pDX, IDC_WORDS, m_Words);
DDX_Control(pDX, IDC_MARKEDWORDS, m_MarkedWords);
DDX_Control(pDX, IDC_MARKEDLINES, m_MarkedLines);
DDX_Control(pDX, IDC_LINES, m_Lines);
DDX_Control(pDX, IDC_FILESPECS, m_Specs);
DDX_Control(pDX, IDC_FILELIST, m_List);
DDX_Control(pDX, IDC_DIRECTORY, m_Dir);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWordLineDlg, CDialog)
//{{AFX_MSG_MAP(CWordLineDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_COUNT, OnCount)
ON_BN_CLICKED(IDC_STOPCOUNT, OnStopcount)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_FILELIST, OnItemchangedFilelist)
ON_BN_CLICKED(IDC_EXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWordLineDlg message handlers
BOOL CWordLineDlg::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
m_List.InsertColumn(0, "File Name", LVCFMT_LEFT, 125);
m_List.InsertColumn(1, "Words", LVCFMT_LEFT, 125);
m_List.InsertColumn(2, "Lines", LVCFMT_LEFT, 125);
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWordLineDlg::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 CWordLineDlg::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 CWordLineDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL ShellGetPathCount(HANDLE hDlg, char lpszPath[])
{
BOOL bRet;
char szPath[MAX_PATH];
LPITEMIDLIST lpil;
HGLOBAL hgMem;
BROWSEINFO bi;
bi.hwndOwner=(HWND) hDlg;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "Select Destination Folder";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0L;
bi.iImage = 0;
lpil = SHBrowseForFolder(&bi);
if(lpil == NULL)
return FALSE;
bRet = SHGetPathFromIDList(lpil, lpszPath);
hgMem = GlobalHandle(lpil);
GlobalFree(hgMem);
return bRet;
}
void CWordLineDlg::OnBrowse()
{
// TODO: Add your control notification handler code here
char Directory[MAX_PATH];
if(ShellGetPathCount(m_hWnd, Directory))
m_Dir.SetWindowText(Directory);
}
void CWordLineDlg::OnCount()
{
// TODO: Add your control notification handler code here
char Directory[MAX_PATH], FileSpecs[300], TmpStr[MAX_PATH], *TmpVal=FileSpecs;
int NumFileSpecs=0, NumWords=0, NumLines=0;
Stop = false;
m_Specs.GetWindowText(FileSpecs, 290);
m_Dir.GetWindowText(Directory, MAX_PATH);
if(!strlen(Directory)){
AfxMessageBox("Directory not found");
return;
}
if(Directory[3])
strcat(Directory, "\\");
while(strlen(TmpVal)){
if(*(TmpVal++) == ';'){
NumFileSpecs++;
}
}
NumFileSpecs++;
TmpVal=FileSpecs;
if(_chdir(Directory)){
AfxMessageBox("Directory not found");
return;
}
if(!strlen(FileSpecs)){
AfxMessageBox("Filespec not found");
return;
}
m_List.DeleteAllItems();
m_Words.SetWindowText("");
m_Files.SetWindowText("");
m_Lines.SetWindowText("");
m_TFiles.SetWindowText("");
m_MarkedWords.SetWindowText("");
m_MarkedLines.SetWindowText("");
m_Start.EnableWindow(FALSE);
m_Stop.EnableWindow(TRUE);
for(int i=0; i<NumFileSpecs; i++){
strcpy(TmpStr, Directory);
char TmpStr1[5]={0, 0};
while(strlen(TmpVal)){
if(*(TmpVal++) != ';'){
TmpStr1[0]=*(TmpVal-1);
strcat(TmpStr, TmpStr1);
}
else
break;
}
struct _finddata_t ff;
long hFind = _findfirst(TmpStr, &ff);
while(strcmp(ff.name, ".")==0 || strcmp(ff.name, "..")==0)
_findnext(hFind, &ff);
do{
HANDLE hFile = CreateFile(ff.name, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile==INVALID_HANDLE_VALUE){
continue;
}
NumWords = CountWords(hFile);
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
NumLines = CountLines(hFile);
CloseHandle(hFile);
m_List.InsertItem(0, ff.name);
char TmpStr2[20];
wsprintf(TmpStr2, "%u", NumWords);
m_List.SetItemText(0, 1, TmpStr2);
wsprintf(TmpStr2, "%u", NumLines);
m_List.SetItemText(0, 2, TmpStr2);
MSG Msg;
if(::PeekMessage(&Msg, m_hWnd, WM_NULL, WM_USER-1, PM_NOREMOVE)){
::PeekMessage(&Msg, m_hWnd, WM_NULL, WM_USER-1, PM_REMOVE);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if(Stop)
break;
}while(!_findnext(hFind, &ff));
_findclose(hFind);
}
m_Stop.EnableWindow(FALSE);
m_Start.EnableWindow(TRUE);
long Tmp;
OnItemchangedFilelist(NULL, &Tmp);
}
int CountWords(HANDLE hFile)
{
char TmpStr[5];
DWORD Tmp=1, TmpCount=0;
while(Tmp){
ReadFile(hFile, TmpStr, 1, &Tmp, NULL);
if((TmpStr[0]<'A' || TmpStr[0]>'Z') && (TmpStr[0]<'0' || TmpStr[0]>'9') && ((TmpStr[0] != 0x0A) && (TmpStr[0] != 0x0D)) && (TmpStr[0]<'a' || TmpStr[0]>'z'))
TmpCount++;
}
return TmpCount;
}
int CountLines(HANDLE hFile)
{
char TmpStr[5];
DWORD Tmp=1, TmpCount=0;
while(Tmp){
ReadFile(hFile, TmpStr, 1, &Tmp, NULL);
if(TmpStr[0] == 0x0A)
TmpCount++;
}
return TmpCount;
}
void CWordLineDlg::OnStopcount()
{
// TODO: Add your control notification handler code here
Stop = true;
}
void CWordLineDlg::OnItemchangedFilelist(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
int NumWords=0, NumLines=0, MarkedLines=0, MarkedWords=0, NumMarkedFiles=0;
char TmpStr[100];
for(int i=0; i<m_List.GetItemCount(); i++){
m_List.GetItemText(i, 1, TmpStr, 20);
NumWords += atoi(TmpStr);
m_List.GetItemText(i, 2, TmpStr, 20);
NumLines += atoi(TmpStr);
if(m_List.GetItemState(i, LVIS_SELECTED)==LVIS_SELECTED){
m_List.GetItemText(i, 1, TmpStr, 20);
MarkedWords += atoi(TmpStr);
m_List.GetItemText(i, 2, TmpStr, 20);
MarkedLines += atoi(TmpStr);
wsprintf(TmpStr, "%u", MarkedWords);
m_MarkedWords.SetWindowText(TmpStr);
wsprintf(TmpStr, "%u", MarkedLines);
m_MarkedLines.SetWindowText(TmpStr);
NumMarkedFiles++;
wsprintf(TmpStr, "%u", NumMarkedFiles);
m_Files.SetWindowText(TmpStr);
}
}
wsprintf(TmpStr, "%u", NumWords);
m_Words.SetWindowText(TmpStr);
wsprintf(TmpStr, "%u", NumLines);
m_Lines.SetWindowText(TmpStr);
wsprintf(TmpStr, "%u", m_List.GetItemCount());
m_TFiles.SetWindowText(TmpStr);
*pResult = 0;
}
void CWordLineDlg::OnExit()
{
// TODO: Add your control notification handler code here
Stop = true;
PostQuitMessage(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -