CDialogBar中的控件变量问题

VincintCao 2003-02-25 08:11:40
搜索了关于CDialogBar的问题,不外乎两类,关于按钮置灰和如何派生。而我的问题是关于CDialogBar中的控件成员变量的。
按照如下步骤(VC6):
----------------------------------------------------------------
1、在msdn中找到ctrlbar的例子程序(可通过CDialogBar Overview中找到),复制到本地,确定其可运行。
2、打开resource中的IDD_VIEWSELECT,往上面扔两个按钮控件(IDC_BUTTON1,IDC_BUTTON2)。
3、Ctrl+W激活classwizard,选择Create...
4、Name:CMyDialogBar Base class:CDialog Dialog ID:IDD_VIEWSELECT
5、添加CMyDialogBar的成员变量(对应的ID是IDC_BUTTON2)m_btnTest
6、分别在CMainFrame和CMyDialogBar中添加对IDC_BUTTON1的BN_CLICKED的消息响应。
7、关闭classwizard,添加代码如下:
void CMyDialogBar::OnButton1()
{
CString strMsg;
strMsg.Format("The HWND value of Button2 is:%d", m_btnTest.m_hWnd);
::AfxMessageBox(strMsg); // guess the number
m_btnTest.SetWindowText("How...?");
}
8、在MyDialogBar.h中将“class CMyDialogBar : public CDialog”改成“class CMyDialogBar : public CDialogBar”;将“CMyDialogBar(CWnd* pParent = NULL)”改成“CMyDialogBar()”
9、在MyDialogBar.cpp中
“CMyDialogBar::CMyDialogBar(CWnd* pParent /*=NULL*/)
: CDialog(CMyDialogBar::IDD, pParent)
”改成
“CMyDialogBar::CMyDialogBar()”;

“CDialog::DoDataExchange(pDX);
”改成
“CDialogBar::DoDataExchange(pDX);


“BEGIN_MESSAGE_MAP(CMyDialogBar, CDialog)
”改成
“BEGIN_MESSAGE_MAP(CMyDialogBar, CDialogBar)

10、在MainFrm.h中增加
“#include "MyDialogBar.h"”
并将
“CDialogBar m_wndDlgBar;
”改成
“CMyDialogBar m_wndDlgBar;”
11、编译运行。并单击Button1,观察结果。
----------------------------------------------------------------
结果是:The HWND value of Button2 is:0
Button2活生生的在你的眼前,其HWND却是0。原因是m_btnTest并没有与IDC_BUTTON2绑定(but why)。有兴趣可以用ATTACH()试一下。
很多关于取DialogBar上的控件的方法都是采用GetDlgItem(),然后再使用。这样的确是可以的,证明如下:
将刚才
“strMsg.Format("The HWND value of Button2 is:%d", m_btnTest.m_hWnd);
m_btnTest.SetWindowText("How...?");
”改成
“strMsg.Format("The HWND value of Button2 is:%d", GetDlgItem(IDC_BUTTON2)->m_hWnd);
GetDlgItem(IDC_BUTTON2)->SetWindowText("How...?");
”一切ok。

无论是自己手动attach还是如上这种方法,都不好,因为我的DialogBar里面有好多冬冬,每次都这样用的话太太太复杂了。有什么好的办法让 “m_btnTest.SetWindowText("How...?");”生效吗?
...全文
128 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
VincintCao 2003-02-26
  • 打赏
  • 举报
回复
To godsmile(笑非哮,世间还有我这号)
呵呵,我参照了别的帮助(http://support.dundas.com/Articles/ArticleBody.asp?ArticleID=161),自己得出了答案,不过,还是谢谢你。
Vincint 2003-02-26
  • 打赏
  • 举报
回复
修改的办法就是添加一个Create函数,如下
BOOL CMyDialogBar::Create(CWnd* pParentWnd, UINT nIDTemplate,
UINT nStyle, UINT nID)
{
if (!CDialogBar::Create(pParentWnd, nIDTemplate,
nStyle, nID))
return FALSE;
UpdateData(FALSE);
return TRUE;
}
godsmile 2003-02-26
  • 打赏
  • 举报
回复

CControlBar is the base class for the control-bar classes (CDialogBar).

The CWnd class provides the base functionality of all window classes in the Microsoft Foundation Class Library.

CWnd::DoDataExchange
virtual void DoDataExchange( CDataExchange* pDX );

Parameters

pDX

A pointer to a CDataExchange object.

Remarks

Called by the framework to exchange and validate dialog data.

Never call this function directly. It is called by the UpdateData member function. Call UpdateData to initialize a dialog box’s controls or retrieve data from a dialog box.

When you derive an application-specific dialog class from CDialog, you need to override this member function if you wish to utilize the framework’s automatic data exchange and validation. ClassWizard will write an overridden version of this member function for you containing the desired “data map” of dialog data exchange (DDX) and validation (DDV) global function calls.

To automatically generate an overridden version of this member function, first create a dialog resource with the dialog editor, then derive an application-specific dialog class. Then call ClassWizard and use it to associate variables, data, and validation ranges with various controls in the new dialog box. ClassWizard then writes the overridden DoDataExchange, which contains a data map. The following is an example DDX/DDV code block generated by ClassWizard:

yifeng_ch 2003-02-26
  • 打赏
  • 举报
回复
学习
VincintCao 2003-02-26
  • 打赏
  • 举报
回复
^_^是什么意思,有同感吗?
demetry 2003-02-26
  • 打赏
  • 举报
回复
^_^

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧