Click here to Skip to main content
Licence 
First Posted 23 Mar 2006
Views 24,415
Bookmarked 16 times

Accelerators and WTL Dialogs

By | 23 Mar 2006 | Article
An article on using accelerators in WTL dialogs.

image

Introduction

I searched and searched the CodeProject but never found an example on using accelerators and WTL dialogs. I have used accelerators in MFC dialogs extensively, but couldn't figure out how to add this functionality to WTL dialogs. Like a lot of things, it is very easy to do once you have figured it out. Well, here goes....

Using the code

Declare a handle to the accelerator, and add the CMessageFilter if it has not been done already.

#pragma once

class CMainDlg : public CDialogImpl<CMainDlg>, 
         public CUpdateUI<CMainDlg>,
         public CMessageFilter, 
         public CIdleHandler
{
private:
    HACCEL    m_haccelerator;
//.......
};

Then in your OnInitDialog, assign the m_haccelerator variable to the accelerator resource, which in this example is IDR_MAINFRAME.

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, 
        LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    // .......

    //Bind keys...
    m_haccelerator = AtlLoadAccelerators(IDR_MAINFRAME);

    // register object for message filtering and idle updates    
    CMessageLoop* pLoop = _Module.GetMessageLoop();
    ATLASSERT(pLoop != NULL);
    pLoop->AddMessageFilter(this);
    pLoop->AddIdleHandler(this);

    //...............

    return TRUE;
}

Then we need to overload the PreTranslateMessage function...

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{    
    if(m_haccelerator != NULL)
    {
        if(::TranslateAccelerator(m_hWnd, m_haccelerator, pMsg))
            return TRUE;
    }

    return CWindow::IsDialogMessage(pMsg);
}

Also, in you constructor, initialize the handle to the accelerator.

CMainDlg::CMainDlg()
{
    //..................

    m_haccelerator = NULL;

    //..................
}

If the dialog wasn't made to be modeless, it needs to be for the PreTranslateMessage to work. This is easily done by...

int WINAPI _tWinMain(HINSTANCE hInstance, 
    HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
    _Module.Init(NULL, hInstance);

    CMessageLoop myMessageLoop;
    _Module.AddMessageLoop(&myMessageLoop);

    CMainDlg dlgMain;
    dlgMain.Create(NULL);
    dlgMain.ShowWindow(nCmdShow);

    int retValue = myMessageLoop.Run();

    _Module.RemoveMessageLoop();
    _Module.Term();

    return retValue;
}

And make sure you include atlmisc.h.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rory Buchanan



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCopy/Paste Pinmemberwejr8:25 16 Apr '09  
GeneralCAccelerator PinmemberIgor Vigdorchik16:58 2 Oct '06  
QuestionHow can I use this for a modeless dialog? Pinmemberhanjack15:02 5 Sep '06  
Questiondifferent accelerator schema for each dialog? PinmemberMihai Moga21:22 23 Mar '06  
AnswerRe: different accelerator schema for each dialog? Pinmemberrbuchana15:20 24 Mar '06  
GeneralRe: different accelerator schema for each dialog? PinmemberPablo Aliskevicius23:59 17 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 23 Mar 2006
Article Copyright 2006 by Rory Buchanan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid