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 联系我们: