VS 2008,MFC:添加 OnInitDialog - 如何?

2022-01-12 00:00:00 ide visual-studio-2008 mfc

这是我第一个远离VC6的MFC应用,感觉有点傻:

This is my first MFC application away from VC6, and I feel a little bit stupid:

如何添加 OnInitDialog 处理程序?(我知道如何手动添加它,但从长远来看这是一种痛苦).

How do I add the OnInitDialog handler? (I know how to add it manually, but that's a pain in the long run).

双击对话框 - 没有.右键单击对话框-添加事件处理程序"被禁用.属性 - 消息具有正常"消息,但没有 WM_INITDIALOG属性 - 事件仅保存来自包含控件的通知在类视图中右键单击-添加.."只有函数和变量

double-clicking the dialog - nothing. right click the dialog - "add event handler" is disabled. Properties - Messages has "normal" messages, but not WM_INITDIALOG Properties - Events only holds notifications from contained controls right-clicking in class view - "Add.." only has functions and variables

抓头

[edit] d'oh - 它是 MFC 中的虚函数,但仍然...

[edit] d'oh - it's a virtual function in MFC, but still...

推荐答案

别傻了,当我第一次从 VC6 迁移到 VS2008 时,我花了很长时间才弄清楚这一点!

Don't feel stupid, it took me forever to figure this out when I first moved from VC6 to VS2008!

无论如何,也供我自己参考,以下是添加对话框和覆盖 OnInitDialog 方法的完整步骤:

Anyway, and also for my own reference, here are the complete steps for adding a dialog box and overriding the OnInitDialog method:

  1. 选择资源视图并展开 .rc 文件.
  2. 右键单击树视图中的 Dialog 条目,然后选择 Insert Dialog.
  3. 选择属性窗口.(注意:如果您没有看到此窗口,请选择查看 > 其他窗口 > 属性窗口菜单项.)
  4. 在 Properties 窗口中,设置对话框的 ID,例如 IDD_MYDIALOG.
  5. 右键单击资源编辑器中的对话框并选择添加类.注意:如果您安装了 IE8,这可能会产生 Internet Explorer 脚本错误.请参阅 这篇博文了解如何更正此问题的详细信息.
  6. 在MFC Class Wizard对话框中,输入类名,如CMyDialog,选择CDialog作为基类,然后按完成.这将创建名为 MyDialog.cppMyDialog.h 的文件并将它们添加到您的项目中.
  1. Select Resource View and expand the .rc file.
  2. Right-click the Dialog entry in the tree view and select Insert Dialog.
  3. Select the Properties window. (Note: If you don't see this window, select the View > Other Windows > Properties Window menu item.)
  4. In the Properties window, set the ID for the dialog, e.g., IDD_MYDIALOG.
  5. Right click the dialog in the resource editor and select Add Class. Note: if you have IE8 installed, this may produce an Internet Explorer Script Error. See this blog post for details on how to correct this.
  6. In the MFC Class Wizard dialog box, enter the class name, e.g., CMyDialog, select CDialog as the base class, then press Finish. This will create files named MyDialog.cpp and MyDialog.h and add them to your project.

要覆盖 CMyDialog 类中的 OnInitDialog 方法:

To override the OnInitDialog method in the CMyDialog class:

  1. 打开文件MyDialog.h.
  2. 选择属性窗口.
  3. 将光标放在读取 class CMyDialog : public CDialog 的行上.(属性窗口的顶部应该显示CMyDialog VCCodeClass" - 这很重要,因为 Properties 窗口对上下文非常敏感,并且您可以根据位置获得不同的选项编辑器中的光标.)
  4. 点击属性窗口中的覆盖图标.
  5. 向下滚动属性窗口以找到 OnInitDialog.
  6. 单击此条目的向下箭头并选择<添加>OnInitDialog.这应该会创建您的 CMyDialog::OnInitDialog 函数.
  1. Open the file MyDialog.h.
  2. Select the Properties window.
  3. Place the cursor on the line that reads class CMyDialog : public CDialog. (The top of the Properties window should show "CMyDialog VCCodeClass" - this is important, because the Properties window is highly context sensitive, and you get different options depending on the location of the cursor in the editor.)
  4. Click the Overrides icon in the Properties window.
  5. Scroll down the properties window to find OnInitDialog.
  6. Click the down arrow against this entry and select <Add> OnInitDialog. This should create your CMyDialog::OnInitDialog function.

我希望这会有所帮助!

相关文章