1、MFC实现简易数据类型转换器MFC实现简易数据类型转换器Free Copy最大支持32位无符号整数,支持直接拷贝数据,操作极为简便;先选择进制,键盘输入或拷贝进去一个数,再点击其它进制单选按钮,即可实现转换并显示,同时显示文本框获得焦点。项目文件名称:Convert完整实现文件代码:在类中,添加成员 jinzhi,用于记录当前正在显示的进制 添加函数 char *dectobin(unsigned long dec,int len) 用于将十进制整数转换为字符串,len为字符串长度/ ConvertDlg.cpp : implementation file/* 简易进制转换器*by 杜运福 *
2、/#include stdafx.h#include Convert.h#include ConvertDlg.h#include string.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic: CAboutDlg();/ Dialog Data /AFX_DATA(CAboutDlg) enum IDD = IDD_
3、ABOUTBOX ; /AFX_DATA / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL/ Implementationprotected: /AFX_MSG(CAboutDlg) /AFX_MSG DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAbo
4、utDlg:IDD) /AFX_DATA_INIT(CAboutDlg) /AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CAboutDlg) /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog) /AFX_MSG_MAP(CAboutDlg) / No message handlers /AFX_MSG_MAPEND_MESSAGE_MAP()/ CConvertDlg dialo
5、gCConvertDlg:CConvertDlg(CWnd* pParent /*=NULL*/) : CDialog(CConvertDlg:IDD, pParent) /AFX_DATA_INIT(CConvertDlg) m_Num = _T(); m_Ridio = 1; /初始化为十进制 /AFX_DATA_INIT / Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CConvertDlg:
6、DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CConvertDlg) DDX_Text(pDX, IDC_Num, m_Num); DDX_Radio(pDX, IDC_Hex, m_Ridio); /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CConvertDlg, CDialog) /AFX_MSG_MAP(CConvertDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLIC
7、KED(IDC_Hex, OnHex) /非别是事件映射 十六进制 ON_BN_CLICKED(IDC_Ten, OnTen) /十进制 ON_BN_CLICKED(IDC_Eight, OnEight)/八进制 ON_BN_CLICKED(IDC_Two, OnTwo) /二进制 /AFX_MSG_MAPEND_MESSAGE_MAP()/ CConvertDlg message handlers/BOOL CConvertDlg:OnInitDialog() CDialog:OnInitDialog(); / Add About. menu item to system menu. / I
8、DM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); / Set the icon for this dialog. The framework does this automatically / when the applications main
9、 window is not a dialog SetIcon(m_hIcon, TRUE); / Set big icon SetIcon(m_hIcon, FALSE); / Set small icon / TODO: Add extra initialization here jinzhi = 10; UpdateData(FALSE); return FALSE; / return TRUE unless you set the focus to a controlvoid CConvertDlg:OnSysCommand(UINT nID, LPARAM lParam) if (n
10、ID & 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 f
11、ramework.void CConvertDlg: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; GetClient
12、Rect(&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 CConvertDlg:OnQueryDragIcon()
13、 return (HCURSOR) m_hIcon;/*将十进制转换为二进制字符串*入口参数:dec:待转换的整数* len:目标二进制位数*出口参数:转换得到的字符串的指针*/char * CConvertDlg:dectobin(unsigned long dec,int len) int i=0; static char buf256; memset(buf,0,256); memset(buf,0,len); while(dec!=0) bufi+=dec%2+48; dec=dec/2; return strrev(buf);/十六进制void CConvertDlg:OnHex()
14、 UpdateData(); GetDlgItem(IDC_Num)-SetFocus(); /首先设置文本框为焦点 char *strnum; /用于存放文本框的字符串 unsigned long num=0,quan=1; /数值与权值 int i,len; /局部变量与字符长度 if(jinzhi != 16) /上次选择不是16进制时,才进行下面操作 strnum=new char100; /先分配内存 memset(strnum,0,99); len = m_Num.GetLength(); /获取字符串长度 m_Num.MakeReverse(); /先逆序,准备按权计算其十进制数
15、转换 strnum = (LPSTR)(LPCTSTR)m_Num; /强行转换为字符串 if(jinzhi = 10) /当前显示的是10进制? for(i=0;ilen;i+) num += (strnumi-48)*quan; quan *= 10; else if(jinzhi = 8) /当前显示的是8进制? for(i=0;ilen;i+) num += (strnumi-48)*quan; quan *= 8; else if(jinzhi = 2) /当前显示的是2进制? for(i=0;iSetFocus(); /首先设置文本框为焦点 char *strnum; unsign
16、ed long num=0,quan=1; int i,len; if(jinzhi != 10) strnum=new char100; memset(strnum,0,99); len = m_Num.GetLength(); m_Num.MakeReverse(); strnum = (LPSTR)(LPCTSTR)m_Num; if(jinzhi = 16) /当前显示的是16进制? for(i=0;i=0)&(strnumi= a)&(strnumi = A)&(strnumi = F) /进行大写字母的处理 num += (strnumi-A+10)*quan; quan *= 1
17、6; else if(jinzhi = 8) /当前显示的是8进制? for(i=0;ilen;i+) num += (strnumi-48)*quan; quan *= 8; else if(jinzhi = 2) /当前显示的是2进制? for(i=0;iSetFocus(); /首先设置文本框为焦点 char *strnum; unsigned long num=0,quan=1; int i,len; if(jinzhi != 8) strnum=new char100; memset(strnum,0,99); len = m_Num.GetLength(); m_Num.MakeR
18、everse(); strnum = (LPSTR)(LPCTSTR)m_Num; if(jinzhi = 16) for(i=0;i=0)&(strnumi= a)&(strnumi = A)&(strnumi = F) num += (strnumi-A+10)*quan; quan *= 16; else if(jinzhi = 10) for(i=0;ilen;i+) num += (strnumi-48)*quan; quan *= 10; else if(jinzhi = 2) for(i=0;iSetFocus(); /首先设置文本框为焦点 char *strnum; unsig
19、ned long num=0,quan=1; int i,len,temp; if(jinzhi != 2) strnum=new char100; memset(strnum,0,99); len = m_Num.GetLength(); m_Num.MakeReverse(); strnum = (LPSTR)(LPCTSTR)m_Num; if(jinzhi = 16) for(i=0;i=0)&(strnumi= a)&(strnumi = A)&(strnumi = F) num += (strnumi-A+10)*quan; quan *= 16; else if(jinzhi = 10) for(i=0;ilen;i+) num += (strnumi-48)*quan; quan *= 10; else if(jinzhi = 8) for(i=0;i 4) /从后向前依次每个4个二进制数插入一个空格符 m_Num.Insert(len-4, ); len -= 4; len = m_Num.GetLength(); while(temp%4) m_Num.Insert(0,0); /在最前端补0,使4位对齐 temp += 1; if(num = 0) m_Num = ; jinzhi = 2; UpdateData(FALSE);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1