Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/10/2010, 08:32
Asaak^
 
Fecha de Ingreso: agosto-2007
Mensajes: 20
Antigüedad: 16 años, 9 meses
Puntos: 1
obtener valor big5/gb232 de un edit box

Hola a todos,

Dispongo de una aplicación muy senzilla. Tengo un diálogo en el cual aparece un editbox. A este editbox le he de escribir caracteres chinos. Esto ya lo he conseguido, pero ahora me gustaría obtener el valor hexadecimal en la codificación gb232 o en su defecto en big5 de cada uno de los caracteres que contiene el editbox.

En el diálogo dispongo de un boton de aceptar y otro de cancelar que son los que venian por defecto y simplemente sirven para salir de la aplicación. Luego dispongo de un editbox y de un boton que cuando lo apreto ejecuta la rutina onbutton1.

El código és el siguiente:

Código:
// unicodeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "unicode.h"
#include "unicodeDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CUnicodeDlg dialog

CUnicodeDlg::CUnicodeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUnicodeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUnicodeDlg)
		// 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 CUnicodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUnicodeDlg)
	DDX_Control(pDX, IDC_EDIT, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUnicodeDlg, CDialog)
	//{{AFX_MSG_MAP(CUnicodeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUnicodeDlg message handlers

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

	// set controls font
	SetControlsFont();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CUnicodeDlg::SetControlsFont()
{
	// create UNICODE Font and apply it to the controls.	
	CDC *pDC = GetDC();
	
	// create UNICODE font
	LOGFONT	lf;
	memset(&lf, 0, sizeof(lf));			
	lf.lfHeight = MulDiv(20, ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY), 72);
	lf.lfWeight = FW_NORMAL;
	lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
	lf.lfCharSet = GB2312_CHARSET;
	wcscpy(lf.lfFaceName, _T("SimSun"));	
	m_font.CreateFontIndirect(&lf);
	
	// apply the font to the controls
	m_edit.SetFont(&m_font);

	// release the device context.
	ReleaseDC(pDC);
}

void CUnicodeDlg::SetControlsText(LPCTSTR lpszFileName, CListCtrl &list, CEdit &edit)
{
		
	CFile file;

	// open the file and read the Unicode characters
	// input file has to be encoded with UTF-16 little endian
	// if the file was saved as described above this condition is already fulfilled
	TRY
	{
		file.Open(lpszFileName, CFile::modeRead);
		CString strText;
		UINT nRes = file.Read(strText.GetBuffer(1024), 1024);
		strText.ReleaseBuffer(nRes / 2);
		
		// test to see if the file is UNICODE
		if(strText.GetAt(0) == 0xFEFF) 
		{
			// insert a column in the list control
			list.InsertColumn(0, _T("Unicode characters"), LVCFMT_LEFT, 300);

			// display characters in the list control
			list.InsertItem(0, strText); 

			// display characters in the edit control
			edit.SetWindowText(strText); 			

			// close file
			file.Close();
		}
		else
			AfxMessageBox(_T("The file is not UNICODE."));
	}
	CATCH(CFileException, e)
	{
		e->ReportError();
	}
	END_CATCH
}

void CUnicodeDlg::OnButton1() 
{
	wchar_t a;

	m_edit.GetWindowTextW(&a,2);	

	a=38463;
//	m_edit.SetWindowTextW(&a);	

}
No se si es correcto o no pero me gustaría obtener el valor en hexadecimal del símbolo en la variable a, de tal modo que si luego quiero dibujar el simbolo correspondiente a ese valor lo muestre correctamente de nuevo.

De momento obtengo valores como el de a=38463..pero para nada es correcto.

Alguna idea? muchas gracias!