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

📄 scckillerdlg.cpp

📁 使用vc删除一个文件夹中指定的文件 能很好地删除一个文件
💻 CPP
字号:
// SccKillerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SccKiller.h"
#include "SccKillerDlg.h"
#include "HyperLink.h"

#include <winuser.h>

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

#define LWA_ALPHA		0x00000002
#define WS_EX_LAYERED	0x00080000

#define IDS_WEBADDR		_T("http://www.neusoft.com")
#define IDS_MAILADDR	_T("mailto:wang-dd@neusoft.com")


#define ShowMessage(strInfo) MessageBox((strInfo), _T("* Notice *"), MB_OK | MB_ICONINFORMATION)


typedef BOOL (WINAPI *lpfnSysFun) (HWND hwnd, // handle to the window to animate
							  DWORD dwTime,   // duration of animation
							  DWORD dwFlags   // animation type
							  );

typedef BOOL (WINAPI* lpfnSetTran)(HWND hWnd,
								   COLORREF crKey,
								   BYTE bAlpha,
								   DWORD dwFlags);

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


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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	BOOL OnInitDialog()
	{
		CDialog::OnInitDialog();

		m_EMailLink.SetURL(IDS_MAILADDR);
		m_EMailLink.SetUnderline(CHyperLink::ulAlways);

		m_HomePageLink.SetURL(IDS_WEBADDR);
		m_HomePageLink.SetUnderline(CHyperLink::ulAlways);
		return TRUE;
	}
// Implementation
protected:
	DECLARE_MESSAGE_MAP()
private:
	CHyperLink		m_EMailLink;
	CHyperLink		m_HomePageLink;
};

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

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EMAILLINK, m_EMailLink);
	DDX_Control(pDX, IDC_HOMEPAGELINK, m_HomePageLink);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSccKillerDlg dialog

CSccKillerDlg::CSccKillerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSccKillerDlg::IDD, pParent)
    , m_strInfo(_T("The number of .scc files is : 0 "))
    , m_iFileCount(0)
    , m_iFileSumCount(0)
{
	//{{AFX_DATA_INIT(CSccKillerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSccKillerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_DOCLEAR, m_btnCleaer);
	DDX_Control(pDX, IDC_FILELIST, m_fileList);
    DDX_Text(pDX, IDC_INFO, m_strInfo);

}

BEGIN_MESSAGE_MAP(CSccKillerDlg, CDialog)

    ON_WM_DROPFILES()
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
    ON_WM_TIMER()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_DOCLEAR, OnDoclear)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSccKillerDlg message handlers

BOOL CSccKillerDlg::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)
    {
        pSysMenu->RemoveMenu(2, MF_BYPOSITION);
        pSysMenu->AppendMenu(MF_SEPARATOR);
        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, _T("About SccKiller"));
    }
    SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

    m_hModule = LoadLibrary("User32.DLL");
	if(m_hModule != NULL)
	{
		lpfnSysFun lpfnWindow;
		lpfnWindow = (lpfnSysFun)GetProcAddress(m_hModule, "AnimateWindow");
		if(lpfnWindow != NULL)
		{
			CenterWindow();
			lpfnWindow(m_hWnd, 400, 0x00000010);
		}
		/*
        ::SetWindowLong(
			m_hWnd, 
			GWL_EXSTYLE, 
			::GetWindowLong(m_hWnd, GWL_EXSTYLE) |  WS_EX_LAYERED);
		SetTransparent(m_hWnd, 0, 255 * 100 / 100 , LWA_ALPHA);
		*/
    }
    m_fileList.SetExtendedStyle(LVS_EX_GRIDLINES);
	m_fileList.InsertColumn(0, _T("\tAll the .scc file(s)"), LVCFMT_LEFT, 700);

	//initialize the buttons' style

    SetTimer(1, 500, NULL);
    m_btnCleaer.SetIcon(IDI_IN, IDI_OUT);
	m_btnCleaer.SetAlign(CButtonST::ST_ALIGN_HORIZ);
    m_btnCleaer.SetTooltipText(_T("惔彍強桳揑.scc暥審"));
	m_btnCleaer.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 40);  
	m_btnCleaer.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 130, 0));
	m_btnCleaer.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(190, 190, 190));
	m_btnCleaer.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(190, 190, 190));

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSccKillerDlg::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 CSccKillerDlg::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();
	}
}

HCURSOR CSccKillerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSccKillerDlg::OnDoclear()
{
	// TODO: Add your control notification handler code here

    int         iSccCount = 0;
    DWORD       dwFileAttrib;
    CString     strFilePath;
    CString     strInfo;
    CFileFind   findFile;

    if (0 == m_iFileSumCount)
    {
        ShowMessage(_T("Hey, man, you should choose your scc file(s) first !"));
        return;
    }

    (CButton *)(GetDlgItem(IDC_DOCLEAR))->EnableWindow(FALSE);
    POSITION position = m_strltFileList.GetHeadPosition();

	for (int i = 0; position != 0; ++i)
	{
        strFilePath = m_strltFileList.GetNext(position);
        if (!findFile.FindFile(strFilePath))
        {
            --m_iFileSumCount;
            continue;
        }

        dwFileAttrib = GetFileAttributes(strFilePath);
        dwFileAttrib &= ~FILE_ATTRIBUTE_READONLY;
        dwFileAttrib &= ~FILE_ATTRIBUTE_SYSTEM;
        dwFileAttrib &= ~FILE_ATTRIBUTE_HIDDEN;
        SetFileAttributes(strFilePath,   dwFileAttrib);

        if (!DeleteFile(strFilePath))
        {
            m_strltDelErrList.AddTail(strFilePath);
            continue;
        }

        ++iSccCount;
    }

    position = m_strltAddFileList.GetHeadPosition();

    for (i = 0; position != 0; ++i)
    {
        strFilePath = m_strltAddFileList.GetNext(position);
        if (!findFile.FindFile(strFilePath))
        {
            --m_iFileSumCount;
            continue;
        }

        dwFileAttrib  =  GetFileAttributes(strFilePath);
        dwFileAttrib &= ~FILE_ATTRIBUTE_READONLY;
        dwFileAttrib &= ~FILE_ATTRIBUTE_SYSTEM;
        dwFileAttrib &= ~FILE_ATTRIBUTE_HIDDEN;
        SetFileAttributes(strFilePath,   dwFileAttrib);

        if (!DeleteFile(strFilePath))
        {
            m_strltDelErrList.AddTail(strFilePath);
            continue;
        }
        else
        {
            ++iSccCount;
        }
    }

    if ((UINT)iSccCount == m_iFileSumCount)
    {
        strInfo.Format(_T("All %d nonrepeated .scc file(s) have been deleted!"), iSccCount);
    }
    else
    {
        strInfo.Format(_T("%d .scc file(s) have been deeted, %d failed as follows:\n"), 
            iSccCount, 
            m_iFileSumCount - iSccCount);

        position = m_strltDelErrList.GetHeadPosition();
        for (int i = 0; position != 0; ++i)
        {
            strInfo += m_strltDelErrList.GetNext(position);
        }
    }

    m_strInfo.Format(_T("The number of .scc file(s) is : 0"));
    m_strltFileList.RemoveAll();
    m_strltAddFileList.RemoveAll();
    m_strltDelErrList.RemoveAll();
    m_fileList.DeleteAllItems();
    m_iFileSumCount = 0;
    ShowMessage(strInfo);
    (CButton *)(GetDlgItem(IDC_DOCLEAR))->EnableWindow();
    UpdateData(FALSE);
}

void CSccKillerDlg::OnDropFiles(HDROP hDropInfo)
{ 
    HANDLE hFile;
    WIN32_FIND_DATA wfdFile;
    CString strFilePath;
    m_iFileCount = 0;
    UINT  iFileCount = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);//count the number of dragged folders or files 
    for (UINT iIndex = 0 ; iIndex < iFileCount; ++iIndex)	
    {	
        UINT iLength = DragQueryFile(hDropInfo, iIndex, NULL, NULL) + 1;
        DragQueryFile(hDropInfo, iIndex, strFilePath.GetBuffer(iLength + 1), iLength); //strFilePath:the string storage list store the path of the dragged folder or file.
        strFilePath.ReleaseBuffer();
        
        hFile = FindFirstFile(strFilePath, &wfdFile);//get the feature of the dragged folder or file.
		if (hFile == INVALID_HANDLE_VALUE)
		{
			continue;
		}

		if (wfdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			FindFile(strFilePath);
		}
        else
        {
            if (strFilePath.Right(3).CollateNoCase(_T("scc")) == 0)
            {
                m_strltAddFileList.AddTail(strFilePath);
                ++m_iFileCount;
            }
        }
	}

    FindClose(hFile);
    DragFinish(hDropInfo);
    m_iFileSumCount += m_iFileCount;
	RefreshWindow();
}
void CSccKillerDlg::FindFile(CString strInitialPath)
{
	HANDLE hFile;
    BOOL bContinue = TRUE;
	WIN32_FIND_DATA wfdFile;
	strInitialPath += _T("\\");
	::SetCurrentDirectory(strInitialPath);
	hFile = FindFirstFile(_T("*.*"), &wfdFile);
	CString strFileName, strFullPath;

	if (hFile == INVALID_HANDLE_VALUE)
	{
		bContinue = FALSE;
	}

	while (bContinue)
	{
		strFileName = wfdFile.cFileName;
		strFullPath = strInitialPath + _T("\\") + strFileName;
		if ((wfdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			&& (wfdFile.cFileName[0] != _TEXT('.')))
		{
			::SetCurrentDirectory(wfdFile.cFileName);
			FindFile(strFullPath);
			SetCurrentDirectory(_T(".."));
		}
		else if (wfdFile.cFileName[0] != _TEXT('.'))
		{
            if (strFileName.Right(3).CollateNoCase(_T("scc")) == 0)
            {
                m_strltFileList.AddTail(strFullPath);
                ++m_iFileCount;
            }
		}
		bContinue = FindNextFile(hFile, &wfdFile);
	}
	FindClose(hFile);
}

void CSccKillerDlg::RefreshWindow(void)
{	
	CString strFileStatus;
	if (0 == m_iFileCount)
	{
		ShowMessage("Sorry, scc files not found !");
	}
	else
	{
        m_fileList.DeleteAllItems();
		POSITION position = m_strltFileList.GetHeadPosition();

		for (int i = 0; position != 0; ++i)
		{
			m_fileList.InsertItem(i, m_strltFileList.GetNext(position), 0);
		}

        position = m_strltAddFileList.GetHeadPosition();

		for (i = 0; position != 0; ++i)
		{
			m_fileList.InsertItem(i, m_strltAddFileList.GetNext(position), 0);
		}
	}
	m_strInfo.Format(_T("The number of .scc files is : %d "), m_iFileSumCount);
    UpdateData(FALSE);
}

void CSccKillerDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y));
	CDialog::OnLButtonDown(nFlags, point);
}

void CSccKillerDlg::OnTimer(UINT_PTR nIDEvent)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime;
	strTime.Format(_T("  %.2d : %.2d : %.2d"), 
		           time.GetHour(), 
				   time.GetMinute(), 
				   time.GetSecond());
	strTime = time.Format("%A  %B - %d  %Y  ") + strTime;
	GetDlgItem(IDC_TIME)->SetWindowText(strTime);
	CDialog::OnTimer(nIDEvent);
}

BOOL CSccKillerDlg::SetTransparent(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
	BOOL bRet = TRUE;
	if (m_hModule)
	{
		lpfnSetTran lpfnSetTrans = NULL;
		lpfnSetTrans = (lpfnSetTran)GetProcAddress(m_hModule, "SetLayeredWindowAttributes");

		if (lpfnSetTrans != NULL)
        {
			bRet = lpfnSetTrans(hWnd, crKey, bAlpha, dwFlags);
        }
		else 
        {
			bRet = FALSE;
        }
	}
	return bRet;
}

void CSccKillerDlg::CloseSmoothly()
{
	for(int nPercent = 100; nPercent >= 0; --nPercent)
	{
		SetTransparent(m_hWnd, 0, 255 * nPercent / 100, LWA_ALPHA);
	}
}

void CSccKillerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	/*

	CloseSmoothly();
	if (m_hModule)
	{
		::FreeLibrary(m_hModule);
	}
	*/

	CDialog::OnCancel();
}

⌨️ 快捷键说明

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