📄 checkdlg.cpp
字号:
// checkDlg.cpp : implementation file
//
#include "stdafx.h"
#include "check.h"
#include "check1.h"
#include "dir.h"
#include "checkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#define TEST_ONLY
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCheckDlg dialog
CCheckDlg::CCheckDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCheckDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCheckDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_bCheck = false;
m_bSearch = false;
m_bStop = false;
m_hThread = NULL;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCheckDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCheckDlg)
DDX_Control(pDX, IDC_EDIT_PATH, m_edit_path);
DDX_Control(pDX, IDC_EDIT1, m_edit);
DDX_Control(pDX, IDC_BUTTON_STOP, m_btnStop);
DDX_Control(pDX, IDC_BUTTON_START, m_btnStart);
DDX_Control(pDX, IDC_BUTTON_SELECT, m_btnSelect);
DDX_Control(pDX, IDC_PROGRESS1, m_prog);
DDX_Control(pDX, IDC_TREE1, m_tree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCheckDlg, CDialog)
//{{AFX_MSG_MAP(CCheckDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_NOTIFY(NM_DBLCLK, IDC_TREE1, OnDblclkTree1)
ON_WM_SIZE()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCheckDlg message handlers
BOOL CCheckDlg::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
this->init();
this->AdjustCtrl();
::SetWindowPos(m_hWnd, HWND_BOTTOM, 0, 0, 800, 600, SWP_NOZORDER);
this->test_only();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCheckDlg::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 CCheckDlg::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 CCheckDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CCheckDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
{
PostMessage(WM_CLOSE, 0, 0);
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
void CCheckDlg::OnOK()
{
//dont exit when press enter
}
void CCheckDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if (MessageBox( "确定要退出吗?", "提示",
MB_ICONQUESTION | MB_YESNO ) == IDNO) return;
CDialog::OnClose();
}
void CCheckDlg::test_only()
{
// ::SetWindowPos(m_DlgView.m_hWnd, HWND_BOTTOM, 0, 0, 600, 400, SWP_NOZORDER);
// m_DlgView.CenterWindow(NULL);
// m_DlgView.ShowWindow(SW_SHOW);
// //m_DlgView.ViewFile("c:\\tmp.txt", 10);
// m_DlgView.ViewFile("E:\\wqzhang\\hlqclient\\MyShell\\MyShellDlg.cpp", 10);
// return;
#ifdef TEST_ONLY
CCheck chk_obj;
chk_obj.set_tree(m_tree);
chk_obj.check_file("e:\\wqzhang\\test\\check\\err\\err.cpp");
#endif
}
void CCheckDlg::init()
{
m_prog.SetRange(1, 100);
this->UpdateBtnState();
m_edit_path.SetWindowText("选择源文件所在目录,然后点击检查!");
//dlg view
m_DlgView.Create(IDD_DIALOG_VIEW, NULL);
::SetWindowPos(m_DlgView.m_hWnd, HWND_BOTTOM, 0, 0, 600, 400, SWP_NOZORDER);
m_DlgView.CenterWindow(NULL);
//init icon
HICON hIcon = NULL;
m_imglst.Create( 16, 16, ILC_COLOR16 | ILC_MASK, 10, 5 );
hIcon = AfxGetApp()->LoadIcon( MAKEINTRESOURCE(IDI_H)); m_imglst.Add(hIcon);
hIcon = AfxGetApp()->LoadIcon( MAKEINTRESOURCE(IDI_C)); m_imglst.Add(hIcon);
hIcon = AfxGetApp()->LoadIcon( MAKEINTRESOURCE(IDI_CPP)); m_imglst.Add(hIcon);
hIcon = AfxGetApp()->LoadIcon( MAKEINTRESOURCE(IDI_ERR)); m_imglst.Add(hIcon);
m_tree.SetImageList( &m_imglst, TVSIL_NORMAL );
}
bool CCheckDlg::ReadPath(CString& strPath)
{
char szPath[500] = "";
GetTempPath(sizeof(szPath), szPath);
strcat(szPath, "\\$checktmp$.ini");
FILE* fp = fopen(szPath, "r");
if (fp == 0)
return false;
char buf[200] = "";
fread(buf, 1, sizeof(buf), fp);
strPath = buf;
fclose(fp);
return true;
}
bool CCheckDlg::WritePath(CString& strPath)
{
char szPath[500] = "";
GetTempPath(sizeof(szPath), szPath);
strcat(szPath, "\\$checktmp$.ini");
FILE* fp = fopen(szPath, "w");
if (fp == 0)
return false;
const char* buf = (LPCSTR) strPath;
fwrite(buf, 1, strlen(buf), fp);
fclose(fp);
return true;
}
void CCheckDlg::DelTmpFile() //not used.
{
char szPath[500] = "";
GetTempPath(sizeof(szPath), szPath);
strcat(szPath, "\\$checktmp$.ini");
DeleteFile(szPath);
}
void CCheckDlg::OnButtonSelect()
{
BROWSEINFO info;
info.hwndOwner = m_hWnd;
info.iImage = 0;
info.lParam = 0;
info.lpszTitle = 0;
info.pidlRoot = 0;
info.ulFlags = 0;
info.pszDisplayName = 0;
info.lpfn = BrowseCallbackProc;
LPITEMIDLIST pidl = SHBrowseForFolder(&info);
if (pidl != NULL)
{
char szPath[_MAX_PATH] = "";
if (SHGetPathFromIDList(pidl, szPath))
{
m_strPath = szPath;
m_edit_path.SetWindowText(m_strPath);
CString str = "[文件夹]"; str += m_strPath;
SetStatus(str);
WritePath(m_strPath);
}
}
}
int CALLBACK CCheckDlg::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED:
{
CString strPath;
if (ReadPath(strPath) && !strPath.IsEmpty())
{
const char* pszPath = (LPCSTR) strPath;
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)pszPath);
}
}
break;
case BFFM_SELCHANGED:
break;
}
return 0;
}
void CCheckDlg::OnButtonStart()
{
if (m_bCheck)
return;
if (m_strPath.IsEmpty())
{
MessageBox("请选择需要检查的文件夹!", "提示", MB_ICONINFORMATION);
return;
}
//create thread
DWORD dwTid = 0;
m_hThread = ::CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) ThreadCheck, (void*)this, 0, &dwTid );
if (m_hThread != NULL)
{
m_bCheck = true;
UpdateBtnState();
//AdjustCtrl();
m_tree.DeleteAllItems();
}
}
void CCheckDlg::OnButtonStop()
{
m_bStop = true;
if (IsSearching())
{
TerminateThread(m_hThread, 0);
m_bCheck = false;
m_bSearch = false;
m_bStop = false;
m_hThread = NULL;
AdjustCtrl();
UpdateBtnState();
SetStatus("已停止");
}
}
void CCheckDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_tree.GetSafeHwnd() && m_prog.GetSafeHwnd())
this->AdjustCtrl();
}
void CCheckDlg::OnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
*pResult = 0;
if (m_bCheck)
return;
HTREEITEM hSelItem = m_tree.GetSelectedItem();
if (hSelItem == NULL)
return;
HTREEITEM hParentItem = m_tree.GetParentItem(hSelItem);
if (hParentItem == NULL)
return;
CString strItem = m_tree.GetItemText(hSelItem);
CString strParentItem = m_tree.GetItemText(hParentItem);
const char* pszFileName = (LPCSTR) strParentItem;
int nLineNum = 0;
if (1 == sscanf((LPCSTR)strItem, "line %d:", &nLineNum))
{
if (!m_DlgView.IsWindowVisible())
m_DlgView.ShowWindow(SW_SHOW);
m_DlgView.ViewFile(pszFileName, nLineNum-1);//0 based!
//m_DlgView.SetFocus();
//m_DlgView.ViewFile("c:\\tmp.txt", 10);
}
}
bool CCheckDlg::IsSearching()
{
return m_bSearch;
// char txt[500] = "";
// m_edit.GetWindowText(txt, sizeof(txt));
// bool bIsSearching = (strstr(txt, "正在搜索") == txt);
// return bIsSearching;
}
void CCheckDlg::AdjustCtrl()
{
RECT rc;
GetClientRect(&rc);
int dlg_w = rc.right - rc.left;
int dlg_h = rc.bottom - rc.top;
m_btnSelect.GetWindowRect(&rc);
ScreenToClient(&rc);
int btn_l = rc.left;
int btn_b = rc.bottom;
const int pad = 6;
m_prog.GetWindowRect(&rc);
int prog_h = rc.bottom - rc.top;
int prog_w = rc.right - rc.left;
m_edit.GetWindowRect(&rc);
int edit_h = rc.bottom - rc.top;
int edit_w = rc.right - rc.left;
char txt[500] = "";
m_edit.GetWindowText(txt, sizeof(txt));
bool bShowProg = (m_bCheck && !IsSearching());
//move tree
rc.left = btn_l;
rc.top = btn_b + pad;
rc.right = dlg_w - pad;
rc.bottom = dlg_h - (edit_h + pad*2);
if (bShowProg)
rc.bottom -= (prog_h + pad);
m_tree.MoveWindow(&rc, TRUE);
//move prog
m_prog.ShowWindow(SW_HIDE);
if (bShowProg)
{
m_prog.ShowWindow(SW_SHOW);
rc.top = rc.bottom + pad;
rc.bottom = rc.top + prog_h;
m_prog.MoveWindow(&rc, TRUE);
}
//move edit
rc.top = rc.bottom + pad;
rc.bottom = rc.top + edit_h;
m_edit.MoveWindow(&rc, TRUE);
// //move edit path
// m_edit_path.GetWindowRect(&rc);
// rc.right = dlg_w - pad;
// ScreenToClient(&rc);
// m_edit_path.MoveWindow(&rc, TRUE);
//invalidate
m_tree.Invalidate(); m_tree.UpdateWindow();
m_edit.Invalidate(); m_edit.UpdateWindow();
m_prog.Invalidate(); m_prog.UpdateWindow();
}
void CCheckDlg::SetStatus(const char *msg)
{
m_edit.SetWindowText(msg);
m_edit.Invalidate();
m_edit.UpdateWindow();
}
int CCheckDlg::GetChildrenCnt(HTREEITEM hItem)
{
if (hItem == NULL)
return 0;
int child_cnt = 0;
HTREEITEM hChild = m_tree.GetChildItem(hItem);
while (hChild != NULL)
{
child_cnt++;
hChild = m_tree.GetNextSiblingItem(hChild);
}
return child_cnt;
}
void CCheckDlg::UpdateBtnState()
{
m_btnSelect.EnableWindow(!m_bCheck);
m_btnStart.EnableWindow(!m_bCheck);
m_btnStop.EnableWindow(m_bCheck);
}
//check thread
DWORD CCheckDlg::ThreadCheck(LPVOID lpParam)
{
if (lpParam == NULL)
return 0;
CCheckDlg& dlg = *(CCheckDlg*) lpParam;
CTreeCtrl& tree = dlg.m_tree;
const char* folder = (LPCTSTR)dlg.m_strPath;
//const char* folder = "e:\\wqzhang\\hlqclient\\myshell";
//search files to check
dlg.m_bSearch = true;
dlg.AdjustCtrl();
dlg.SetStatus("正在搜索该目录下的源文件....");
vector<std::string> set_file;
CDir dir_obj;
CCheck chk_obj;
chk_obj.set_tree(tree);
dir_obj.search_src_file(folder, set_file);
dlg.m_bSearch = false;
dlg.AdjustCtrl();
//exit when no file
int nCount = set_file.size();
if (nCount == 0)
{
dlg.m_bCheck = false;
dlg.AdjustCtrl();
dlg.UpdateBtnState();
char str[500] = "";
sprintf(str, "[该目录下没有源文件]%s", folder);
dlg.SetStatus(str);
return 0;
}
//checking......
for (int i = 0; i < nCount; i++)
{
if (dlg.m_bStop)
{
set_file.resize(i);
break;
}
const char* file = set_file[i].c_str();
char str[200] = "";
sprintf(str, "正在检查%s [%d/%d]", file, i + 1, nCount);
dlg.SetStatus(str);
chk_obj.check_file(file);
dlg.m_prog.SetPos(100.0 * (i + 1) / nCount);
}
//report
dlg.m_bCheck = false;
dlg.m_bStop = false;
dlg.m_hThread = NULL;
dlg.Report(set_file);
dlg.AdjustCtrl();
dlg.UpdateBtnState();
return 0;
}
void CCheckDlg::Report(vector<std::string>& set_file)
{
//report
CTreeCtrl& tree = m_tree;
int nFileCnt = 0, nErrCnt = 0;
HTREEITEM hItem = tree.GetRootItem();
while (hItem)
{
tree.Expand(hItem, TVE_EXPAND);
nFileCnt++;
nErrCnt += GetChildrenCnt(hItem);
hItem = tree.GetNextSiblingItem(hItem);
}
char str[200] = "";
sprintf(str, "完毕! 共检查了%d个文件, %d个文件存在问题, 共%d个问题", set_file.size(), nFileCnt, nErrCnt);
SetStatus(str);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -