您可以使用对话框编辑器向对话框中添加控件或者通过代码自己添加控件。
下面的示例显示如何在派生对话框类的类声明中声明 CEdit 对象,然后调用 OnInitDialog 中的 Create 成员函数。由于 CEdit 对象被声明为嵌入对象,它将在对话框对象构造时自动构造,但仍必须用它自己的 Create 成员函数初始化。
class CMyDialog : public CDialog
{
protected:
CEdit m_edit; // Embedded edit object
public:
virtual BOOL OnInitDialog();
};
下列 OnInitDialog 函数设置一个矩形,然后调用 Create 以创建 Windows 编辑控件 (Edit Control) 并将其附加到未初始化的 CEdit 对象。
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CRect rect(85, 110, 180, 210);
m_edit.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP |
ES_AUTOHSCROLL | WS_BORDER, rect, this, 2);
m_edit.SetFocus();
return FALSE;
}
CWnd::Create
若要自己创建控件对象,通常要将 C++ 控件对象嵌入 C++ 对话框或框架窗口对象。与框架中的许多其他对象一样,控件也需要两步构造。作为创建父对话框或框架窗口的一部分,应调用控件的 Create 成员函数。对于对话框,这通常在 OnInitDialog 中完成;对于框架窗口,则通常在 OnCreate 中完成。
下面的示例显示如何在派生对话框类的类声明中声明 CEdit 对象,然后调用 OnInitDialog 中的 Create 成员函数。由于 CEdit 对象被声明为嵌入对象,它将在对话框对象构造时自动构造,但仍必须用它自己的 Create 成员函数初始化。
class CMyDialog : public CDialog
{
protected:
CEdit m_edit; // Embedded edit object
public:
virtual BOOL OnInitDialog();
};
下列 OnInitDialog 函数设置一个矩形,然后调用 Create 以创建 Windows 编辑控件 (Edit Control) 并将其附加到未初始化的 CEdit 对象。
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CRect rect(85, 110, 180, 210);
m_edit.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP |
ES_AUTOHSCROLL | WS_BORDER, rect, this, 2);
m_edit.SetFocus();
return FALSE;
}
创建编辑对象后,还可以通过调用 SetFocus 成员函数设置控件的输入焦点。最后从 OnInitDialog 返回 0,说明设置了焦点。如果返回非零值,对话框管理器将焦点设置为对话框项列表中的第一个控件项。
-------------------------------------------------------------------------------------------------------------------------------------------------------
CWnd::Create
窗口创建函数。指定父窗口的指针,将自己覆盖到父窗口上面。
virtual BOOL Create(
LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
Const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CCreateContext* pContext = NULL
);
Parameters
[in] lpszClassName 窗口类名
[in] lpszWindowName 窗口标题
[in] dwStyle 窗口类型
[in] rect 窗口在父窗口上面的位置,相对父窗口。
[in] pParentWnd 父窗口的指针
[in] nID 窗口的ID(类似Tag,用以区分父窗口中其他的子窗口)
[in] pContext Pointer to a CCreateContext structure that is used to customize the document-view architecture for the application.
Return Value
成功返回true,失败返回false
CToolBar::LoadToolBar
调用该成员函数的加载 lpszResourceName 或 nIDResource指定的工具栏。
BOOL LoadToolBar(
LPCTSTR lpszResourceName
);
BOOL LoadToolBar(
UINT nIDResource
);
参数
lpszResourceName
对要加载的工具栏的资源名称的指针。
nIDResource
要加载的工具栏的资源 ID。
返回值
非零,如果成功;否则为 0。