MFC>将对话框连接到对话框类

[英]MFC> Connecting a dialog to a dialog class


I have defined a new dialog and its controls in an already existing resource file. I have also created a new file which will handle the events being generated from this dialog. But I am not sure how to connect these two.

我在已经存在的资源文件中定义了一个新对话框及其控件。我还创建了一个新文件,它将处理从该对话框生成的事件。但我不知道如何连接这两个。

Is the statement enum { IDD=IDD_NEW_DIALOG }; all that is required to connect the two? Or should we add some other statement?

语句是枚举{IDD = IDD_NEW_DIALOG};连接这两个所需的一切?或者我们应该添加一些其他声明?

3 个解决方案

#1


14  

The way this is usually done in MFC is to define a dialog template in the resource editor (as you've done), then in C++ derive a class from CDialog and associate it with the dialog template (which it sounds like you've done - it's not entirely clear).

这通常在MFC中完成的方法是在资源编辑器中定义一个对话框模板(就像你已经完成的那样),然后在C ++中从CDialog派生一个类并将它与对话框模板相关联(听起来你已经完成了) - 它并不完全清楚)。

What actually associates the two is the constructor for your CDialog code. If you look at dialog related code auto-generated by the MFC class wizard, you'll see in the constructor implementation something like this:

实际上将两者联系起来的是CDialog代码的构造函数。如果查看由MFC类向导自动生成的对话框相关代码,您将在构造函数实现中看到如下内容:

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyDlg::IDD, pParent)

where CMyDlg::IDD is defined as an enumeration with a value of your new dialog template's identifier. It's this that makes it all happen, not the declaration of the enum. You could modify it to

其中CMyDlg :: IDD被定义为具有新对话框模板标识符值的枚举。这就是让一切都发生,而不是枚举的声明。你可以修改它

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(IDD_NEW_DIALOG, pParent)

and it will still work (assuming IDD_NEW_DIALOG is the template id of your dialog in the resources), as all that's happening is the dialog id is being passed into the constructor.

并且它仍然可以工作(假设IDD_NEW_DIALOG是资源中对话框的模板ID),因为所有发生的事情都是将对话框id传递给构造函数。

In general, it's always worth remembering that, despite initial appearances, MFC does not bind to Windows through bits of compiler magic - it's all done with C++ and a few macros.

一般来说,总是值得记住的是,尽管初次出现,MFC并没有通过编译器魔法绑定到Windows - 它都是用C ++和一些宏完成的。

EDIT: DIALOGEX and DIALOG declare slightly difference dialog resource formats that Windows understands: the former is newer than the latter. However both have been around since at least Windows 95, so it's not a very significant distinction.

编辑:DIALOGEX和DIALOG声明Windows理解的略有不同的对话框资源格式:前者比后者更新。但是至少从Windows 95开始,两者都存在,因此它并不是一个非常重要的区别。

#2


1  

That's all that is needed when you create the dialog through the dialog class (DoModal(), or Create for a non-modal dialog), which is the normal way to go.

当您通过对话框类(DoModal()或创建非模态对话框)创建对话框时,这就是所需要的,这是正常的方法。

You of course need to inherit from CDialog, and add a message map to route the messages to your ewvent handler functions.

您当然需要从CDialog继承,并添加消息映射以将消息路由到您的ewvent处理函数。

#3


0  

Use class wizard to create a class for newly created dialog. ctrl+w is the shortcut key from UI resource view.

使用类向导为新创建的对话框创建类。 ctrl + w是UI资源视图中的快捷键。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/12/08/f22d538c9457d0a1d9248186136c0c60.html



 
  © 2014-2022 ITdaan.com 联系我们: