简体   繁体   English

在MFC中添加加速器(快捷方式) - 如何?

[英]Adding accelerators(shortcuts) in MFC - HOW?

I found this link: http://support.microsoft.com/kb/222829 我找到了这个链接: http//support.microsoft.com/kb/222829

But I can't understand that much. 但我无法理解那么多。

Ok, I understood I need to add this to my header file: 好的,我知道我需要将它添加到我的头文件中:

HACCEL  m_hAccelTable;

and then this: 然后这个:

m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));

to my main .cpp 到我的主要.cpp

But where does this go? 但这又往哪里了?

BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg) {
   if (m_hAccelTable) {
      if (::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg)) {
         return(TRUE);
      }
   }
   return CDialog::PreTranslateMessage(pMsg);

}

I need around 6 shortcuts (CTRL + U to load something, CTRL + O to load smth else), I can't understand how this works, need a little bit of help 我需要大约6个快捷方式(CTRL + U加载东西,CTRL + O加载其他东西),我无法理解这是如何工作的,需要一点帮助

Now, MSDN article is misleading. 现在,MSDN文章具有误导性。 It shows how to add accelerators to About box and only About box will be able to handle accelerator that is in this case equivalent of pressing the button with IDC_BUTTON1 ID. 它显示了如何将加速器添加到“关于”框中,只有“关于”框将能够处理加速器,在这种情况下,相当于按下具有IDC_BUTTON1 ID的按钮。

You need to do something very different allowing all objects in your application to get a chance to handle this message. 您需要做一些非常不同的事情,允许应用程序中的所有对象都有机会处理此消息。 This is done for you in MDI/SDI apps. 这是在MDI / SDI应用程序中为您完成的。

Once you create accelerator table in the resource, you have to add accelerators: Key combination paired Accelerator key combination, when used generates command message with appropriate ID. 在资源中创建加速器表后,必须添加加速器:组合密钥组合加速器组合键,在使用时生成具有适当ID的命令消息。 Once you are done adding, you have to create command message handlers for each of the ID. 完成添加后,必须为每个ID创建命令消息处理程序。 When accelerator is used the handler is invoked and you can add the code you need. 使用加速器时,将调用处理程序,您可以添加所需的代码。 Now do this: Declare HACCEL type variable to your app class. 现在执行此操作:向您的app类声明HACCEL类型变量。 In the InitInstance call LoadAccelerators. 在InitInstance中调用LoadAccelerators。 Use wizard to insert PreTranslateMessage override in your application class. 使用向导在应用程序类中插入PreTranslateMessage覆盖。 Add following: 添加以下:

      if (m_hAccelTable) 
      {
                if (::TranslateAccelerator(*m_pMainWnd, m_hAccelTable, pMsg)) 
                {
                          return(TRUE);
                }
      }

This will allow the main dialog to handle accelerators. 这将允许主对话框处理加速器。 Note *m_pMainWnd. 注意* m_pMainWnd。 It is your dialog handle (automatically casted). 它是您的对话框句柄(自动转换)。 Now you can add handlers for any accelerator to the dialog or to the application class. 现在,您可以将任何加速器的处理程序添加到对话框或应用程序类。 You can also route command messages to any window in your application using OnCmdMsg. 您还可以使用OnCmdMsg将命令消息路由到应用程序中的任何窗口。

My advice for the future. 我对未来的建议。 When you decide to make your app a dialog based, consider creating SDI application with CFormView derived class. 当您决定使应用程序成为基于对话框时,请考虑使用CFormView派生类创建SDI应用程序。 You can change frame style to not allow resizing and it will look like dialog based but. 您可以更改框架样式以不允许调整大小,它看起来像基于对话框但是。 . . You will have ability to use a toolbar a menu for free and most importantly you will have all accelerator and command routing for free. 您将能够免费使用工具栏菜单,最重要的是,您将免费获得所有加速器和命令路由。

The page you referenced describes adding an accelerator table to a dialog based applicaion. 您引用的页面描述了将加速器表添加到基于对话框的应用程序。

Are you creating a dialog based application or just a normal MFC frame based application with a menu bar? 您是使用菜单栏创建基于对话框的应用程序还是仅使用普通的基于MFC帧的应用程序?

If you are doing the former then as the page you referenced suggest you need to override the PreTranslateMessage dialog box method. 如果您正在执行前者,那么当您引用的页面建议您需要覆盖PreTranslateMessage对话框方法时。

If you are doing the later then you only need to call the CFrameWnd::LoadAccelTable function. 如果您正在执行稍后操作,则只需调用CFrameWnd :: LoadAccelTable函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM