Re: Strange PreTranslateMessage Behavior (2)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 02/11/05


Date: Fri, 11 Feb 2005 09:45:08 -0500

First, get rid of all instances of GetDlgItem in your code. This should be thought of as
effectively obsolete except in very restricted and special cases, of which this is not
one.

There is no need to put CWnd:: in front of the method in any case. In fact, there is an
excellent chance this actually could create erroneous code (imagine if you had a CButton
class that overrode EnableWindow! GetDlgItem wouldn't give you a correct type for the
pointer, giving you a generic CWnd * instead of CMyButton *, but the CWnd:: override would
guarantee the wrong method was called!)

The correct solution is to create a control member variable; see my essay on Avoiding
GetDlgItem on my MVP Tips site. I hope you are not actually using names as silly as
Button1 and Button2 in your code; you should always change the names of the controls to
something meaningful, and thus get meaningful names for your functions. Note that if you
later change the button to be a subclass that redefines EnableWindow, your code works
correctly without change. The code you wrote would fail, as would the often-written
        ((CButton *)GetDlgItem(IDC_WHATEVER))->EnableWindow(FALSE);
Code should be written so that it always works correctly even when the control is
subclassed and the base methods overridden.

In the examples below, I've substituted "DoThis" and "DoThat" for whatever domain-specific
actions you would be doing. I never use the control name assigned by the dialog editor.

void CMyDlg::OnDoThis()
    {
     c _DoThat.EnableWindow(TRUE);
    }

void CMyDlg::OnDoThat()
   {
    c_DoThat.EnableWindow(FALSE);
   }

I suspect what has happened is as follows: when the second button is pressed, it is
disabled. But it still has the focus. Since the keyboard is now trying to send characters
to the control that has the focus, and that control is disabled, the characters are being
discarded.

I would suggest adding a SetFocus() to some window which is enabled, and which makes sense
to have the focus set. Perhaps just doing
void CMyDlg::OnDoThat()
   {
    c_DoThat.EnableWindow(FALSE);
    SetFocus( );
   }

will work, but I'm not sure if setting focus to the dialog will actually have an effect.
Try it,
On Thu, 10 Feb 2005 05:18:58 GMT, mcmillan@viselect.com (Brad McMillan) wrote:

>Hi:
>
>I have a vc++ 6.0 dialog-based application where I'm using
>PreTranslateMessage to send keyboard characters out the serial port
>with the following code (thank you, Joseph M. Newcomer):
>
>BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
>{
> if (pMsg->message == WM_CHAR )
> {
> TCHAR ch = (TCHAR)pMsg->wParam;
> pSPort->WriteData(&ch, sizeof(TCHAR));
> }
> return CDialog::PreTranslateMessage(pMsg);
>}
>
>I also have a couple of buttons that perform some functions and are
>enabled and/or disabled after the functions are complete. Stripped of
>all irrelevant code these functions are:
>
>void CMyDlg::OnButton1()
>{
> GetDlgItem(IDC_BUTTON2)->CWnd::EnableWindow(TRUE);
>}
>
>and
>
>void CMyDlg::OnButton2()
>{
> GetDlgItem(IDC_BUTTON2)->CWnd::EnableWindow(FALSE);
>}
>
>The problem I'm having is that after the 2 buttons are pressed the
>keyboard characters no longer show up in PreTranslateMessage and are
>no longer sent out the serial port.
>
>BTW, I also tried detecting the character by using
>TRACE(_T("the char was '%c' or %d\n"), ch, ch);
> (as suggested from an earlier posting) with the same result. The
>characters stopped showing up after the buttons were pressed.
>
>Can someone tell me how to diagnose this problem, or what I need to
>change such that the keyboard characters continue to be sent out the
>serial port?
>
>Thanks in advance for any help anyone can provide.
>
>Brad McMillan
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • Re: ? Post Window (Dialog) Creation Flag
    ... The point is that since the control has already been bound, ... all the GetDlgItem calls and change the ones that are for my class. ... And the control variables work correctly ... Therefore, I should be able to subcass a CStatic, override SetWindowText, ...
    (microsoft.public.vc.mfc)
  • Re: how to display character from 128-159 range
    ... coded graphic characters identified as Latin ... which defined the control characters as well. ... ISO-8859-1 being the Internet standard, ...
    (comp.unix.programmer)
  • Re: Reading in file containing Low Values doesnt work correctly
    ... line sequential record it will be processed and all control characters ... control character that needs to be preserved. ... printable, nor is it a normal control character like TAB or Linefeed. ... another then the low-values prefix acts as an escape character. ...
    (comp.lang.cobol)
  • Re: how to display character from 128-159 range
    ... which defined the control characters as well. ... ISO-8859-1 being the Internet standard, ...
    (comp.unix.programmer)
  • Re: Change export delimitor?
    ... Did you hit and hold the control key when you hit the j key? ... "PhillyRon" wrote: ... invisible characters but dec 013 and dec 010. ... "Dave Peterson" wrote: ...
    (microsoft.public.excel.misc)