我需要更改对话框的静态文本,作了此函数,去不能正常执行:
UINT ThreadProc(LPVOID str)
{
CString s;
for(int i=0;i<100;++i)
{
s.Format("%d%",i);
CWnd * pWnd=GetDlgItem(AfxGetMainWnd()->m_hWnd,IDC_STATIC);
pWnd->SetWindowText(s);
}
return 0;
}
报错:
E:\ImageSys\vclz\lz1\lz1Dlg.cpp(185) : error C2440: 'initializing' : cannot convert from 'struct HWND__ *' to 'class CWnd *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
10 个解决方案
//假如你的对话框类为mydlg;
UINT ThreadProc(LPVOID str)
{
mydlg mydlg1;
CString s;
for(int i=0;i<100;++i)
{
s.Format("%d%",i);
// CWnd * pWnd=GetDlgItem(AfxGetMainWnd()->m_hWnd,IDC_STATIC);
// pWnd->SetWindowText(s);
mydlg1.GetDlgItem(IDC_STATIC)->SetWindowText(s);
}
return 0;
}
CWnd * pWnd=(CWnd * )GetDlgItem(AfxGetMainWnd()->m_hWnd,IDC_STATIC);
或
CWnd * pWnd=CWnd::GetDlgItem(IDC_STATIC,AfxGetMainWnd()->m_hWnd)
IDC_STATIC当然不能修改了,必须把IDC_STATIC改成任何不叫IDC_STATIC的ID即可.
比如IDC_STATICTEST等等,就可以得到他的指针后,用 SetWindowText设置值了.
楼主是不是要实现在一个Lable里显示东西啊?同意楼上的,同时注意你要获取句柄,调用afxgetmainwnd()时该是对象指针啊
在你需要设置的对话框类的成员函数里添加
GetDlgItem(IDC_STATICTEST)->SetWindowText("test!!");
即可。