如题,不明白两者的区别和用法,他们不都是在 TranslateMessage() 和 DispatchMessage() 之前调用的吗?那两者又有什么关系呢??

解决方案 »

  1.   

    恩恩,不过看那里不是很明白这样我看了MSDN说的好像最大的区别就是ProcessMessageFilter是关于hook的
      

  2.   

    ProcessMessageFilter 在特定的消息到达应用程序之前截获消息
    PreTranslateMessage 在消息被发送到Windows函数TranslateMessage和DispatchMessage之前过滤消息   应用层面不同
      

  3.   

    Introduction
    There are a substantial number of Windows programmers who insist, often very vehemently, that a programmer should avoid overriding PreTranslateMessage. They have their reasons for saying so and I believe they are correct. But in this article my intention is not to contemplate on whether PreTranslateMessageis good for you or whether you should avoid it like the plague. I have found that PreTranslateMessage can come in quite handy in dialog-based applications for handling keyboard messages. In addition to usingPreTranslateMessage I also show you how you can override ProcessMessageFilter for handling accelerator keys in a dialog based application.Using PreTranslateMessage to handle dialog keystrokes
    Very often you hear questions from novice programmers asking how they can trap keystrokes in a dialog based application. Presumably they tried to handleWM_KEYDOWN/WM_KEYUP unsuccessfully. The whole problem is that in a dialog based application the focus is always on one of the child controls and not on the main dialog window. So what do you need to do? You need to override PreTranslateMessage. I'll show you a simple example.Suppose that you have a dialog based app with a lot of edit boxes on the dialog. It's basically a data entry program and thus you feel it would make it easier for the end-user if pressing the ENTER key would take the focus to the next edit box, just as if he had pressed TAB. The solution is so very easy and straightforward withPreTranslateMessage as I'll demonstrate below.BOOL CPreTransTestDlg::PreTranslateMessage(MSG* pMsg) 
    {
    if(pMsg->message==WM_KEYDOWN)
    {
    if(pMsg->wParam==VK_RETURN)
    pMsg->wParam=VK_TAB;

    return CDialog::PreTranslateMessage(pMsg);
    }
    All I have done is to check whether the message is a WM_KEYDOWN, and if it is so, then I check to see if thewParam is VK_RETURN. If I find it so, I change the wParam to VK_TAB and then the base class implementation is called. Easy huh?Using ProcessMessageFilter to handle dialog-based accelerator keys
    Let's say you have a menu in your dialog based app. And you have an accelerator key for some particular task. You'll soon be disappointed to find that the hotkey does not work. The problem is that the modal dialog app's message loop does not call TranslateAccelerator. I do not know why this is so. Presumable the Microsoft team decided that people shouldn't use dialog based apps to write complicated applications, with hotkeys and menus.But as usual they have suggested a workaround too. Here's is how you go about implementing it. I'd like to state again, that even though this is a Microsoft recommended technique there will be a good majority of MFC gurus, like Joseph Newcomer for example, who would tell you that you shouldn't be doing this. But then sometimes you have to sacrifice elegance for getting things done quickly and with minimum effort.Add a member variable to your CWinApp derived class.HACCEL m_haccel;
    Use the resource editor to create a new Accelerator, by default it will be named IDR_ACCELERATOR1. And add a new accelerator key that is a short cut for some menu item.
    Put the following line in your InitInstance just before the line where the CDialog derived object is declaredm_haccel=LoadAccelerators(AfxGetInstanceHandle(), 
    MAKEINTRESOURCE(IDR_ACCELERATOR1));
    Now override ProcessMessageFilter and modify the function so that it looks like :-BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg) 
    {
    if(m_haccel)
    {
    if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg)) 
    return(TRUE);
    }return CWinApp::ProcessMessageFilter(code, lpMsg);
    }
    All we did was to call TranslateAccelerator and if it succeeds then we don't need to call the base classProcessMessageFilter, as the message has been handled. So we return TRUE.Disclaimer
    The author wishes to state here that the two methods mentioned above are generally used methods and the author is not in any way endorsing these methods. Users should read more on the usage ofPreTranslateMessage and ProcessMessageFilter before they use it in their programs.
      

  4.   


    那在ProcessMessageFilter()拦截消息处理,和在PreTranslateMessage()拦截消息处理是不是一样的?
      

  5.   

    差不多,就多一个钩子类型的参数。都有pMsg这个消息嘛
      

  6.   

     tiger9991 
    tiger9991 等级: 
    结帖率:94.12% 4
    更多勋章
      #3 得分:0 回复于: 2013-01-18 09:36:55 
    ProcessMessageFilter 在特定的消息到达应用程序之前截获消息
    PreTranslateMessage 在消息被发送到Windows函数TranslateMessage和DispatchMessage之前过滤消息   应用层面不同