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

📄 codetooldlg.cpp

📁 是一个数制转换的源码
💻 CPP
字号:
// CodeToolDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CodeTool.h"
#include "CodeToolDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCodeToolDlg dialog

CCodeToolDlg::CCodeToolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCodeToolDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCodeToolDlg)
	m_bin = _T("");
	m_dec = _T("");
	m_hex = _T("");
	m_bByte = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_binLast = _T("");
	m_decLast = _T("");
	m_hexLast = _T("");

	m_num = 0;
}

void CCodeToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCodeToolDlg)
	DDX_Text(pDX, IDC_BIN, m_bin);
	DDX_Text(pDX, IDC_DEC, m_dec);
	DDX_Text(pDX, IDC_HEX, m_hex);
	DDX_Check(pDX, IDC_CHECK1, m_bByte);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCodeToolDlg, CDialog)
	//{{AFX_MSG_MAP(CCodeToolDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_BIN, OnChangeBin)
	ON_EN_CHANGE(IDC_DEC, OnChangeDec)
	ON_EN_CHANGE(IDC_HEX, OnChangeHex)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCodeToolDlg message handlers

BOOL CCodeToolDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCodeToolDlg::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 CCodeToolDlg::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 CCodeToolDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/////////////////////////////////////////////////////////////////
// my code here

static TCHAR ValidChar[_MAX_TYPE][17] = 
{
	{ '0', '1', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0 },
	{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0,   0 },
	{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',   0}
};

static TCHAR SpaceChar[] =
{
	' ',  ',',  ';',  '_',  '\\',  '/',  '(',  ')',
	0
};

void CCodeToolDlg::OnChangeBin() 
{
	UpdateData(TRUE);
	if (!VerifyData(CCodeToolDlg::typeBin, m_bin))
	{
		m_bin = m_binLast;
		UpdateData(FALSE);
		MessageBeep(MB_ICONEXCLAMATION);
		return;
	}
	MakeDateString(CCodeToolDlg::typeDec);
	MakeDateString(CCodeToolDlg::typeHex);
	m_binLast = m_bin;
	UpdateData(FALSE);
}

void CCodeToolDlg::OnChangeDec() 
{
	UpdateData(TRUE);
	if (!VerifyData(CCodeToolDlg::typeDec, m_dec))
	{
		m_dec = m_decLast;
		UpdateData(FALSE);
		MessageBeep(MB_ICONEXCLAMATION);
		return;
	}
	MakeDateString(CCodeToolDlg::typeBin);
	MakeDateString(CCodeToolDlg::typeHex);
	m_decLast = m_dec;
	UpdateData(FALSE);
}

void CCodeToolDlg::OnChangeHex() 
{
	UpdateData(TRUE);
	if (!VerifyData(CCodeToolDlg::typeHex, m_hex))
	{
		m_hex = m_hexLast;
		UpdateData(FALSE);
		MessageBeep(MB_ICONEXCLAMATION);
		return;
	}
	MakeDateString(CCodeToolDlg::typeBin);
	MakeDateString(CCodeToolDlg::typeDec);
	m_hexLast = m_hex;
	UpdateData(FALSE);
}

BOOL CCodeToolDlg::VerifyData(int nDataType, CString str)
{
	CString cs1 = ValidChar[nDataType];
	CString cs2 = SpaceChar;
	str.MakeLower();

	int len = str.GetLength();
	for (int i = 0; i < len; i++)	
		if (cs1.Find(str[i]) == -1 && cs2.Find(str[i]) == -1)
			return FALSE;	

	TCHAR* temp = str.GetBuffer(len);
	TCHAR* tok;
	
	m_num = 0;
	tok = _tcstok(temp, SpaceChar);
	if (tok != NULL)
		do 
		{
			if (m_num >= 10)
			{
				AfxMessageBox(_T("输入数字过多"));
				return FALSE;
			}
			switch(nDataType)
			{
			case CCodeToolDlg::typeBin:
				m_data[m_num] = strBinToInt(tok);
				if (m_bByte && m_data[m_num] > 255)
				{
					AfxMessageBox(_T("字节值不能超过255"));
					return FALSE;
				}
				break;
			case CCodeToolDlg::typeDec:
				m_data[m_num] = _ttoi(tok);
				if (m_bByte && m_data[m_num] > 255)
				{
					AfxMessageBox(_T("字节值不能超过255"));
					return FALSE;
				}
				break;
			case CCodeToolDlg::typeHex:
				_stscanf(tok, _T("%x"), &(m_data[m_num]));
				if (m_bByte && m_data[m_num] > 255)
				{
					AfxMessageBox(_T("字节值不能超过255"));
					return FALSE;
				}
				break;
			}
			m_num++;
		}while ((tok = _tcstok(NULL, SpaceChar)) != NULL);

	str.ReleaseBuffer();

	return TRUE;
}

int CCodeToolDlg::strBinToInt(LPCTSTR str)
{
	int ret = 0, j;
	int len = _tcslen(str);
	for (int i = 0; i < len; i++)
	{
		j = 1;
		if (str[len - 1 - i] == '1')
		{
			j <<= i;
			ret += j;
		}
	}
	return ret;
}

void CCodeToolDlg::MakeDateString(int nDataType)
{
	int i;
	TCHAR temp[40];

	switch(nDataType)
	{
	case CCodeToolDlg::typeBin:
		m_bin = _T("");
		for (i = 0; i < m_num; i++)
		{
			intToStrBin(temp, m_data[i]);
			m_bin += temp;
			m_bin += _T(" ");
		}
		m_binLast = m_bin;		
		break;
	case CCodeToolDlg::typeDec:
		m_dec = _T("");
		for (i = 0; i < m_num; i++)
		{
			wsprintf(temp, _T("%d "), m_data[i]);
			m_dec += temp;
		}
		m_decLast = m_dec;
		break;
	case CCodeToolDlg::typeHex:
		m_hex = _T("");
		for (i = 0; i < m_num; i++)
		{
			if (m_bByte)
				wsprintf(temp, _T("%02X "), m_data[i]);
			else
				wsprintf(temp, _T("%x "), m_data[i]);
			m_hex += temp;
		}		
		m_hexLast = m_hex;
		break;
	}
}

void CCodeToolDlg::intToStrBin(LPTSTR str, int i)
{
	unsigned int j;
	unsigned int in = i;
	int pos = 0;

	if (in ==0)
	{
		if (m_bByte)
		{
			memset(str, '0', 8);
			str[8] = 0;
		}
		else
		{
			str[pos++] = '0';
			str[pos] = 0;
		}
		return;
	}

	if (m_bByte)
		j = 0x80;
	else 
	{
		j = 0x80000000;		
		while (j > in)
			j >>= 1;
	}

	while (j > 0)
	{
		if (in >= j)
		{
			str[pos++] = '1';
			in -= j;
		}
		else 
			str[pos++] = '0';		
		j >>= 1;
	}
	
	str[pos] = 0;
}

void CCodeToolDlg::OnAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();		
}

void CCodeToolDlg::OnCheck1() 
{
	m_bByte = !m_bByte;

	m_bin = _T("");
	m_dec = _T("");
	m_hex = _T("");	

	m_binLast = _T("");
	m_decLast = _T("");
	m_hexLast = _T("");

	m_num = 0;

	UpdateData(FALSE);
}

void CCodeToolDlg::OnExit() 
{
	CDialog::OnOK();	
}

void CCodeToolDlg::OnOK() 
{		
}

⌨️ 快捷键说明

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