www.gusucode.com > VC++进制转换组件及示例-源码程序 > VC++进制转换组件及示例-源码程序/code/ConvertDemo/ConvertDemoDlg.cpp

    //////////////////////////////////////////////////////////////////////////////
//类名:CConvertDemoDlg
//功能:多种进制转换组件示例程序(客户端)
//下载:http://www.NewXing.com
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期:2003.1.1
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ConvertDemo.h"
#include "ConvertDemoDlg.h"

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

#include "..\ConvertNum\ConvertNum.h"
#include "..\ConvertNum\ConvertNum_i.c"
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CPictureEx	m_Flag;				// GIF动态图像显示
	CHyperLink	m_Mail;				// 超链接形式显示EMAIL
	CXPButton	m_OK;				// XP风格按钮
	//}}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)
	virtual BOOL OnInitDialog();
	//}}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)
	DDX_Control(pDX, IDC_FLAG, m_Flag);
	DDX_Control(pDX, IDC_STATIC_MAIL, m_Mail);
	DDX_Control(pDX, IDOK, m_OK);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////////////
//名称:OnInitDialog
//功能:初始化"关于"对话框
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期:2002.12.1
/////////////////////////////////////////////////////////////////////////////
BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//加入EMAIL的超连接
    m_Mail.SetURL(_T("mailto:jingzhou_xu@163.net"));
	m_Mail.SetUnderline(FALSE);	

	//显示动态GIF图像logo
	if(m_Flag.Load(MAKEINTRESOURCE(IDR_FLAG),_T("GIF")))
	{
		m_Flag.SetBkColor(RGB(160,180,220));
		m_Flag.Draw();	
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////
// CConvertDemoDlg dialog

CConvertDemoDlg::CConvertDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConvertDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConvertDemoDlg)
	m_InputNum = _T("");
	m_OutputResult = _T("");
	m_Input = 2;
	m_Output = 2;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConvertDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConvertDemoDlg)
	DDX_Control(pDX, ID_ABOUT, m_About);
	DDX_Control(pDX, IDC_OUTPUTRESULT, m_OutputEdit);
	DDX_Control(pDX, IDC_INPUTNUM, m_InputEdit);
	DDX_Control(pDX, IDOK, m_Convert);
	DDX_Control(pDX, IDCANCEL, m_Exit);
	DDX_Text(pDX, IDC_INPUTNUM, m_InputNum);
	DDV_MaxChars(pDX, m_InputNum, 30);
	DDX_Text(pDX, IDC_OUTPUTRESULT, m_OutputResult);
	DDV_MaxChars(pDX, m_OutputResult, 30);
	DDX_Radio(pDX, IDC_INPUT2, m_Input);
	DDX_Radio(pDX, IDC_OUTPUT2, m_Output);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConvertDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CConvertDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConvertDemoDlg message handlers

BOOL CConvertDemoDlg::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

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

////////////////////////////////////////////////////////////////////////////
//名称:OnOK
//功能:转换各种进制数值
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期:2003.1.1
/////////////////////////////////////////////////////////////////////////////
void CConvertDemoDlg::OnOK() 
{
	// 传值给成员变量
	UpdateData(TRUE);
	if(m_InputNum == "")
	{
		AfxMessageBox("输入数值不能为空");
		return;
	}

	// 创建进行转换组件实例
	CComPtr<IConvert> ptrConvert;
	HRESULT hr;
	hr = CoCreateInstance(CLSID_Convert,
						  NULL,
						  CLSCTX_SERVER,
						  IID_IConvert,
						  (void**)&ptrConvert);

	if(FAILED(hr))
	{
		AfxMessageBox("创建IConvert实例失败!");
		return ;
	}

	CComVariant varStr(m_InputNum);
	hr = varStr.ChangeType(VT_BSTR);
	if(FAILED(hr))
	{
		AfxMessageBox("输入数值类型转换失败!");
		return ;
	}

	// 检验输入数值类型是否正确
	int nInput = 0;
	switch(m_Input)					// 输入进制
	{
	case 0:
		nInput = 2;
		hr = ptrConvert->IsValidate(varStr,nInput,m_InputNum.GetLength());
		break;
	case 1:
		nInput = 8;
		hr = ptrConvert->IsValidate(varStr,nInput,m_InputNum.GetLength());
		break;
	case 2:
		nInput = 10;
		hr = ptrConvert->IsValidate(varStr,nInput,m_InputNum.GetLength());
		break;
	case 3:
		nInput = 16;
		hr = ptrConvert->IsValidate(varStr,nInput,m_InputNum.GetLength());
		break;
	}

	if(hr == S_FALSE)
	{
		AfxMessageBox("转换数值及其输入进制类型不匹配!");
		return ;
	}

	// 进行数制间转换
	VARIANT varResult;
	VariantInit(&varResult);
	hr = VariantChangeType(&varResult,&varResult,0,VT_BSTR);
	if(FAILED(hr))
	{
		AfxMessageBox("输出数值类型转换失败!");
		return ;
	}

	switch(m_Output)				// 输出进制
	{
	case 0:
		hr = ptrConvert->Convert(varStr,nInput,2,m_InputNum.GetLength(),&varResult);
		break;
	case 1:
		hr = ptrConvert->Convert(varStr,nInput,8,m_InputNum.GetLength(),&varResult);
		break;
	case 2:
		hr = ptrConvert->Convert(varStr,nInput,10,m_InputNum.GetLength(),&varResult);
		break;
	case 3:
		hr = ptrConvert->Convert(varStr,nInput,16,m_InputNum.GetLength(),&varResult);
		break;
	}

	if(&varResult)
	{
		USES_CONVERSION;
		m_OutputResult = OLE2T(varResult.bstrVal);

	}

	// 传成员变量值到相应控件中
	UpdateData(FALSE);
	
}

////////////////////////////////////////////////////////////////////////////
//名称:OnAbout
//功能:弹出"关于"对话框
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期:2003.1.1
/////////////////////////////////////////////////////////////////////////////
void CConvertDemoDlg::OnAbout() 
{
	CAboutDlg dlg;

	dlg.DoModal();
}

BOOL CConvertDemoDlg::DestroyWindow() 
{
	return CDialog::DestroyWindow();
}