Derive a new class from cedit that only allows the input of hexadecimal data

Source: Internet
Author: User

// Derive a new class from cedit that only allows the input of hexadecimal data
// Enter space 0-F backspace
// Switch the Input Method to the English keyboard during activation
// Restore the input method when the focus is lost
// A warning message is prompted in case of invalid characters
 

# Pragma once // chexeditctrlclass chexeditctrl: Public cedit {declare_dynamic (chexeditctrl) Public: chexeditctrl (); Virtual ~ Chexeditctrl (); protected: afx_msg void onensetfocus (); afx_msg void encrypt (); encrypt () virtual lresult windowproc (uint message, wparam, lparam); // input protected: HKL m_defaulthkl; HKL m_saved_kbdlayout; HKL getdefault_kbdlayout ();};

 

// Hexeditctrl. CPP: implementation file // # include "stdafx. H "# include" hexeditctrl. H "// decrypt (chexeditctrl, cedit) chexeditctrl: chexeditctrl () {m_defaulthkl = getdefault_kbdlayout (); m_saved_kbdlayout = 0;} chexeditctrl ::~ Chexeditctrl () {}← (chexeditctrl, cedit) ← (en_setfocus, & chexeditctrl: onensetfocus) ← (en_killfocus, & chexeditctrl: onenkillfocus) end_message_map () // chexeditctrl message handlerslresult chexeditctrl: windowproc (uint message, wparam, lparam) {// todo: add your specialized code here and/or call the base class // filter other characters // allow spaces 0-f backspaceif (M Essage = wm_char) {wchar szinput [2] = {(wchar) wparam, 0}; _ tcsupr_s (szinput); cstringw szvaliditem = l "0123456789abcdef \ B "; if (szvaliditem. find (szinput) =-1) {messagebeep (-1); // return 0;} wparam = szinput [0];} If (wm_inputlangchangerequest = message) // switch the input method message {return 0; // lock the input method // m_saved_kbdlayout = 0; // accept the change and do not need to restore the input method} return cedit: windowproc (message, wparam, lparam);} void chexeditctrl: onenset Focus () {// todo: add your control notification handler code hereif (m_defaulthkl) {HKL curkbdlayout = getkeyboardlayout (0); If (curkbdlayout! = M_defaulthkl) {m_saved_kbdlayout = curkbdlayout; // Save the input method if (activatekeyboardlayout (m_defaulthkl, 0) = 0) // set the Default Input Method to {trace (_ T ("set activatekeyboardlayout err = % d"), getlasterror () ;}}} void chexeditctrl: onenkillfocus () {// todo: add your control notification handler code hereif (m_saved_kbdlayout) // restore Input Method {If (activatekeyboardlayout (m_saved_kbdlayout, 0) = 0) trace (_ T ("Restore activatekey Boardlayout err = % d "), getlasterror (); m_saved_kbdlayout = 0 ;}// obtain the English keyboard HKL chexeditctrl: getdefault_kbdlayout () {HKL hkl_ret = 0; int nsize = getkeyboardlayoutlist (0, 0); If (nsize! = 0) {HKL far * lplist = (hkl far *) New HKL [nsize]; zeromemory (lplist, sizeof (hkl) * nsize); If (getkeyboardlayoutlist (nsize, lplist) = nsize) {for (INT I = 0; I <nsize; I ++) {HKL val = lplist [I]; If (hiword (VAL) = loword (VAL) hkl_ret = val; trace (_ T ("0x % 08x/N"), Val) ;}delete [] lplist;} return hkl_ret ;}

// Application example

// Mfc_1dlg.h: header file // # pragma once # include "hexeditctrl. H "// cmfc_dlg dialog box class cmfc_dlg: Public cdialog {protected: chexeditctrl m_edit; virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support

 

// Mfc_1dlg.cpp: implementation file // void cmfc_dlg: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX); ddx_control (PDX, idc_edit, m_edit );}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.