📄 makedictdlg.cpp
字号:
#include "stdafx.h"
#include "MakeDict.h"
#include "MakeDictDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct MAKEPASS_INFO
{
double dCount;
double dNo;
CString strCurWord;
CString strWord;
};
#define MAKEPASS_INFO_MSG WM_USER + 0xAA
static char g_chDefaultWord[] = "abcdefghijklmnopqrstuvwxyz0123456789";
static char *g_chWord = NULL;
static MAKEPASS_INFO g_mpInfo;
int MakePass(int nLen, CWnd *pWnd)
{
int nRet = 0;
int nCount = strlen(g_chWord);
double nTotal = pow(nCount, nLen);
char *chPass = new char[nLen+1];
int *nIndex = new int[nLen];
ZeroMemory(nIndex, sizeof(int)*nLen);
ZeroMemory(chPass, nLen);
CString strTemp;
for(double i = 0; i < nTotal; i ++)
{
for(int j = 0; j < nLen; j ++)
{
if(nIndex[j] == nCount)
{
nIndex[j] = 0;
nIndex[j+1] ++;
}
}
for(j = 0; j < nLen; j ++)
{
chPass[nLen - j - 1] = g_chWord[nIndex[j]];
}
chPass[nLen] = '\0';
nIndex[0] ++;
strTemp += ((CString)(chPass));
strTemp += _T("\n");
g_mpInfo.dNo ++;
if(pWnd && (((int)i)%1000) == 0)
{
g_mpInfo.strCurWord = CString(chPass);
g_mpInfo.strWord = strTemp;
::SendMessage(pWnd->GetSafeHwnd(), MAKEPASS_INFO_MSG, 0, 0);
strTemp = _T("");
}
}
if(pWnd)
{
g_mpInfo.strCurWord = CString(chPass);
g_mpInfo.strWord = strTemp;
::SendMessage(pWnd->GetSafeHwnd(), MAKEPASS_INFO_MSG, 0, 0);
strTemp = _T("");
}
delete []nIndex;
delete []chPass;
return nRet;
}
UINT MakePassThread(LPVOID lParam)
{
if(lParam)
{
g_mpInfo.dNo = 0;
CMakeDictDlg *pDlg = (CMakeDictDlg *)lParam;
for(int i = pDlg->m_nStart; i <= pDlg->m_nEnd; i ++)
{
MakePass(i, pDlg);
}
::SendMessage(pDlg->GetSafeHwnd(), MAKEPASS_INFO_MSG, 1, 0);
}
return 0;
}
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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMakeDictDlg::CMakeDictDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMakeDictDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMakeDictDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bIsDoing = FALSE;
m_pMakeThread = NULL;
}
void CMakeDictDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMakeDictDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMakeDictDlg, CDialog)
//{{AFX_MSG_MAP(CMakeDictDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_FILE, OnBtnFile)
ON_EN_CHANGE(IDC_EDIT_LEN, OnChangeEditLen)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_CBN_SELCHANGE(IDC_COMBO_START, OnSelchangeComboStart)
ON_EN_CHANGE(IDC_EDIT_PASS, OnChangeEditLen)
//}}AFX_MSG_MAP
ON_MESSAGE(MAKEPASS_INFO_MSG, OnMsgMakeInfo)
END_MESSAGE_MAP()
BOOL CMakeDictDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
SetDlgItemText(IDC_EDIT_FILE, _T("c:\\dict.txt"));
SetDlgItemText(IDC_EDIT_PASS, g_chDefaultWord);
SetDlgItemText(IDC_EDIT_LEN, _T("6"));
((CEdit *)GetDlgItem(IDC_EDIT_LEN))->SetLimitText(1);
((CEdit *)GetDlgItem(IDC_EDIT_PASS))->SetLimitText(99);
return TRUE;
}
void CMakeDictDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CMakeDictDlg::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 CMakeDictDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMakeDictDlg::OnOK()
{
if(!m_bIsDoing)
{
if(ReadWord() <= 0)
{
MessageBox(_T("没有密码字符!请重新输入!"));
return ;
}
CString strFile;
GetDlgItemText(IDC_EDIT_FILE, strFile);
if(strFile.GetLength() <= 0)
{
MessageBox(_T("请指定输出字典文件名!请重新输入!"));
return ;
}
if(m_file.m_hFile != CFile::hFileNull)
{
m_file.Close();
}
if(m_file.Open(strFile, CFile::modeCreate | CFile::modeWrite))
{
CComboBox *pCom = (CComboBox *)GetDlgItem(IDC_COMBO_START);
int nIndex = pCom->GetCurSel();
if(nIndex >= 0)
{
m_nStart = pCom->GetItemData(nIndex);
ASSERT(m_nStart > 0 && m_nStart <= m_nEnd);
if(!(m_nStart > 0 && m_nStart <= m_nEnd))
{
MessageBox(_T("哈哈,程序有BUG!给我写信吧!"));
return ;
}
}
m_bIsDoing = TRUE;
SetDlgItemText(IDOK, _T("停止(&M)"));
EnableWnd();
m_pMakeThread = AfxBeginThread(MakePassThread, this);
}
else
{
MessageBox(_T("写文件错误!请确认文件名!"));
}
}
else
{
StopThread();
SetDlgItemText(IDOK, _T("生成(&M)"));
m_bIsDoing = FALSE;
EnableWnd();
if(m_file.m_hFile != CFile::hFileNull)
{
m_file.Close();
}
}
}
int CMakeDictDlg::ReadWord()
{
CString str;
GetDlgItemText(IDC_EDIT_PASS, str);
int nLen = str.GetLength();
if(nLen > 0)
{
if(g_chWord != NULL)
{
delete []g_chWord;
g_chWord = NULL;
}
g_chWord = new char[nLen + 1];
ZeroMemory(g_chWord, nLen + 1);
sprintf(g_chWord, "%s", str);
}
return nLen;
}
void CMakeDictDlg::OnCancel()
{
if(g_chWord != NULL)
{
delete []g_chWord;
g_chWord = NULL;
}
StopThread();
CDialog::OnCancel();
}
void CMakeDictDlg::OnBtnFile()
{
CFileDialog fileDlg(FALSE);
if(fileDlg.DoModal() == IDOK)
{
SetDlgItemText(IDC_EDIT_FILE, fileDlg.GetPathName());
}
}
void CMakeDictDlg::OnChangeEditLen()
{
GetCount();
}
void CMakeDictDlg::StopThread()
{
if(m_pMakeThread)
{
::TerminateThread(m_pMakeThread->m_hThread, 0);
::WaitForSingleObject(m_pMakeThread->m_hThread, INFINITE);
m_pMakeThread->m_hThread = NULL;
m_pMakeThread = NULL;
}
}
HRESULT CMakeDictDlg::OnMsgMakeInfo(WPARAM wParam, LPARAM)
{
if(wParam != 1)
{
CString str;
double dRem = g_mpInfo.dCount - g_mpInfo.dNo;
if(dRem > 999999999999.0f)
{
str = _T("Overlong!");
}
else
{
str.Format(_T("%.0lf"), dRem);
}
SetDlgItemText(IDC_STATIC_REM, str);
SetDlgItemText(IDC_STATIC_CURPASS, g_mpInfo.strCurWord);
if(m_file.m_hFile != CFile::hFileNull)
{
m_file.WriteString(g_mpInfo.strWord);
}
}
else
{
SetDlgItemText(IDOK, _T("生成(&M)"));
m_bIsDoing = FALSE;
EnableWnd();
if(m_file.m_hFile != CFile::hFileNull)
{
m_file.Close();
}
}
return 0;
}
void CMakeDictDlg::OnBtnSave()
{
}
void CMakeDictDlg::OnSelchangeComboStart()
{
CComboBox *pCom = (CComboBox *)GetDlgItem(IDC_COMBO_START);
int nIndex = pCom->GetCurSel();
if(nIndex >= 0)
{
m_nStart = pCom->GetItemData(nIndex);
}
CString str;
GetDlgItemText(IDC_EDIT_PASS, str);
int nLen = str.GetLength();
double dTotal = 0.0f;
for(int i = m_nStart; i <= m_nEnd; i ++)
{
dTotal += pow(nLen, i);
}
g_mpInfo.dCount = dTotal;
if(dTotal > 999999999999.0f)
{
str = _T("Overlong!");
}
else
{
str.Format(_T("%.0lf"), dTotal);
}
SetDlgItemText(IDC_STATIC_COUNT, str);
SetDlgItemText(IDC_STATIC_REM, str);
}
void CMakeDictDlg::GetCount()
{
m_nEnd = GetDlgItemInt(IDC_EDIT_LEN);
CComboBox *pCom = (CComboBox *)GetDlgItem(IDC_COMBO_START);
pCom->ResetContent();
for(int i = 1; i <= m_nEnd; i ++)
{
CString str;
str.Format(_T("%d"), i);
int nIndex = pCom->AddString(str);
if(nIndex >= 0)
{
pCom->SetItemData(nIndex, i);
}
}
pCom->SetCurSel(pCom->GetCount() - 1);
CString str;
GetDlgItemText(IDC_EDIT_PASS, str);
int nLen = str.GetLength();
double dTotal = pow(nLen, m_nEnd);
if(m_nEnd == 0)
{
dTotal = 0;
}
g_mpInfo.dCount = dTotal;
if(dTotal > 999999999999.0f)
{
str = _T("Overlong!");
}
else
{
str.Format(_T("%.0lf"), dTotal);
}
SetDlgItemText(IDC_STATIC_COUNT, str);
SetDlgItemText(IDC_STATIC_REM, str);
}
void CMakeDictDlg::EnableWnd()
{
GetDlgItem(IDC_EDIT_FILE)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_BTN_OPEN)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_BTN_SAVE)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_BTN_FILE)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_EDIT_LEN)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_COMBO_START)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDC_EDIT_PASS)->EnableWindow(!m_bIsDoing);
GetDlgItem(IDCANCEL)->EnableWindow(!m_bIsDoing);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -