15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


WM_CHAR and CEdit

julian templeman -- julian@groucho.demon.co.uk
Saturday, April 06, 1996

[VC++ 4.0, NT 3.51]

In VC++ 4.0, if you handle WM_CHAR for a CEdit-derived control class
in order to filter the input, it appears that you can't alter what
gets passed on to CEdit. From the VC++ online help: "If you call the
base class implementation of this function, that implementation will
use the parameters originally passed with the message and not the
parameters you supply to the function."  (c) Microsoft, just to be
safe :-)

So, two questions...
(1) Why? 
(2) You commonly want to override OnChar() in order to do just that -
to alter the character that gets passed through to the base class.
What is the recommended way to do this if you can't use OnChar() for
the process?

It strikes me that the way OnChar() is coded isn't very helpful -
ClassWiz puts in a call to CEdit::OnChar() complete with parameters,
when it would be much better to have a parameterless version, thus
explicitly spelling out that you can't pass altered arguments on to
CEdit. The current version leads you to suppose you can, and you only
find out otherwise when your code doesn't work. and you look at the
help file!

julian

-- 
julian templeman             julian@groucho.demon.co.uk
[This .sig guaranteed 100% free from beef products]



Chet Murphy -- cmurphy@modelworks.com
Sunday, April 07, 1996

[Mini-digest: 2 responses]

julian templeman wrote:
> 
> [VC++ 4.0, NT 3.51]
> 
> In VC++ 4.0, if you handle WM_CHAR for a CEdit-derived control class
> in order to filter the input, it appears that you can't alter what
> gets passed on to CEdit. From the VC++ online help: "If you call the
> base class implementation of this function, that implementation will
> use the parameters originally passed with the message and not the
> parameters you supply to the function."  (c) Microsoft, just to be
> safe :-)
> 
> So, two questions...
> (1) Why?
> (2) You commonly want to override OnChar() in order to do just that -
> to alter the character that gets passed through to the base class.
> What is the recommended way to do this if you can't use OnChar() for
> the process?
> 
> It strikes me that the way OnChar() is coded isn't very helpful -
> ClassWiz puts in a call to CEdit::OnChar() complete with parameters,
> when it would be much better to have a parameterless version, thus
> explicitly spelling out that you can't pass altered arguments on to
> CEdit. The current version leads you to suppose you can, and you only
> find out otherwise when your code doesn't work. and you look at the
> help file!
> 
> julian
> 
> --
> julian templeman             julian@groucho.demon.co.uk
> [This .sig guaranteed 100% free from beef products]

julian,

Yes you can modify or insert other characters inside of your OnChar 
handler.  You do this by calling DefWindowProc(WM_CHAR, nChar, 
MAKELONG(nRepCnt, nFlags)) with what ever value of nChar you want.  You 
can even call DefWindowProc more than once.

--Chet Murphy
ModelWorks Software
cmurphy@modelworks.com
http://www.modelworks.com/express

-----From: "Stacey Blaschke" 

I can't answer question number 1 for you because I don't know the reasoning 
behind the decision
but I can answer question number 2.

To bypass the default behavior you will have to pass the changed nChar 
character onto the
default window procedure yourself this will let you bypass MFC's default 
behavior and let you 
change the character.

 
 afx_msg void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
   {
     //change nChar anyway you want to and then instead of
     //calling CEdit::OnChar call DefWindowProc
      DefWindowProc(WM_CHAR, nChar, MAKELONG(nRepCnt, nFlags));
   }

--Stacey Blaschke
Stacey_Blaschke@msn.com



Jeff Pek -- jpek@emeraldi.com
Monday, April 08, 1996

        Reply to:   RE>WM_CHAR and CEdit
>>
In VC++ 4.0, if you handle WM_CHAR for a CEdit-derived control class
in order to filter the input, it appears that you can't alter what
gets passed on to CEdit. From the VC++ online help: "If you call the
base class implementation of this function, that implementation will
use the parameters originally passed with the message and not the
parameters you supply to the function."  (c) Microsoft, just to be
safe :-)

So, two questions...
(1) Why? 
(2) You commonly want to override OnChar() in order to do just that -
to alter the character that gets passed through to the base class.
What is the recommended way to do this if you can't use OnChar() for
the process?
>>

I would try to use PreTranslateMessage override to do this. It should let
you change the message as you desire. I remember somthing obscure in the
documentation that says you can't alter the values of the parameters that
are passed on to superclasses' message handlers.

HTH
Jeff Pek
Emerald Intelligence




Crucial Software -- crucial@ix.netcom.com
Monday, April 08, 1996

[Mini-digest: 2 responses]

>> (1) Why? 

To complicate the nature of the universe?  :-)

>> (2) You commonly want to override OnChar() in order to do just that -
>> to alter the character that gets passed through to the base class.
>> What is the recommended way to do this if you can't use OnChar() for
>> the process?

Eat the message and post/send a new one with the correct parameters.  Or,
don't rely on the default implementation; insert the character yourself.
Or, eat the message, construct a new one, and call the default window
procedure for the control.

>> It strikes me that the way OnChar() is coded isn't very helpful -
>> ClassWiz puts in a call to CEdit::OnChar() complete with parameters,
>> when it would be much better to have a parameterless version, thus
>> explicitly spelling out that you can't pass altered arguments on to
>> CEdit. The current version leads you to suppose you can, and you only
>> find out otherwise when your code doesn't work. and you look at the
>> help file!

You have to remember that MFC != Windows.  The message is a Windows message,
and is handled in Windows.  The parameters are a "convenience" for the MFC
programmer.  Apparently (without looking, no flames if I'm wrong :-), the
default implementation of messages that should be handled by Windows is to
go get the original (packed) message and call DefWindowProc (or similar).

_That_ is why changing the parameters doesn't work.

--
Brad Wilson, Crucial Software     crucial@ix.netcom.com    +1 (810) 620-9803
Custom software engineering services for Microsoft Windows NT and Windows 95

            "Some may rise by sin; and some, fall by virtue."

-----From: michero@login.net (Michel Robert)

>(2) You commonly want to override OnChar() in order to do just that -
>to alter the character that gets passed through to the base class.
>What is the recommended way to do this if you can't use OnChar() for
>the process?
>
One author recommends using SendMessage from within the subclassed message
handler.





Dan Kirby -- dkirby@accessone.com
Friday, April 12, 1996

Article  Q92394 in the Microsoft Developer Knowledgebase 
(http://www.microsoft.com/kb) talks about filtering keystrokes in OnChar(). 
 Just do something like:

 /*
      Assume that CMyEdit is derived from the CEdit class.
   */

   afx_msg void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
   {
      if (nChar == 'a' || nChar == 'A')
         nChar = 'X';
      DefWindowProc(WM_CHAR, nChar, MAKELONG(nRepCnt, nFlags));
   }


--dan

----------
From: 	Brad Wilson[SMTP:crucial@ix.netcom.com]
Sent: 	Monday, April 08, 1996 6:23 AM
To: 	'mfc-l@netcom.com'
Subject: 	RE: WM_CHAR and CEdit

<>
An error prevented processing the message to completion. The original 
message is contained in the attached file MESSAGE.TXT.



begin 600 WINMAIL.DAT
M>)\^(@D.`0:0" `$```````!``$``0>0!@`(````Y 0```````#H``$(@ <`
M& ```$E032Y-:6-R;W-O9G0@36%I;"Y.;W1E`#$(`0V ! `"`````@`"``$$
MD 8`# $```$````,`````P``, (````+``\.``````(!_P\!````/P``````
M``"!*Q^DOJ,0&9UN`-T!#U0"`````&UF8RUL0&YE=&-O;2YC;VT`4TU44 !M
M9F,M;$!N971C;VTN8V]M```>``(P`0````4```!33510`````!X``S !````
M$0```&UF8RUL0&YE=&-O;2YC;VT``````P`5# $````#`/X/!@```!X``3 !
M````$P```"=M9F,M;$!N971C;VTN8V]M)P```@$+, $````6````4TU44#I-
M1D,M3$!.151#3TTN0T]-`````P``.0`````+`$ Z`0````(!]@\!````! ``
M``````(]+ $$@ $`%@```%)%.B!735]#2$%2(&%N9"!#161I= !.!@$%@ ,`
M#@```,P'! `,``<`!0`?``4`$P$!(( #``X```#,!P0`# `'``,`+P`%`"$!
M`0F `0`A````.$(Q.#4U-40S,3DT0T8Q,3DR,C@T-#0U-3,U-# P,# `N08!
M`Y &`,@$```4````"P`C```````#`"8```````L`*0```````P`N```````#
M`#8``````$ `.0#@7S8<>2B[`1X`< `!````%@```%)%.B!735]#2$%2(&%N
M9"!#161I= ````(!<0`!````%@````&[*'D<+EU5&(R4,1'/DBA$15-4````
M`!X`'@P!````!0```%--5% `````'@`?# $````5````9&MIL"@P!0$P-4`@!C: K  @9&\@
M3QR@!X <$"%R;&DAL#IC"H4*A2 O*B4F)B-!+000=0> ' %A!4!#3?!Y161I
M!4 $`". !G'='1!D(0`#81P#0R>#&R#)'C!S+B7(*B\DO1M00&%F>%]M"YN$@`?T1_"74"?WT&M!F ",$+?0^M-`B [P)IY+A!!/W #
M$3 X+A" ,3DY-B V.AN0S1-P34>"B[`4 `"#" RW?>>"B[`1X`/0`!````!0``
3`%)%.B ``````P`--/TW``#A02B[
`
end





| Вернуться в корень Архива |