CDialogBar中的控件变量问题
搜索了关于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...?");”生效吗?