Add your own alternative version
Stats
92.8K views 3.7K downloads 31 bookmarked
Posted
25 Jan 2005
|
Comments and Discussions
|
|
I do some chang,
I add a member 'last' to the CHexEdit,this member count the hex code from keyboard,when 'last' equal 2,I add a space after the two hex code,but i can't get this feature.
the code below:
if (pMsg->message == WM_CHAR)
{
int c=(int)pMsg->wParam;
if(pMsg->wParam<32)
{
if(pMsg->wParam==8&&(second=='x'||second=='X')&&head=='0'&&end==1)
return TRUE;
else
return CEdit::PreTranslateMessage(pMsg);
}
if(second=='x'||second=='X')
{
if(start<=1&&end<=1) return TRUE;
}
if(start==1&&(c=='X'||c=='x')&&head=='0') {pMsg->wParam='x';return CEdit::PreTranslateMessage(pMsg);}
else if(c>=48&&c<=57||c>='A'&&c<='F')
{
last++;
if(last==2)
{
last=0;
SendMessage(WM_CHAR,32,0);
}
return CEdit::PreTranslateMessage(pMsg);
}
else if(c>='a'&&c<='f')
{
last++;
if(last==2)
{
last=0;
SendMessage(WM_CHAR,32,0);
}
pMsg->wParam-=32;
return CEdit::PreTranslateMessage(pMsg);
}
else return TRUE;
}
the first team hex code always can't get the right space.
|
|
|
|
|
when you defined
_UNICODE
UNICODE
....
You should use char & TCHAR carefully.
|
|
|
|
|
I'm having troubles adding your CHexEdit Control to my own test project (your test project compiles and works fine). If I add HexEdit.cpp and HexEdit.h to my project I get a bunch of compiler errors like:
Error 3 error C2665: 'AfxMessageBox' : none of the 2 overloads could
convert all the argument typesc:\projects\snap\memorycontroller\software\gui_test\pta2_led\pta2_led\hexedit.cpp
I've changed my CEdit control to "CHexEdit m_cEdit;" like your article says. Is there anything else I need to do?
I'm a old time C guy but I'm VERY new to VSC++/MFC so have patience
I'm using VS2005 if it matters
|
|
|
|
|
I think you are building your program with UNICODE support, so some changes should be made to the source code to make it work properly:
1.Add macro L to string, eg: change "abc" to L"abc";
2.Use string functions declared in TCHAR.H such as _tcscat, __tcscpy, _tcscmp etc.;
|
|
|
|
|
That was it!!!
Thanks a lot. When I went through the project setup wizzard with VS2005 I saw the unicode check box was "checked" and just left it that way. I generated a new test project with out unicode support and it worked fine.
Thanks for the quick reply!
|
|
|
|
|
The OnEnUpdate() method has seveal shortcomings that you might want to address:
1) As long as 0x is found in any part of the input string, m_bHex is set to true . This should only be the case if it is the first two characters only.
2) For an input such as 0xz , m_bHex is set to true and does not get set to false when the 'z' is encountered.
3) For an input such as a1b2 , m_bHex is never set to true even though it is a valid base-16 number.
How about something like:
void CHexEdit::OnEnUpdate()
{
CString text;
int nIndex = 0;
this->GetWindowText(text);
text.MakeLower();
this->m_bHex = true;
if (text.Left(2) == "0x")
nIndex = 2;
for (; nIndex < text.GetLength() && true == this->m_bHex; nIndex++)
{
if (! isxdigit(text[nIndex]))
this->m_bHex = false;
}
this->RedrawWindow();
}
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
|
|
|
|
|
I think the errors you mentions won't occur. The function CHexEdit::PreTranslateMessage has already handled the cases you mentioned.
|
|
|
|
|
Luo Pei`en wrote:
I think the errors you mentions won't occur.
If you think that something will not happen, that will ultimately be your demise!
Luo Pei`en wrote: The function CHexEdit::PreTranslateMessage has already handled the cases you mentioned.
You put validation code in the PreTranslateMessage() method. Why? That's not what that method is for. A better solution would be a FSM in the OnChar() method.
Why all the sscanf() calls with a CString object? Use CString::Format() instead.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
|
|
|
|
|
In fact, you can limit your input in the following four functions: 1)CEdit::PreTranslateMessage(); 2)CEdit::WindowProc(); 3)CEdit::OnChar(); 4)CEdit::DefWindowProc().
But however, WM_CHAR is not sent by some certain keys such as VK_DELETE. If you verify the input in OnChar() function, this would make things more complex. You may argue that you can add OnKeyDown(), but I think this is not a good way.
|
|
|
|
|
Gr8 work!. U get a 5/5 from me.
When user enters more than 6 chars . The only way to use the control is to delete all what u have typed inside the control.
Juz Replace this
void CHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
.......
if((sTemp.GetLength() < 6) || ((nChar == VK_BACK) ||(nChar == VK_DELETE)) )
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
with
void CHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
.........
if((sTemp.GetLength() < 6)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
|
|
|
|
|
... actually a GDI object leak.
In your handling of WM_CTRLCOLOR_REFLECT (CtlColor), you create a brush handle every single time. One way to fix it is to have two CBrush members in the class, say m_brHex and m_brDec. Create the solid brush for them in the constructor and return the respective m_hObject in the CtlColor function. In the methods for changing the colors, call DeleteObject first for the brush, then re-create it with the new color.
For example, in the constructor, I added:
m_brHex.CreateSolidBrush(m_icHexText.clrBkColor);
m_brDec.CreateSolidBrush(m_icDecText.clrBkColor);
In CtlColor:
HBRUSH CHexEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_bHex)
{
pDC->SetTextColor(m_icHexText.clrTextColor);
pDC->SetBkColor(m_icHexText.clrBkColor);
return (HBRUSH)m_brHex.m_hObject;
}
else
{
pDC->SetTextColor(m_icDecText.clrTextColor);
pDC->SetBkColor(m_icDecText.clrBkColor);
return (HBRUSH)m_brDec.m_hObject;
}
}
In SetHexColor, I added:
m_brHex.DeleteObject();
m_brHex.CreateSolidBrush(m_icHexText.clrBkColor);
(and something similar to SetDecColor).
Hope this helps!
|
|
|
|
|
Thanks. Src code will be updated soon.
|
|
|
|
|
The routine OnEnUpdate() has some interesting words in the TODO area, but I'm unable to decipher them. Did my download garble things?
|
|
|
|
|
I was about to write one of these for a demo I am working on. You beat me to it! I may modify it so that I do not need to change the entire dialog's font to courier; just the edit field.
Also, you used LPTSTR (and simular) which would work fine in Unicode. I would have just gone the extra distance and used the _t string commands instead of the standard set (e.g. use _tcscpy() instead of strcpy(), _tcslen() instead of _strlen(), use _T() wrappers around literals, etc.).
Nice work, though. Thanks for the article.
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|