📄 crc32projdlg.cpp
字号:
// CRC32ProjDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CRC32Proj.h"
#include "CRC32ProjDlg.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCRC32ProjDlg dialog
CCRC32ProjDlg::CCRC32ProjDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCRC32ProjDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCRC32ProjDlg)
// 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 CCRC32ProjDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCRC32ProjDlg)
DDX_Control(pDX, IDC_PROGRESS, m_Pos);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCRC32ProjDlg, CDialog)
//{{AFX_MSG_MAP(CCRC32ProjDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
ON_BN_CLICKED(IDC_BTN_CALCULATION, OnBtnCalculation)
ON_EN_CHANGE(IDC_EDIT_DATA, OnChangeEditData)
ON_BN_CLICKED(IDC_CHECK_UPPER, OnCheckUpper)
ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
ON_WM_DROPFILES()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCRC32ProjDlg message handlers
BOOL CCRC32ProjDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("文件");
((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("字符串");
((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->SetCurSel(0);
AdjustWindow(true);
GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
((CButton*)GetDlgItem(IDC_CHECK_UPPER))->SetCheck(TRUE);
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 CCRC32ProjDlg::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 CCRC32ProjDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCRC32ProjDlg::AdjustWindow(bool bFlag)
{
RECT rcEdit;
RECT rcButton;
::GetWindowRect(GetDlgItem(IDC_EDIT_RESULT)->m_hWnd, &rcEdit);
::GetWindowRect(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, &rcButton);
if(!bFlag)
{
GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0,
(rcEdit.right - rcEdit.left) + (rcButton.right - rcButton.left),
rcEdit.bottom - rcEdit.top,
SWP_NOMOVE | SWP_NOOWNERZORDER);
GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_HIDE);
((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(false);
GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(false);
::DragAcceptFiles(m_hWnd, false);
}
else
{
GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0,
rcEdit.right - rcEdit.left,
rcEdit.bottom - rcEdit.top,
SWP_NOMOVE | SWP_NOOWNERZORDER);
GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_SHOW);
((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(true);
GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(true);
::DragAcceptFiles(m_hWnd, true);
}
GetDlgItem(IDC_COMBO_TYPE)->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE );
}
void CCRC32ProjDlg::OnSelchangeComboType()
{
SetDlgItemText(IDC_EDIT_DATA, "");
SetDlgItemText(IDC_EDIT_RESULT, "");
if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 0)
AdjustWindow(true);
else if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 1)
AdjustWindow(false);
}
void CCRC32ProjDlg::OnBtnAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CCRC32ProjDlg::OnBtnCalculation()
{
CString strButton;
GetDlgItemText(IDC_BTN_CALCULATION, strButton);
if(strButton == "停止(&S)")
{
SetDlgItemText(IDC_BTN_CALCULATION, "计算(&C)");
m_CRC32Ex.StopCalcFile();
return ;
}
CString strInput;
GetDlgItemText(IDC_EDIT_DATA, strInput);
if(strInput.IsEmpty())
{
SetDlgItemText(IDC_EDIT_RESULT, "");
return ;
}
GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_HIDE);
SetDlgItemText(IDC_BTN_CALCULATION, "停止(&S)");
SetDlgItemText(IDC_EDIT_RESULT, "");
void* proc = m_Thunk.Stdcall(this, Thunk::GetMemberFxnAddr(&CCRC32ProjDlg::ThreadProc));
HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)proc, 0, 0, 0 );
CloseHandle(hThread);
}
void CCRC32ProjDlg::OnChangeEditData()
{
if(((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel() == 1)
{
CString strInput;
GetDlgItemText(IDC_EDIT_DATA, strInput);
if(strInput.IsEmpty())
{
SetDlgItemText(IDC_EDIT_RESULT, "");
return ;
}
string strCRC32 = "";
if(((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck())
strCRC32 = m_CRC32Ex.GetCRC32FromBuffer((unsigned char *)(LPCTSTR)strInput, strInput.GetLength(), true);
else
strCRC32 = m_CRC32Ex.GetCRC32FromBuffer((unsigned char *)(LPCTSTR)strInput, strInput.GetLength(), false);
SetDlgItemText(IDC_EDIT_RESULT, strCRC32.c_str());
}
}
void CCRC32ProjDlg::OnCheckUpper()
{
CString strInput;
GetDlgItemText(IDC_EDIT_RESULT, strInput);
if(strInput.IsEmpty())
return ;
if(((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck())
SetDlgItemText(IDC_EDIT_RESULT, CharUpper((LPSTR)(LPCTSTR)strInput));
else
SetDlgItemText(IDC_EDIT_RESULT, CharLower((LPSTR)(LPCTSTR)strInput));
}
void CCRC32ProjDlg::OnProcess(int nProgress)
{
m_Pos.SetPos(nProgress);
}
void CCRC32ProjDlg::OnBtnBrowse()
{
FileDlg dlg;
vector<string> Files = dlg.GetOpenFileName(m_hWnd, false);
if(Files.size() > 0)
SetDlgItemText(IDC_EDIT_DATA, Files[0].c_str());
}
DWORD CCRC32ProjDlg::ThreadProc( LPVOID lpParameter )
{
CString strInput;
GetDlgItemText(IDC_EDIT_DATA, strInput);
if(strInput.IsEmpty())
{
SetDlgItemText(IDC_EDIT_RESULT, "");
return 0;
}
MemberFxn Addr(this, &CCRC32ProjDlg::OnProcess);
string strCRC32 = "";
if(((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck())
strCRC32 = m_CRC32Ex.GetCRC32FromFile((char*)(LPCTSTR)strInput, true, Addr);
else
strCRC32 = m_CRC32Ex.GetCRC32FromFile((char*)(LPCTSTR)strInput, false, Addr);
string strErr = m_CRC32Ex.GetError();
if(strCRC32.empty() || !strErr.empty())
{
AfxMessageBox(strErr.c_str());
GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);
SetDlgItemText(IDC_BTN_CALCULATION, "计算(&C)");
return 0;
}
SetDlgItemText(IDC_EDIT_RESULT, strCRC32.c_str());
AfxMessageBox("计算完成");
GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);
SetDlgItemText(IDC_BTN_CALCULATION, "计算(&C)");
return 0;
}
void CCRC32ProjDlg::OnDropFiles(HDROP hDropInfo)
{
SetDlgItemText(IDC_EDIT_DATA, "");
SetDlgItemText(IDC_EDIT_RESULT, "");
int nFiles = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, NULL);
if(nFiles > 0)
{
char buf[MAX_PATH] = { 0 };
memset(buf, 0, MAX_PATH);
::DragQueryFile(hDropInfo, 0, buf, sizeof(buf));
// if(GetFileAttributes(buf) == FILE_ATTRIBUTE_DIRECTORY)
// SetDlgItemText(IDC_EDIT_DATA, "");
// else
// SetDlgItemText(IDC_EDIT_DATA, buf);
WIN32_FIND_DATA data;
HANDLE hFile = ::FindFirstFile(buf, &data);
if(hFile == INVALID_HANDLE_VALUE)
return ;
CloseHandle(hFile);
if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return ;
SetDlgItemText(IDC_EDIT_DATA, buf);
}
CDialog::OnDropFiles(hDropInfo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -