PostHeaderIcon A MFC Number Edit

To write A number edit class  in MFC is simple, but to achieve the class like the system itself is a little difficult.

let us see a demo with picture:

number edit

number edit

This edit is same with windows system edit, it only support decimal number.

Actually, it is easy, just a utility class.

Let us see the definition of  CNumberEdit

class CNumberEdit : public CEdit , public CDecimalSupport
{
protected:
	afx_msg void OnChar( UINT nChar, UINT nRepCnt, UINT nFlags );
	DECLARE_MESSAGE_MAP()
};

In the above code fragments, you can find a new Class, CDecimalSupport this one is really behind the attacks:

class CDecimalSupport
{
public:
	/// the locale dependant decimal separator
	TCHAR m_DecimalSeparator[5];
	/// the locale dependant negative sign
	TCHAR m_NegativeSign[6];

#ifdef BEGIN_MSG_MAP
	BEGIN_MSG_MAP(CDecimalSupport)

		ALT_MSG_MAP(8)
		MESSAGE_HANDLER(WM_CHAR, OnChar)
		MESSAGE_HANDLER(WM_PASTE, OnPaste)
	END_MSG_MAP()
#endif

        /// \brief Initialize m_DecimalSeparator and m_NegativeSign
	/// \remarks calls InitDecimalSeparator and InitNegativeSign
	CDecimalSupport();

	/// \brief sets m_DecimalSeparator
	/// \remarks calls GetLocaleInfo with LOCALE_SDECIMAL to set m_DecimalSeparator
	/// \param[in] Locale the locale parameter (see GetLocaleInfo)
	/// \return the number of TCHARs written to the destination buffer
	int InitDecimalSeparator(LCID Locale = LOCALE_USER_DEFAULT);

	/// \brief sets m_NegativeSign
	/// \remarks calls GetLocaleInfo with LOCALE_SNEGATIVESIGN to set m_NegativeSign
	/// \param[in] Locale the locale parameter (see GetLocaleInfo)
	/// \return the number of TCHARs written to the destination buffer
	int InitNegativeSign(LCID Locale = LOCALE_USER_DEFAULT);

	/// callback for the WM_PASTE message
	/// validates the input
	/// \param uMsg
	/// \param wParam
	/// \param lParam
	/// \param[out] bHandled true, if the text is a valid number
	/// \return 0
	LRESULT OnPaste(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/,BOOL& bHandled);

	/// callback for the WM_CHAR message
	/// handles the decimal point and the negative sign keys
	/// \param uMsg
	/// \param[in] wParam contains the pressed key
	/// \param lParam
	/// \param[out] bHandled true, if the key press was handled in this function
	/// \return 0
	LRESULT OnChar(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled);

        /// converts the controls text to double
	/// \param[out] d the converted value
	/// \return true on success
	bool GetDecimalValue(double& d) const;

        /// converts a string to double
	/// \remarks the decimal separator and the negative sign may change in the string
	/// \param[in, out] szBuff the string to convert
	/// \param[out] d the converted value
	/// \return true on success
	bool TextToDouble(TCHAR* szBuff, double& d) const;

	/// sets a number as the controls text
	/// \param[in] d the value
	/// \param[in] count digits after the decimal point
	void SetFixedValue(double d, int count);

	/// sets a number as the controls text
	/// \param[in] d the value
	/// \param[in] count total number of digits
	void SetDecimalValue(double d, int count);

	/// sets a number as the controls text
	/// \param[in] d the value
	/// \remarks the total number of digits is calculated using the GetLimitText function
	void SetDecimalValue(double d);

	/// sets the controls text
	/// \param[in] digits array containing the digits
	/// \param[in] decimal_pos the position of the decimal point
	/// \param[in] sign 1 if negative
	void DisplayDecimalValue(const char* digits, int decimal_pos, int sign);

	/// convert a digit array to string
	/// \param[out] szBuff target buffer for output
	/// \param[in] buflen maximum characters in output buffer
	/// \param[in] digits array containing the digits
	/// \param[in] decimal_pos the position of the decimal point
	/// \param[in] sign 1 if negative
	void DecimalToText(TCHAR* szBuff, size_t buflen ,
        const char* digits, int decimal_pos, int sign) const;
};

Here is the source CDecimalSupport Class, you can download it and use it in your project~

112,336 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com