C++ MFC

Moderators: Lundin
Number of threads: 3354
Number of posts: 9032

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Handling a WM_KEYDOWN message... Posted by ShinGohan on 10 Jul 2003 at 3:28 AM
Hello, i'm new to MFC and i have got this problem:
i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?
Report
Re: Handling a WM_KEYDOWN message... Posted by stober on 10 Jul 2003 at 4:16 AM
: Hello, i'm new to MFC and i have got this problem:
: i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
: i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?
:

you need to subclass the CEdit control and handle it in that class. Create a new MFC class that is derived from CEdit, then in the dialog's .h file change the control's type from CEdit to your new class, for example CMyEdit.
Report
Re: Handling a WM_KEYDOWN message... Posted by ShinGohan on 10 Jul 2003 at 4:23 AM
: : Hello, i'm new to MFC and i have got this problem:
: : i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
: : i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?
: :
:
: you need to subclass the CEdit control and handle it in that class. Create a new MFC class that is derived from CEdit, then in the dialog's .h file change the control's type from CEdit to your new class, for example CMyEdit.
:
I tried it.. what i don't understand is: where do i write the event handler?
I need it to be a Dialog member and not a CMyEdit member, because i have to access to other members defined in the dialog class.. if i just overload the CEdit's OnKeyDown() method i can't access something that is "external" to the class..
Report
Re: Handling a WM_KEYDOWN message... Posted by stober on 10 Jul 2003 at 4:34 PM
One way I solved that problem is for CMyEdit to reflect the WM_KEYDOWN to its parent (the CDialog derived class that contains the edit control) Here is the code snippet. Use ClassWizard to create both event handlers.


void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN,nChar,0);	
	CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CTestDlgDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.