MFC实现简易数据类型转换器Word文件下载.docx
《MFC实现简易数据类型转换器Word文件下载.docx》由会员分享,可在线阅读,更多相关《MFC实现简易数据类型转换器Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);
//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
IDD)
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
DoDataExchange(CDataExchange*pDX)
CDialog:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//CConvertDlgdialog
CConvertDlg:
CConvertDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CConvertDlg:
IDD,pParent)
//{{AFX_DATA_INIT(CConvertDlg)
m_Num=_T("
"
);
m_Ridio=1;
//初始化为十进制
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->
LoadIcon(IDR_MAINFRAME);
voidCConvertDlg:
//{{AFX_DATA_MAP(CConvertDlg)
DDX_Text(pDX,IDC_Num,m_Num);
DDX_Radio(pDX,IDC_Hex,m_Ridio);
BEGIN_MESSAGE_MAP(CConvertDlg,CDialog)
//{{AFX_MSG_MAP(CConvertDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Hex,OnHex)//非别是事件映射十六进制
ON_BN_CLICKED(IDC_Ten,OnTen)//十进制
ON_BN_CLICKED(IDC_Eight,OnEight)//八进制
ON_BN_CLICKED(IDC_Two,OnTwo)//二进制
//CConvertDlgmessagehandlers
////////////////////////////////////////////////////////////////////////////
BOOLCConvertDlg:
OnInitDialog()
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
//TODO:
Addextrainitializationhere
jinzhi=10;
UpdateData(FALSE);
returnFALSE;
//returnTRUEunlessyousetthefocustoacontrol
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
OnPaint()
if(IsIconic())
CPaintDCdc(this);
//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&
rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
OnPaint();
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCConvertDlg:
OnQueryDragIcon()
return(HCURSOR)m_hIcon;
/************************************************
**将十进制转换为二进制字符串
**入口参数:
dec:
待转换的整数
**len:
目标二进制位数
**出口参数:
转换得到的字符串的指针
************************************************/
char*CConvertDlg:
dectobin(unsignedlongdec,intlen)
inti=0;
staticcharbuf[256];
memset(buf,0,256);
memset(buf,0,len);
while(dec!
=0)
buf[i++]=dec%2+48;
dec=dec/2;
returnstrrev(buf);
//十六进制
OnHex()
UpdateData();
GetDlgItem(IDC_Num)->
SetFocus();
//首先设置文本框为焦点
char*strnum;
//用于存放文本框的字符串
unsignedlongnum=0,quan=1;
//数值与权值
inti,len;
//局部变量与字符长度
if(jinzhi!
=16){//上次选择不是16进制时,才进行下面操作
strnum=newchar[100];
//先分配内存
memset(strnum,0,99);
len=m_Num.GetLength();
//获取字符串长度
m_Num.MakeReverse();
//先逆序,准备按权计算其十进制数转换
strnum=(LPSTR)(LPCTSTR)m_Num;
//强行转换为字符串
if(jinzhi==10)//当前显示的是10进制?
for(i=0;
i<
len;
i++)
{
num+=(strnum[i]-48)*quan;
quan*=10;
elseif(jinzhi==8)//当前显示的是8进制?
quan*=8;
elseif(jinzhi==2)//当前显示的是2进制?
if((strnum[i]=='
0'
)||(strnum[i]=='
1'
))
{
num+=(strnum[i]-48)*quan;
quan*=2;
}
if(num!
=0)
strnum=ultoa(num,strnum,16);
//用函数将整数转换成16进制形式字符串
m_Num=strnum;
elsem_Num="
;
}//将数值转换为16进制到此完成
m_Num.MakeUpper();
//字符串中小写字母大写输出
jinzhi=16;
//十进制
OnTen()
=10){
if(jinzhi==16)//当前显示的是16进制?
if((strnum[i]>
='
)&
&
(strnum[i]<
9'
num+=(strnum[i]-'
)*quan;
elseif((strnum[i]>
='
a'
(strnum[i]<
f'
))//进行小写字母的处理
+10)*quan;
A'
F'
))//进行大写字母的处理
quan*=16;
strnum=ultoa(num,strnum,10);
//用函数将整数转换成10进制形式字符串
}//将数值转换为10进制到此完成
//八进制
OnEight()
=8){
if(jinzhi==16)
elseif(jinzhi==10)
elseif(jinzhi==2)
strnum=ultoa(num,strnum,8);
//用函数将整数转换成8进制形式字符串
jinzhi=8;
//二进制
OnTwo()
inti,len,temp;
=2){
elseif(jinzhi==8)
strnum=dectobin(num,36);
//八位十六进制为32位二进制
temp=len;
while(len>
4)//从后向前依次每个4个二进制数插入一个空格符
m_Num.Insert(len-4,'
'
len-=4;
while(temp%4)
m_Num.Insert(0,'
//在最前端补0,使4位对齐
temp+=1;
if(num==0)m_Num="
jinzhi=2;